Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Saga:
- def __init__(self):
- self.steps = []
- self.current_step = -1
- self.completed = False
- def add_step(self, step):
- self.steps.append(step)
- def execute(self):
- try:
- if self.completed:
- return
- for step in self.steps:
- self.current_step += 1
- result = step.execute()
- step.set_result(result)
- if not result:
- self.rollback()
- break
- else:
- self.completed = True
- except Exception as e:
- print(f"Error during saga execution: {e}")
- def rollback(self):
- if self.current_step < 0:
- return
- for i in range(self.current_step, -1, -1):
- step = self.steps[i]
- result = step.get_result()
- try:
- step.rollback(result)
- except Exception as e:
- print(f"Error during rollback of step {step}: {e})
- finally:
- self.current_step -= 1
- class SagaStep:
- def execute(self):
- pass
- def rollback(self, result):
- pass
- def get_result(self):
- pass
- def set_result(self, result):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement