Skip to content

Commit ac2d8f3

Browse files
authored
Merge pull request #408 from sartography/feature/align-callbacks-with-state-transitions
rename events & run in hook functions
2 parents 651ed6c + 3478d5e commit ac2d8f3

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

SpiffWorkflow/specs/AcquireMutex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _update_hook(self, my_task):
5252
super()._update_hook(my_task)
5353
mutex = my_task.workflow._get_mutex(self.mutex)
5454
if mutex.testandset():
55-
self.entered_event.emit(my_task.workflow, my_task)
55+
self.update_event.emit(my_task.workflow, my_task)
5656
return True
5757
else:
5858
my_task._set_state(TaskState.WAITING)

SpiffWorkflow/specs/ThreadMerge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _update_hook(self, my_task):
120120
# completed, except for the first one, which should be READY.
121121
for task in tasks:
122122
if task == last_changed:
123-
self.entered_event.emit(my_task.workflow, my_task)
123+
self.update_event.emit(my_task.workflow, my_task)
124124
task._ready()
125125
else:
126126
task._set_state(TaskState.COMPLETED)

SpiffWorkflow/specs/base.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ def __init__(self, wf_spec, name, **kwargs):
100100
self.lookahead = 2 # Maximum number of MAYBE predictions.
101101

102102
# Events.
103-
self.entered_event = Event()
104-
self.reached_event = Event()
103+
self.update_event = Event()
105104
self.ready_event = Event()
106105
self.completed_event = Event()
106+
self.error_event = Event()
107107
self.cancelled_event = Event()
108-
self.finished_event = Event()
108+
self.run_event = Event()
109109

110110
self._wf_spec._add_notify(self)
111111
self.data.update(self.defines)
@@ -257,7 +257,6 @@ def _update(self, my_task):
257257
"""
258258
if my_task.has_state(TaskState.PREDICTED_MASK):
259259
self._predict(my_task)
260-
self.entered_event.emit(my_task.workflow, my_task)
261260
if self._update_hook(my_task):
262261
my_task._ready()
263262

@@ -268,6 +267,7 @@ def _update_hook(self, my_task):
268267
Returning True will cause the task to go into READY.
269268
"""
270269
my_task._inherit_data()
270+
self.update_event.emit(my_task.workflow, my_task)
271271
return True
272272

273273
def _on_ready(self, my_task):
@@ -285,7 +285,6 @@ def _on_ready(self, my_task):
285285

286286
# Run task-specific code.
287287
self._on_ready_hook(my_task)
288-
self.reached_event.emit(my_task.workflow, my_task)
289288

290289
def _on_ready_hook(self, my_task):
291290
"""
@@ -294,7 +293,7 @@ def _on_ready_hook(self, my_task):
294293
:type my_task: Task
295294
:param my_task: The associated task in the task tree.
296295
"""
297-
pass
296+
self.ready_event.emit(my_task.workflow, my_task)
298297

299298
def _run(self, my_task):
300299
"""
@@ -314,15 +313,11 @@ def _run(self, my_task):
314313
try:
315314
result = self._run_hook(my_task)
316315
# Run user code, if any.
317-
if self.ready_event.emit(my_task.workflow, my_task):
318-
# Assign variables, if so requested.
319-
for assignment in self.post_assign:
320-
assignment.assign(my_task, my_task)
321-
322-
self.finished_event.emit(my_task.workflow, my_task)
316+
for assignment in self.post_assign:
317+
assignment.assign(my_task, my_task)
323318
return result
324319
except Exception as exc:
325-
my_task._set_state(TaskState.ERROR)
320+
my_task.error()
326321
raise exc
327322

328323
def _run_hook(self, my_task):
@@ -332,6 +327,7 @@ def _run_hook(self, my_task):
332327
:type my_task: Task
333328
:param my_task: The associated task in the task tree.
334329
"""
330+
self.run_event.emit(my_task.workflow, my_task)
335331
return True
336332

337333
def _on_cancel(self, my_task):
@@ -371,7 +367,6 @@ def _on_complete(self, my_task):
371367
if not child.has_state(TaskState.FINISHED_MASK):
372368
child.task_spec._update(child)
373369
my_task.workflow._task_completed_notify(my_task)
374-
self.completed_event.emit(my_task.workflow, my_task)
375370

376371
def _on_complete_hook(self, my_task):
377372
"""
@@ -382,14 +377,14 @@ def _on_complete_hook(self, my_task):
382377
:rtype: bool
383378
:returns: True on success, False otherwise.
384379
"""
385-
pass
380+
self.completed_event.emit(my_task.workflow, my_task)
386381

387382
def _on_error(self, my_task):
388383
self._on_error_hook(my_task)
389384

390385
def _on_error_hook(self, my_task):
391386
"""Can be overridden for task specific error handling"""
392-
pass
387+
self.error_event.emit(my_task.workflow, my_task)
393388

394389
@abstractmethod
395390
def serialize(self, serializer, **kwargs):

tests/SpiffWorkflow/bpmn/events/CallActivityEscalationTest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__author__ = '[email protected]'
99

1010

11-
def on_reached_cb(workflow, task, completed_set):
11+
def on_ready_cb(workflow, task, completed_set):
1212
# In workflows that load a subworkflow, the newly loaded children
1313
# will not have on_reached_cb() assigned. By using this function, we
1414
# re-assign the function in every step, thus making sure that new
@@ -24,9 +24,9 @@ def on_complete_cb(workflow, task, completed_set):
2424

2525

2626
def track_task(task_spec, completed_set):
27-
if task_spec.reached_event.is_connected(on_reached_cb):
28-
task_spec.reached_event.disconnect(on_reached_cb)
29-
task_spec.reached_event.connect(on_reached_cb, completed_set)
27+
if task_spec.ready_event.is_connected(on_ready_cb):
28+
task_spec.ready_event.disconnect(on_ready_cb)
29+
task_spec.ready_event.connect(on_ready_cb, completed_set)
3030
if task_spec.completed_event.is_connected(on_complete_cb):
3131
task_spec.completed_event.disconnect(on_complete_cb)
3232
task_spec.completed_event.connect(on_complete_cb, completed_set)

tests/SpiffWorkflow/core/util.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from SpiffWorkflow.specs import SubWorkflow
55

66

7-
def on_reached_cb(workflow, task, taken_path):
7+
def on_ready_cb(workflow, task, taken_path):
88
reached_key = "%s_reached" % str(task.task_spec.name)
99
n_reached = task.get_data(reached_key, 0) + 1
1010
task.set_data(**{reached_key: n_reached,
@@ -44,31 +44,31 @@ def on_complete_cb(workflow, task, taken_path):
4444
indent = ' ' * task.depth
4545
taken_path.append('%s%s' % (indent, task.task_spec.name))
4646
# In workflows that load a subworkflow, the newly loaded children
47-
# will not have on_reached_cb() assigned. By using this function, we
47+
# will not have on_ready_cb() assigned. By using this function, we
4848
# re-assign the function in every step, thus making sure that new
49-
# children also call on_reached_cb().
49+
# children also call on_ready_cb().
5050
for child in task.children:
5151
track_task(child.task_spec, taken_path)
5252
return True
5353

54-
def on_entered_cb(workflow, task, taken_path):
54+
def on_update_cb(workflow, task, taken_path):
5555
for child in task.children:
5656
track_task(child.task_spec, taken_path)
5757
return True
5858

5959
def track_task(task_spec, taken_path):
6060
# Disconnecting and reconnecting makes absolutely no sense but inexplicably these tests break
6161
# if just connected based on a check that they're not
62-
if task_spec.reached_event.is_connected(on_reached_cb):
63-
task_spec.reached_event.disconnect(on_reached_cb)
64-
task_spec.reached_event.connect(on_reached_cb, taken_path)
62+
if task_spec.ready_event.is_connected(on_ready_cb):
63+
task_spec.ready_event.disconnect(on_ready_cb)
64+
task_spec.ready_event.connect(on_ready_cb, taken_path)
6565
if task_spec.completed_event.is_connected(on_complete_cb):
6666
task_spec.completed_event.disconnect(on_complete_cb)
6767
task_spec.completed_event.connect(on_complete_cb, taken_path)
6868
if isinstance(task_spec, SubWorkflow):
69-
if task_spec.entered_event.is_connected(on_entered_cb):
70-
task_spec.entered_event.disconnect(on_entered_cb)
71-
task_spec.entered_event.connect(on_entered_cb, taken_path)
69+
if task_spec.update_event.is_connected(on_update_cb):
70+
task_spec.update_event.disconnect(on_update_cb)
71+
task_spec.update_event.connect(on_update_cb, taken_path)
7272

7373
def track_workflow(wf_spec, taken_path=None):
7474
if taken_path is None:

0 commit comments

Comments
 (0)