Skip to main content

A python framework for implementing a saga pattern.

Project description

Saga Framework

Overview

The Saga Pattern Implementation in Python is a robust and flexible library that facilitates managing complex transactions in distributed systems using the Saga pattern. It ensures data consistency and reliability by handling long-running transactions through a sequence of local transactions, each with corresponding compensating actions in case of failures.

Features

  • Task Management: Easily add and manage a sequence of tasks that constitute a saga.
  • Compensation Mechanism: Automatically compensates completed tasks in reverse order if a failure occurs.
  • Context Handling: Share contextual information across tasks seamlessly.
  • Comprehensive Logging: Detailed logging of saga execution, task progress, and error handling.
  • Customizable Logging: Inject custom loggers to tailor logging to your needs.
  • Exception Handling: Robust error handling with custom exceptions for clear error propagation.
  • Extensible Design: Modular architecture allowing for easy extension and customization.

Table of Contents

Installation

You can install the Saga library using pip:

pip install saga-framework

Usage Here's a simple example demonstrating how to define and execute a saga with tasks and compensations:

from saga import Saga, Task, Context
from saga.logging import SagaLogger
from saga.errors import SagaExecutionException

# Define your tasks
class CreateOrderTask(Task):
    def execute(self, context: Context):
        # Logic to create an order
        print("Order created.")

    def compensate(self, context: Context):
        # Logic to compensate order creation
        print("Order creation compensated.")

class ChargePaymentTask(Task):
    def execute(self, context: Context):
        # Logic to charge payment
        print("Payment charged.")
        # Simulate failure
        raise SagaExecutionException("Payment failed.")

    def compensate(self, context: Context):
        # Logic to refund payment
        print("Payment refunded.")

# Initialize Saga with a logger
logger = SagaLogger()
saga = Saga(logger=logger)

# Add tasks to the saga
saga.add_task(CreateOrderTask())
saga.add_task(ChargePaymentTask())

# Execute the saga
try:
    saga.execute()
except SagaExecutionException as e:
    print(f"Saga failed: {e}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

saga_framework_python-0.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

saga_framework_python-0.0.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file saga_framework_python-0.0.2.tar.gz.

File metadata

  • Download URL: saga_framework_python-0.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for saga_framework_python-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6617201966e29737e77239d5d12b48becfec63b71dae5c296d75653ce1666f4d
MD5 a7e51fd4cadf1d581e6cedb49f6682d5
BLAKE2b-256 2a3f88b21cb0e68f31e9ca959dc5ee04b59290da8363a4852306803d43b93dd2

See more details on using hashes here.

File details

Details for the file saga_framework_python-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for saga_framework_python-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 87b90985b96559944ed2b32432cbc133ebf975c33a20da7c5eeaf55f0dc192db
MD5 77b70658bbc3766a3446319d33e25d93
BLAKE2b-256 0a252a7e2f905a00f7ce792713212357dacffcd24e15b72ad37459065eade0c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page