Skip to content

Commit ddfafb5

Browse files
committed
Catch InterruptExecption during rollbacks
Closes #132 Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent ffb4353 commit ddfafb5

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.7.0...HEAD
66

7+
### Added
8+
9+
- Catch `InterruptExecution` during rollbacks so that the experiment terminates
10+
gracefully [#132][132]
11+
12+
[132]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/132
13+
714
## [1.7.0][] - 2019-09-21
815

916
[1.7.0]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.6.0...1.7.0

chaoslib/experiment.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ def run_experiment(experiment: Experiment,
248248
"leaving without applying rollbacks.")
249249
else:
250250
journal["status"] = journal["status"] or "completed"
251-
journal["rollbacks"] = apply_rollbacks(
252-
experiment, config, secrets, rollback_pool, dry)
251+
try:
252+
journal["rollbacks"] = apply_rollbacks(
253+
experiment, config, secrets, rollback_pool, dry)
254+
except InterruptExecution as i:
255+
journal["status"] = "interrupted"
256+
logger.fatal(str(i))
253257

254258
journal["end"] = datetime.utcnow().isoformat()
255259
journal["duration"] = time.time() - started_at

tests/fixtures/experiments.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@
8989
]
9090
}
9191

92+
ExperimentWithRollbackLongPause = {
93+
"title": "do cats live in the Internet?",
94+
"description": "an experiment of importance",
95+
"steady-state-hypothesis": {
96+
"title": "hello"
97+
},
98+
"method": [
99+
PythonModuleProbe
100+
],
101+
"rollbacks": [
102+
PythonModuleProbeWithLongPause
103+
]
104+
}
105+
92106
ExperimentWithLongPauseBefore = deepcopy(ExperimentWithLongPause)
93107
ExperimentWithLongPauseBefore["method"][1] = BackgroundPythonModuleProbeWithLongPauseBefore
94108

tests/test_experiment.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import yaml
1313

1414
from chaoslib.exceptions import ActivityFailed, InvalidActivity, \
15-
InvalidExperiment
15+
InvalidExperiment, InterruptExecution
1616
from chaoslib.experiment import ensure_experiment_is_valid, load_experiment, \
1717
run_experiment, run_activities
1818
from chaoslib.types import Experiment
@@ -172,6 +172,21 @@ def handler(signum, frame):
172172
pytest.fail("we should have swalled the SystemExit exception")
173173

174174

175+
def test_can_interrupt_rollbacks():
176+
def handler(signum, frame):
177+
raise InterruptExecution("boom")
178+
179+
signal.signal(signal.SIGALRM, handler)
180+
signal.alarm(1)
181+
182+
try:
183+
journal = run_experiment(experiments.ExperimentWithRollbackLongPause)
184+
assert isinstance(journal, dict)
185+
assert journal["status"] == "interrupted"
186+
except Exception:
187+
pytest.fail("we should have swalled the InterruptExecution exception")
188+
189+
175190
def test_probes_can_reference_each_other():
176191
experiment = experiments.RefProbeExperiment.copy()
177192
experiment["dry"] = True

0 commit comments

Comments
 (0)