1
|
|
|
from __future__ import ( |
|
|
|
|
2
|
|
|
absolute_import, |
3
|
|
|
division, |
4
|
|
|
print_function |
5
|
|
|
) |
6
|
|
|
|
7
|
|
|
import json |
8
|
|
|
import logging |
9
|
|
|
|
10
|
|
|
from .schema import ValidationError |
11
|
|
|
from .state import State |
12
|
|
|
from .step_results import ( |
13
|
|
|
ActivityStepResult, |
14
|
|
|
TemplatedStepResult, |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
_LOGGER = logging.getLogger(__name__) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class StateMachine(object): |
|
|
|
|
21
|
|
|
|
22
|
|
|
__slots__ = ('plan', 'state', '_event_ids') |
23
|
|
|
|
24
|
|
|
def __init__(self, plan): |
25
|
|
|
self.plan = plan |
26
|
|
|
self.state = None |
27
|
|
|
self._event_ids = {} |
28
|
|
|
|
29
|
|
|
########################################################################### |
30
|
|
|
# Accessors |
31
|
|
|
@property |
32
|
|
|
def is_failed(self): |
|
|
|
|
33
|
|
|
return self.state.is_in_state('failed') |
34
|
|
|
|
35
|
|
|
@property |
36
|
|
|
def is_succeeded(self): |
|
|
|
|
37
|
|
|
return self.state.is_in_state('succeeded') |
38
|
|
|
|
39
|
|
|
########################################################################### |
40
|
|
|
def eval(self, events): |
|
|
|
|
41
|
|
|
# First clear the state |
42
|
|
|
self.state = State() |
43
|
|
|
self._event_ids.clear() |
44
|
|
|
|
45
|
|
|
# Inject a load plan event |
46
|
|
|
self._run_event({'eventId': 0, 'eventType': 'PlanLoad'}) |
47
|
|
|
|
48
|
|
|
# Then, replay the state from the events |
49
|
|
|
for event in events: |
50
|
|
|
results = self._run_event(event) |
51
|
|
|
|
52
|
|
|
_LOGGER.debug('State replayed from events: %r', self.state) |
53
|
|
|
return results |
54
|
|
|
|
55
|
|
|
########################################################################### |
56
|
|
|
def _run_event(self, event): |
57
|
|
|
"""Process a given event and return a list of results. |
58
|
|
|
""" |
59
|
|
|
_LOGGER.debug('Processing event %r', event['eventType']) |
60
|
|
|
|
61
|
|
|
handler_fun = getattr( |
62
|
|
|
self, |
63
|
|
|
'EVENT_%s' % event['eventType'], |
64
|
|
|
self.__ev_abort |
65
|
|
|
) |
66
|
|
|
handler_fun(event) |
67
|
|
|
|
68
|
|
|
# If there is nothing left to do, stop here |
69
|
|
|
if self.state.is_in_state('completed'): |
70
|
|
|
return [] |
71
|
|
|
|
72
|
|
|
_LOGGER.debug('Running all "ready" steps') |
73
|
|
|
ready_steps = self.state.step_next() |
74
|
|
|
_LOGGER.debug('Next steps: %r', ready_steps) |
75
|
|
|
|
76
|
|
|
results = [] |
77
|
|
|
for step in ready_steps: |
78
|
|
|
step_result = step.run() |
79
|
|
|
_LOGGER.debug('step_result: %r', step_result) |
80
|
|
|
if isinstance(step_result, ActivityStepResult): |
81
|
|
|
results.append(step_result) |
82
|
|
|
|
83
|
|
|
elif isinstance(step_result, TemplatedStepResult): |
84
|
|
|
raise Exception('Not implemented') |
85
|
|
|
|
86
|
|
|
_LOGGER.debug('Results: %r', results) |
87
|
|
|
return results |
88
|
|
|
|
89
|
|
|
########################################################################### |
90
|
|
|
# Events and handlers |
91
|
|
|
def __ev_load(self, event): |
|
|
|
|
92
|
|
|
# Create loading time steps from the plan |
93
|
|
|
with self.state(event['eventId']): |
94
|
|
|
# Import all predefined steps from the plan |
95
|
|
|
for step in self.plan.steps: |
96
|
|
|
self.state.step_insert(step) |
97
|
|
|
|
98
|
|
|
def __ev_skip(self, event): |
|
|
|
|
99
|
|
|
_LOGGER.info('Skipping event: %r', event['eventType']) |
100
|
|
|
|
101
|
|
|
def __ev_abort(self, event): |
|
|
|
|
102
|
|
|
_LOGGER.error('Unknown event: %r', event) |
103
|
|
|
with self.state(event['eventId']): |
104
|
|
|
# Set the input |
105
|
|
|
self.state.set_abort() |
106
|
|
|
|
107
|
|
|
def __ev_start(self, event): |
108
|
|
|
"""Import input data""" |
109
|
|
|
_LOGGER.debug('%r', event) |
110
|
|
|
start_attrs = event['workflowExecutionStartedEventAttributes'] |
111
|
|
|
# Note that if no input was provided, the 'input' key will not be there |
112
|
|
|
wf_input = start_attrs.get('input', 'null') |
113
|
|
|
try: |
114
|
|
|
input_data = json.loads(wf_input) |
115
|
|
|
self.plan.check_input(input_data) |
116
|
|
|
|
117
|
|
|
except (ValueError, ValidationError): |
118
|
|
|
_LOGGER.exception('Invalid workflow input: %r', wf_input) |
119
|
|
|
# We cannot do anything, just abort |
120
|
|
|
with self.state(event['eventId']): |
121
|
|
|
self.state.set_abort() |
122
|
|
|
return |
123
|
|
|
|
124
|
|
|
with self.state(event['eventId']): |
125
|
|
|
# Set the input |
126
|
|
|
self.state.set_input(input_data) |
127
|
|
|
|
128
|
|
|
def __ev_scheduled(self, event): |
129
|
|
|
"""Record the eventId associated with activities we scheduled.""" |
130
|
|
|
_LOGGER.debug('%r', event) |
131
|
|
|
step_name = event['activityTaskScheduledEventAttributes']['activityId'] |
132
|
|
|
event_id = event['eventId'] |
133
|
|
|
|
134
|
|
|
with self.state(event['eventId']): |
135
|
|
|
self.state.step_update(step_name, 'running') |
136
|
|
|
|
137
|
|
|
_LOGGER.debug('Associating step %r with event_id %r', |
138
|
|
|
step_name, event_id) |
139
|
|
|
self._event_ids[event_id] = step_name |
140
|
|
|
|
141
|
|
|
def __ev_completed(self, event): |
|
|
|
|
142
|
|
|
_LOGGER.debug('%r', event) |
143
|
|
|
completed_event = event['activityTaskCompletedEventAttributes'] |
144
|
|
|
output_json = completed_event.get('result', 'null') |
145
|
|
|
output = json.loads(output_json) |
146
|
|
|
sched_event_id = completed_event['scheduledEventId'] |
147
|
|
|
step_name = self._event_ids[sched_event_id] |
148
|
|
|
with self.state(event['eventId']): |
149
|
|
|
self.state.step_update(step_name, 'succeeded', output) |
150
|
|
|
|
151
|
|
|
EVENT_PlanLoad = __ev_load |
152
|
|
|
EVENT_WorkflowExecutionStarted = __ev_start |
153
|
|
|
EVENT_DecisionTaskScheduled = __ev_skip |
154
|
|
|
EVENT_DecisionTaskStarted = __ev_skip |
155
|
|
|
EVENT_DecisionTaskCompleted = __ev_skip |
156
|
|
|
EVENT_DecisionTaskTimedOut = __ev_skip |
157
|
|
|
EVENT_ActivityTaskScheduled = __ev_scheduled |
158
|
|
|
EVENT_ActivityTaskStarted = __ev_skip |
159
|
|
|
EVENT_ActivityTaskCompleted = __ev_completed |
160
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.