Add SWF endpoint PollForDecisionTask and associated DecisionTask model

This commit is contained in:
Jean-Baptiste Barth 2015-10-11 14:02:55 +02:00
commit c16da9da2d
7 changed files with 213 additions and 3 deletions

View file

@ -13,6 +13,7 @@ from ..exceptions import (
SWFTypeDeprecatedFault,
)
from .activity_type import ActivityType
from .decision_task import DecisionTask
from .domain import Domain
from .generic_type import GenericType
from .history_event import HistoryEvent
@ -162,6 +163,36 @@ class SWFBackend(BaseBackend):
domain = self._get_domain(domain_name)
return domain.get_workflow_execution(run_id, workflow_id)
def poll_for_decision_task(self, domain_name, task_list, identity=None):
self._check_string(domain_name)
self._check_string(task_list)
domain = self._get_domain(domain_name)
# Real SWF cases:
# - case 1: there's a decision task to return, return it
# - case 2: there's no decision task to return, so wait for timeout
# and if a new decision is schedule, start and return it
# - case 3: timeout reached, no decision, return an empty decision
# (e.g. a decision with an empty "taskToken")
#
# For the sake of simplicity, we forget case 2 for now, so either
# there's a DecisionTask to return, either we return a blank one.
#
# SWF client libraries should cope with that easily as long as tests
# aren't distributed.
#
# TODO: handle long polling (case 2) for decision tasks
decision_candidates = []
for wf_id, wf_execution in domain.workflow_executions.iteritems():
decision_candidates += wf_execution.scheduled_decision_tasks
if any(decision_candidates):
# TODO: handle task priorities (but not supported by boto for now)
decision = min(decision_candidates, key=lambda d: d.scheduled_at)
wfe = decision.workflow_execution
wfe.start_decision_task(decision.task_token, identity=identity)
return decision
else:
return None
swf_backends = {}
for region in boto.swf.regions():

View file

@ -0,0 +1,35 @@
from __future__ import unicode_literals
from datetime import datetime
import uuid
class DecisionTask(object):
def __init__(self, workflow_execution, scheduled_event_id):
self.workflow_execution = workflow_execution
self.workflow_type = workflow_execution.workflow_type
self.task_token = str(uuid.uuid4())
self.scheduled_event_id = scheduled_event_id
self.previous_started_event_id = 0
self.started_event_id = None
self.state = "SCHEDULED"
# this is *not* necessarily coherent with workflow execution history,
# but that shouldn't be a problem for tests
self.scheduled_at = datetime.now()
def to_full_dict(self):
hsh = {
"events": [
evt.to_dict() for evt in self.workflow_execution.events
],
"taskToken": self.task_token,
"previousStartedEventId": self.previous_started_event_id,
"workflowExecution": self.workflow_execution.to_short_dict(),
"workflowType": self.workflow_type.to_short_dict(),
}
if self.started_event_id:
hsh["startedEventId"] = self.started_event_id
return hsh
def start(self, started_event_id):
self.state = "STARTED"
self.started_event_id = started_event_id

View file

@ -50,9 +50,12 @@ class HistoryEvent(object):
"taskList": {"name": wfe.task_list}
}
elif self.event_type == "DecisionTaskStarted":
return {
hsh = {
"scheduledEventId": self.scheduled_event_id
}
if hasattr(self, "identity") and self.identity:
hsh["identity"] = self.identity
return hsh
else:
raise NotImplementedError(
"HistoryEvent does not implement attributes for type '{}'".format(self.event_type)

View file

@ -4,6 +4,7 @@ import uuid
from moto.core.utils import camelcase_to_underscores
from ..exceptions import SWFDefaultUndefinedFault
from .decision_task import DecisionTask
from .history_event import HistoryEvent
@ -32,6 +33,10 @@ class WorkflowExecution(object):
}
# events
self.events = []
# tasks
self.decision_tasks = []
self.activity_tasks = []
self.child_workflow_executions = []
def __repr__(self):
return "WorkflowExecution(run_id: {})".format(self.run_id)
@ -99,13 +104,44 @@ class WorkflowExecution(object):
def _add_event(self, *args, **kwargs):
evt = HistoryEvent(self.next_event_id(), *args, **kwargs)
self.events.append(evt)
return evt
def start(self):
self._add_event(
"WorkflowExecutionStarted",
workflow_execution=self,
)
self._add_event(
self.schedule_decision_task()
def schedule_decision_task(self):
self.open_counts["openDecisionTasks"] += 1
evt = self._add_event(
"DecisionTaskScheduled",
workflow_execution=self,
)
self.decision_tasks.append(DecisionTask(self, evt.event_id))
@property
def scheduled_decision_tasks(self):
return filter(
lambda t: t.state == "SCHEDULED",
self.decision_tasks
)
def _find_decision_task(self, task_token):
for dt in self.decision_tasks:
if dt.task_token == task_token:
return dt
raise ValueError(
"No decision task with token: {}".format(task_token)
)
def start_decision_task(self, task_token, identity=None):
dt = self._find_decision_task(task_token)
evt = self._add_event(
"DecisionTaskStarted",
workflow_execution=self,
scheduled_event_id=dt.scheduled_event_id,
identity=identity
)
dt.start(evt.event_id)