https://github.com/narrativescience-old/sagah
Orchestrated, async sagas in Python
https://github.com/narrativescience-old/sagah
Last synced: 7 months ago
JSON representation
Orchestrated, async sagas in Python
- Host: GitHub
- URL: https://github.com/narrativescience-old/sagah
- Owner: NarrativeScience-old
- License: bsd-3-clause
- Created: 2021-06-29T22:45:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-01T19:25:30.000Z (almost 5 years ago)
- Last Synced: 2025-02-18T09:40:45.197Z (over 1 year ago)
- Language: Python
- Size: 12.7 KB
- Stars: 0
- Watchers: 12
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# sagah
[](https://pypi.org/pypi/sagah/) [](https://opensource.org/licenses/BSD-3-Clause)
Orchestrated, async sagas in Python
Table of Contents:
- [Installation](#installation)
- [Guide](#guide)
- [Development](#development)
## Installation
sagah requires Python 3.6 or above.
```bash
pip install sagah
```
## Guide
Example usage:
```python
from sagah import Saga
state = {"counter": 0}
def incr():
state["counter"] += 1
def decr():
state["counter"] -= 1
with Saga() as saga:
await saga.action(incr, decr)
await saga.action(incr, decr)
assert state["counter"] == 2
```
If some action fails, the compensating functions from previous transactions will be called to restore the state:
```python
from sagah import Saga
state = {"counter": 0}
def incr():
state["counter"] += 1
def decr():
state["counter"] -= 1
def fail():
raise ValueError("oops")
try:
with Saga() as saga:
await saga.action(incr, decr)
await saga.action(incr, decr)
await saga.action(fail, noop)
except Exception:
assert state["counter"] == 0
```
## Development
To develop sagah, install dependencies and enable the pre-commit hook:
```bash
pip install pre-commit poetry
poetry install
pre-commit install
```
To run tests:
```bash
poetry run pytest
```