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.1.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.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: saga_framework_python-0.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ae4a3b91ef7749ceb7511594c6db316b5f71583970728cb32cd44e1097508b76
MD5 90e94af9f06e506cc62cfebaba8664c2
BLAKE2b-256 cfa8bdc3f477c4055f311d3f073d039dae15d2597197a19bdc095b3a0890bcaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for saga_framework_python-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fea321792295f8fd1f840cbddfb7deb504e0a2cb3a5597f7565e64b76704ee4b
MD5 3ac241a050396a57afa9ebddb82f4275
BLAKE2b-256 a10207d876a74ff40fe26da6c9ec9128037c52982cd5fa4cc35f282a44466a4e

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