Created
February 11, 2021 13:08
-
-
Save absent1706/47e4edbd7d222e6d9e5fd660b5dc8d0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install saga_py | |
from saga import SagaBuilder, SagaError | |
counter1 = 0 | |
counter2 = 0 | |
def _print_counters(): | |
global counter1, counter2 | |
print(f'After this action, {counter1=}, {counter2=} \n') | |
def incr_counter1(amount): | |
print('action 1 runs') | |
global counter1 | |
counter1 += amount | |
_print_counters() | |
def incr_counter2(amount): | |
print('action 2 runs. it will fail soon') | |
global counter2 | |
counter2 += amount | |
_print_counters() | |
raise BaseException('some error happened') | |
def decr_counter1(amount): | |
print('compensation 1 runs') | |
global counter1 | |
counter1 -= amount | |
_print_counters() | |
def decr_counter2(amount): | |
print('compensation 2 runs') | |
global counter2 | |
counter2 -= amount | |
_print_counters() | |
try: | |
SagaBuilder \ | |
.create() \ | |
.action(lambda: incr_counter1(15), lambda: decr_counter1(15)) \ | |
.action(lambda: incr_counter2(1), lambda: decr_counter2(1)) \ | |
.build() \ | |
.execute() | |
except SagaError as e: | |
print('saga error: ', e) # wraps the BaseException('some error happened') | |
_print_counters() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment