Execute all commands, rolling back on failure.
(self)
| 128 | self.executed_commands = [] |
| 129 | |
| 130 | def execute(self) -> bool: |
| 131 | """Execute all commands, rolling back on failure.""" |
| 132 | print(f"\n=== COMPOSITE COMMAND EXECUTE START ===") |
| 133 | print(f"DEBUG: Executing composite command with {len(self.commands)} commands") |
| 134 | |
| 135 | self.executed_commands = [] |
| 136 | |
| 137 | for i, command in enumerate(self.commands): |
| 138 | print(f"DEBUG: Executing command {i+1}/{len(self.commands)}: {command.get_description()}") |
| 139 | result = command.execute() |
| 140 | print(f"DEBUG: Command {i+1} returned: {result}") |
| 141 | |
| 142 | if result: |
| 143 | command._mark_executed() |
| 144 | self.executed_commands.append(command) |
| 145 | print(f"DEBUG: Command {i+1} succeeded, added to executed list") |
| 146 | else: |
| 147 | print(f"DEBUG: Command {i+1} FAILED - rolling back {len(self.executed_commands)} executed commands") |
| 148 | # Rollback executed commands on failure |
| 149 | for j, executed in enumerate(reversed(self.executed_commands)): |
| 150 | print(f"DEBUG: Rolling back command {j+1}/{len(self.executed_commands)}: {executed.get_description()}") |
| 151 | rollback_result = executed.undo() |
| 152 | print(f"DEBUG: Rollback {j+1} returned: {rollback_result}") |
| 153 | executed._mark_undone() |
| 154 | print(f"DEBUG: Rollback complete, composite command failed") |
| 155 | print(f"=== COMPOSITE COMMAND EXECUTE END (FAILED) ===\n") |
| 156 | return False |
| 157 | |
| 158 | self._mark_executed() |
| 159 | print(f"DEBUG: All {len(self.commands)} commands succeeded") |
| 160 | print(f"=== COMPOSITE COMMAND EXECUTE END (SUCCESS) ===\n") |
| 161 | return True |
| 162 | |
| 163 | def undo(self) -> bool: |
| 164 | """Undo all executed commands in reverse order.""" |