Skip to main content

Simple async sagas package

Project description

Tests PyPI version Publish

SAGAS

A simple async sagas implementation

Installation

$ pip install pysagas

Usage

Simple example

import asyncio

from sagas import SagaBuilder

counter = [10, 20]
amount = 30


def add(amount, index):
    """Increments array index by amount

    Args:
        amount (int): increment value
        index (int): index of value to increment        
    """    
    counter[index] += amount


def decrement(amount, index):
    """Decrements array index by amount

    Args:
        amount (int): decrement value
        index (int): index of value to decrement        
    """    
    counter[index] -= amount
    return counter


saga_builder = SagaBuilder.create()

saga = saga_builder\
    .add_step(lambda: add(amount, 0), lambda: decrement(amount, 0))\
    .add_step(lambda: add(amount, 1), lambda: decrement(amount, 1))\
    .build()

if __name__ == "__main__":
    asyncio.run(saga.run(exceptions=(OSError)))
    print(counter)  # [40, 50]

Failure example

If one step fails, the compensating functions for the executed steps run and the counter values are compensated

import asyncio

from sagas import SagaBuilder

counter = [10, 20]
amount = 30


def add(amount, index):
    """Increments array index by amount

    Args:
        amount (int): increment value
        index (int): index of value to increment
    """
    counter[index] += amount
    raise Exception('error occurred')


def decrement(amount, index):
    """Decrements array index by amount

    Args:
        amount (int): decrement value
        index (int): index of value to decrement
    """
    counter[index] -= amount
    return counter


saga_builder = SagaBuilder.create()

saga = saga_builder\
    .add_step(lambda: add(amount, 0), lambda: decrement(amount, 0))\
    .add_step(lambda: add(amount, 1), lambda: decrement(amount, 1))\
    .build()

if __name__ == "__main__":
    asyncio.run(saga.run(exceptions=(OSError, Exception)))
    print(counter)  # [10, 20]

CHANGES

  • add test step
  • add unit tests
  • ch(docs):add documentation- add readme docs- add usage examples

0.0.2

  • rename package
  • add changelog
  • add setup.py file

0.0.1

  • remove example
  • Setup publishing to pypi
  • initial commit

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

pysagas-0.0.3.tar.gz (5.7 kB view hashes)

Uploaded source

Built Distribution

pysagas-0.0.3-py3-none-any.whl (2.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page