Advertisement
Mochinov

Untitled

Oct 31st, 2023
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. class Saga:
  2.     def __init__(self):
  3.         self.steps = []
  4.         self.current_step = -1
  5.         self.completed = False
  6.  
  7.     def add_step(self, step):
  8.         self.steps.append(step)
  9.  
  10.     def execute(self):
  11.         try:
  12.             if self.completed:
  13.                 return
  14.             for step in self.steps:
  15.                 self.current_step += 1
  16.                 result = step.execute()
  17.                 step.set_result(result)
  18.                 if not result:
  19.                     self.rollback()
  20.                     break
  21.             else:
  22.                 self.completed = True
  23.         except Exception as e:
  24.             print(f"Error during saga execution: {e}")
  25.  
  26.     def rollback(self):
  27.         if self.current_step < 0:
  28.             return
  29.         for i in range(self.current_step, -1, -1):
  30.             step = self.steps[i]
  31.             result = step.get_result()
  32.             try:
  33.                 step.rollback(result)
  34.             except Exception as e:
  35.                 print(f"Error during rollback of step {step}: {e})
  36.            finally:
  37.                self.current_step -= 1
  38.  
  39. class SagaStep:
  40.    def execute(self):
  41.        pass
  42.  
  43.    def rollback(self, result):
  44.        pass
  45.  
  46.    def get_result(self):
  47.        pass
  48.  
  49.    def set_result(self, result):
  50.        pass
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement