Skip to content
This repository was archived by the owner on Jul 28, 2020. It is now read-only.

Commit 3e5a57f

Browse files
authored
Merge pull request #46 from zenaton/retry-context
Add runtime context
2 parents 2dc7378 + 5367784 commit 3e5a57f

File tree

8 files changed

+103
-13
lines changed

8 files changed

+103
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
# Changelog
22

3-
## [Unreleased]
3+
## [0.4.1] - 2019-09-25
44

55
### Added
6-
7-
### Changed
8-
9-
### Deprecated
10-
11-
### Removed
12-
13-
### Fixed
14-
15-
### Security
6+
- Added a `intent_id` property when dispatching workflows and tasks, sending events to workflows, and
7+
pausing/resuming/killing workflows.
8+
- Execution context for tasks and workflows
9+
- Optional `on_error_retry_delay` method handling task failures and specifying
10+
how many seconds to wait before retrying.
1611

1712
## [0.4.0] - 2019-08-26
1813

zenaton/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '0.4.0'
2-
__version_id__ = 304
1+
__version__ = '0.4.1'
2+
__version_id__ = 401

zenaton/abstracts/task.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
import abc
22

33
from .job import Job
4+
from ..contexts.task_context import TaskContext
45

56

67
class Task(Job):
78

89
@abc.abstractmethod
910
def handle(self):
1011
pass
12+
13+
"""
14+
(Optional) Implement this method for automatic retrial of task in case of failures.
15+
16+
:params error Error
17+
:raises Exception when the return type is not falsy or is not positive integer.
18+
:return [int, false, None] the amount of seconds to wait
19+
before automatically retrying this task. Falsy values will avoid
20+
retrial. Other values will raise.
21+
"""
22+
def on_error_retry_delay(self, error):
23+
None
24+
25+
"""
26+
:return TaskContext
27+
"""
28+
def get_context(self):
29+
return self._context or TaskContext()
30+
31+
"""
32+
Sets a new context if none has been set yet.
33+
This is called from the zenaton agent and will raise if called twice.
34+
35+
:params context TaskContext
36+
:raises Exception when the context was already set.
37+
"""
38+
def set_context(self, context):
39+
if hasattr(self, '_context') and self._context != None:
40+
raise Exception('Context is already set and cannot be mutated.')
41+
42+
self._context = context

zenaton/abstracts/workflow.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .job import Job
44

5+
from ..contexts.workflow_context import WorkflowContext
56

67
class Workflow(Job):
78

@@ -11,3 +12,23 @@ def handle(self):
1112

1213
def id(self):
1314
return None
15+
16+
17+
"""
18+
:return TaskContext
19+
"""
20+
def get_context(self):
21+
return self._context or WorkflowContext()
22+
23+
"""
24+
Sets a new context if none has been set yet.
25+
This is called from the zenaton agent and will raise if called twice.
26+
27+
:context WorkflowContext
28+
:raises Exception when the context was already set.
29+
"""
30+
def set_context(self, context):
31+
if hasattr(self, '_context') and self._context != None:
32+
raise Exception('Context is already set and cannot be mutated.')
33+
34+
self._context = context

zenaton/contexts/__init__.py

Whitespace-only changes.

zenaton/contexts/task_context.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class TaskContext():
2+
"""
3+
Represents the current runtime context of a Task.
4+
5+
The information provided by the context can be useful to alter the
6+
behaviour of the task.
7+
8+
For example, you can use the retry_index to know if a task has been
9+
automatically retried or not and how many times, and decide to do
10+
something when you did not expect the task to be retried more than X
11+
times.
12+
13+
You can also use the retry_index in the `on_error_retry_delay` method
14+
of a task in order to implement complex retry strategies.
15+
16+
Attributes
17+
----------
18+
id : str
19+
The UUID identifying the current task.
20+
21+
retry_index : int
22+
The number of times this task has been automatically retried.
23+
This counter is reset if you issue a manual retry from your dashboard
24+
"""
25+
def __init__(self, **kwargs):
26+
self.id = kwargs.get('id', None)
27+
self.retry_index = kwargs.get('retry_index', None)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class WorkflowContext():
2+
"""
3+
Represents the current runtime context of a Workflow.
4+
5+
Attributes
6+
----------
7+
id : str
8+
The UUID identifying the current workflow
9+
"""
10+
def __init__(self, **kwargs):
11+
self.id = kwargs.get('id', None)

zenaton/services/serializer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def encode(self, data):
2929
self._encoded = []
3030
self._decoded = []
3131
value = dict()
32+
33+
if isinstance(data, dict) and data.get('_context'):
34+
del data['_context']
35+
3236
if self.__is_basic_type(data):
3337
value[self.KEY_DATA] = data
3438
else:

0 commit comments

Comments
 (0)