Skip to main content

Flexible and extensible implementation of the Saga patterns

Project description

ORSA (ORchestrator SAga)

This module provides a flexible and extensible framework for implementing Saga patterns in Python.
It enables coordination of complex business transactions spanning multiple services, ensuring eventual consistency through choreography and compensation mechanisms.

Key features include:

  • Simplified Saga Declaration - Streamlined saga declaration using decorators and intuitive syntaxis, enabling rapid creation of complex distributed transactions

  • Saga Orchestration - Full-featured saga orchestration with support for complex execution scenarios, including parallel and sequential operations

  • Extensible Saga Execution Manager - Extendable saga execution manager with customizable execution strategies and error handling capabilities

  • Compensation Logic Management - Automated compensation logic management with support for compensation chains and complex rollback scenarios

  • Asynchronous Execution - Asynchronous saga execution support with handling of long-running operations and events

  • Readiness Probe Before Saga Execution - Pre-execution system readiness checks with automatic dependency and resource discovery

TODO

  • Step executioin flow control (saga.goto(step_name))

Example

saga_example.py

import httpx
from logging import getLogger
from orsa import orchestrator, Saga, Result

_logger = getLogger('EXAMPLES')

@orchestrator
async def currency_exchange(saga: Saga, amount: float, fromCurrency: str, toCurrency: str):
    """
    Sample Saga. Exchange from currencies
    """
    @saga.step(retry=3) # Number  of retries for this step
    async def get_exchange_rates() -> dict[str,tuple[float,float]]:
        async with httpx.AsyncClient() as cli:
            res = await cli.get('https://api.nbrb.by/exrates/rates?periodicity=0')
            res.raise_for_status()
            data = res.json()
            _logger.info("Obtain exchange rates ... success (%d)",len(data))
            return { cur['Cur_Abbreviation']: (cur['Cur_OfficialRate'],cur['Cur_Scale']) for cur in data }

    @saga.step
    def convert_to_base_currency(ExchRates: Result[dict[str,tuple[float,float]], get_exchange_rates]) -> float:
        _rate, _scale = ExchRates[fromCurrency]
        _baseAmount = amount * (_rate / _scale)

        _logger.info("Convert %.3f (%s) to %.3f (BYN) ... rate (%.3f)",amount, fromCurrency, _baseAmount, (_rate / _scale))

        return _baseAmount

    @saga.step
    def convert_to_dst_currency(
            BaseAmount: Result[float,convert_to_base_currency], 
            ExchRates: Result[dict[str,tuple[float,float]], get_exchange_rates]) -> float:

        _rate, _scale = ExchRates[toCurrency]
        _dstAmount = BaseAmount / (_rate / _scale)

        _logger.info("Convert %.3f (%s) to %.3f (%s)",amount, fromCurrency, _dstAmount, toCurrency)

        return _dstAmount

main.py

import asyncio, logging
from saga_example import currency_exchange

logging.basicConfig(level=logging.INFO)

async def add_task():
    await currency_exchange(45.12,'USD','CNY')

if __name__ == "__main__":
    asyncio.run(add_task())

See other examples

License

MIT License

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

orsa-0.1.2.1.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

orsa-0.1.2.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file orsa-0.1.2.1.tar.gz.

File metadata

  • Download URL: orsa-0.1.2.1.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for orsa-0.1.2.1.tar.gz
Algorithm Hash digest
SHA256 78c7e05b95fb0b5a189584f6e0fb1bd94895f3ca3615a0947edf798f43d51c35
MD5 398e558e5843d4f8905a17f4e0ac19bb
BLAKE2b-256 0a17a282843931bd2dadd689955f6a966c7079299d9a67706d3db3a70ff2ff87

See more details on using hashes here.

File details

Details for the file orsa-0.1.2.1-py3-none-any.whl.

File metadata

  • Download URL: orsa-0.1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for orsa-0.1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5c4e0e68206f9cba2cce9440d4a01c84f9e7021d19d5a3a11a324173986a7845
MD5 a9f6fd0453de65b3bd227f418f509996
BLAKE2b-256 5e866b6fe547f7c00c509063c451345596a38cc8b8ea4ca4194aeaee35fadc8a

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