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.dev5.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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