Skip to content

Commit f2908ff

Browse files
committed
SWF: add Lambda function support
Signed-off-by: Yves Bastide <yves@botify.com>
1 parent 38668f5 commit f2908ff

File tree

8 files changed

+114
-19
lines changed

8 files changed

+114
-19
lines changed

swf/models/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#
66
# See the file LICENSE for copying permission.
77

8-
from swf.models.base import BaseModel # NOQA
9-
from swf.models.activity import ActivityType, ActivityTask # NOQA
10-
from swf.models.domain import Domain # NOQA
11-
from swf.models.workflow import WorkflowType, WorkflowExecution # NOQA
12-
from swf.models.history import History # NOQA
8+
from .base import BaseModel # NOQA
9+
from .activity import ActivityType, ActivityTask # NOQA
10+
from .domain import Domain # NOQA
11+
from .workflow import WorkflowType, WorkflowExecution # NOQA
12+
from .history import History # NOQA

swf/models/decision/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#
66
# See the file LICENSE for copying permission.
77

8-
from swf.models.decision.marker import MarkerDecision # NOQA
9-
from swf.models.decision.task import ActivityTaskDecision # NOQA
10-
from swf.models.decision.timer import TimerDecision # NOQA
11-
from swf.models.decision.workflow import ( # NOQA
8+
from .lambda_function import LambdaFunctionDecision # NOQA
9+
from .marker import MarkerDecision # NOQA
10+
from .task import ActivityTaskDecision # NOQA
11+
from .timer import TimerDecision # NOQA
12+
from .workflow import ( # NOQA
1213
WorkflowExecutionDecision,
1314
ChildWorkflowExecutionDecision,
1415
ExternalWorkflowExecutionDecision,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding:utf-8 -*-
2+
3+
# Copyright (c) 2016, Botify
4+
#
5+
# See the file LICENSE for copying permission.
6+
7+
from simpleflow.utils import json_dumps
8+
from swf.models.decision.base import Decision, decision_action
9+
10+
11+
class LambdaFunctionDecision(Decision):
12+
_base_type = 'LambdaFunction'
13+
14+
@decision_action
15+
def schedule(self, id, name, input=None, start_to_close_timeout=None):
16+
"""Schedule lambda function decision builder
17+
18+
:param id: id of the Lambda function
19+
:type id: str
20+
21+
:param name: name of the Lambda function to schedule
22+
:type name: str
23+
24+
:param input: input provided to the activity task
25+
:type input: Optional[dict]
26+
27+
:param start_to_close_timeout: timeout, 1-300 seconds. Default: 300
28+
:type start_to_close_timeout: Optional[str]
29+
"""
30+
if input is not None:
31+
input = json_dumps(input)
32+
33+
self.update_attributes({
34+
'id': id,
35+
'name': name,
36+
'input': input,
37+
'startToCloseTimeout': start_to_close_timeout,
38+
})

swf/models/event/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#
66
# See the file LICENSE for copying permission.
77

8-
from swf.models.event.base import Event # NOQA
9-
from swf.models.event.compiler import CompiledEvent # NOQA
10-
from swf.models.event.factory import EventFactory, CompiledEventFactory # NOQA
11-
from swf.models.event.task import DecisionTaskEvent, ActivityTaskEvent # NOQA
12-
from swf.models.event.workflow import WorkflowExecutionEvent # NOQA
13-
from swf.models.event.marker import MarkerEvent # NOQA
14-
from swf.models.event.timer import TimerEvent # NOQA
8+
from .base import Event # NOQA
9+
from .compiler import CompiledEvent # NOQA
10+
from .factory import EventFactory, CompiledEventFactory # NOQA
11+
from .task import DecisionTaskEvent, ActivityTaskEvent # NOQA
12+
from .workflow import WorkflowExecutionEvent # NOQA
13+
from .lambda_function import LambdaFunctionEvent # NOQA
14+
from .marker import MarkerEvent # NOQA
15+
from .timer import TimerEvent # NOQA

swf/models/event/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Event(object):
4747
_name = None
4848
_attributes_key = None
4949
_attributes = None
50+
_attribute_mapping = {} # dict for remapping Attributes items colliding with id, name...
5051

5152
excluded_attributes = (
5253
'eventId',
@@ -100,4 +101,6 @@ def process_attributes(self):
100101
"""Processes the event raw_data attributes_key elements
101102
and sets current instance attributes accordingly"""
102103
for key, value in iteritems(self.raw[self._attributes_key]):
103-
setattr(self, camel_to_underscore(key), value)
104+
name = camel_to_underscore(key)
105+
name = self._attribute_mapping.get(name) or name
106+
setattr(self, name, value)

swf/models/event/factory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
CompiledMarkerEvent
3434
)
3535

36+
from swf.models.event.lambda_function import (
37+
LambdaFunctionEvent,
38+
CompiledLambdaFunctionEvent
39+
)
40+
3641
from swf.utils import camel_to_underscore, decapitalize
3742

3843

@@ -66,6 +71,10 @@
6671
'event': TimerEvent,
6772
'compiled': CompiledTimerEvent,
6873
}),
74+
('LambdaFunction', {
75+
'event': LambdaFunctionEvent,
76+
'compiled': CompiledLambdaFunctionEvent,
77+
}),
6978
])
7079

7180

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding:utf-8 -*-
2+
3+
# Copyright (c) 2016, Botify
4+
#
5+
# See the file LICENSE for copying permission.
6+
7+
from swf.models.event.base import Event
8+
from swf.models.event.compiler import CompiledEvent
9+
10+
11+
class LambdaFunctionEvent(Event):
12+
_type = 'LambdaFunction'
13+
_attribute_mapping = {
14+
'id': 'lambda_id',
15+
'name': 'lambda_name',
16+
}
17+
18+
19+
class CompiledLambdaFunctionEvent(CompiledEvent):
20+
_type = 'LambdaFunction'
21+
states = (
22+
'scheduled',
23+
'schedule_failed',
24+
'start_failed',
25+
'started',
26+
'completed',
27+
'failed',
28+
'timed_out',
29+
)
30+
31+
transitions = {
32+
'scheduled': ('schedule_failed', 'timed_out', 'start_failed', 'started'),
33+
'schedule_failed': ('scheduled', 'timed_out'),
34+
'started': ('failed', 'timed_out', 'completed'),
35+
'failed': ('scheduled', 'timed_out'),
36+
'timed_out': ('scheduled'),
37+
}
38+
39+
initial_state = 'scheduled'

swf/models/workflow.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _diff(self):
164164
('task_list', self.task_list, workflow_config['defaultTaskList']['name']),
165165
('child_policy', self.child_policy, workflow_config['defaultChildPolicy']),
166166
('execution_timeout', self.execution_timeout, workflow_config['defaultExecutionStartToCloseTimeout']),
167-
('decision_tasks_timout', self.decision_tasks_timeout, workflow_config['defaultTaskStartToCloseTimeout']),
167+
('decision_tasks_timeout', self.decision_tasks_timeout, workflow_config['defaultTaskStartToCloseTimeout']),
168168
('description', self.description, workflow_info['description']),
169169
)
170170

@@ -341,6 +341,7 @@ class WorkflowExecution(BaseModel):
341341
'close_status',
342342
'execution_timeout',
343343
'input',
344+
'lambda_role',
344345
'tag_list',
345346
'decision_tasks_timeout',
346347
'close_timestamp',
@@ -365,6 +366,7 @@ def __init__(self, domain, workflow_id, run_id=None,
365366
latest_activity_task_timestamp=None,
366367
open_counts=None,
367368
parent=None,
369+
lambda_role=None,
368370
*args, **kwargs):
369371
Domain.check(domain)
370372
self.domain = domain
@@ -377,6 +379,7 @@ def __init__(self, domain, workflow_id, run_id=None,
377379
self.close_status = close_status
378380
self.execution_timeout = execution_timeout
379381
self.input = input
382+
self.lambda_role = lambda_role
380383
self.tag_list = tag_list or []
381384
self.decision_tasks_timeout = decision_tasks_timeout
382385
self.close_timestamp = close_timestamp
@@ -388,7 +391,7 @@ def __init__(self, domain, workflow_id, run_id=None,
388391
self.parent = parent or {} # so we can query keys in any case
389392

390393
# immutable decorator rebinds class name,
391-
# so have to use generice self.__class__
394+
# so have to use generic self.__class__
392395
super(self.__class__, self).__init__(*args, **kwargs)
393396

394397
def _diff(self):
@@ -420,6 +423,7 @@ def _diff(self):
420423
('status', self.status, execution_info['executionStatus']),
421424
('task_list', self.task_list, execution_config['taskList']['name']),
422425
('child_policy', self.child_policy, execution_config['childPolicy']),
426+
('lambda_role', self.lambda_role, execution_config['lambdaRole']),
423427
('execution_timeout', self.execution_timeout, execution_config['executionStartToCloseTimeout']),
424428
('tag_list', self.tag_list, execution_info.get('tagList')),
425429
('decision_tasks_timeout', self.decision_tasks_timeout, execution_config['taskStartToCloseTimeout']),

0 commit comments

Comments
 (0)