是的。实际上,我认为您提出了一个有趣的用例:使用最小化的工作流作为分布式系统中一次性行动的集中锁定机制 - 比如从众多主机中执行的cron任务(主机必须首先进行选举,获得锁的主机才能执行操作)。使用Amazon SWF和最少量的代码也可以实现相同的效果:
以下是一个小的Python示例,使用`boto.swf`(请参考
此帖子 设置域):
编写决策者的代码:
import boto.swf.layer2 as swf
class OneShotDecider(swf.Decider):
domain = 'stackoverflow'
task_list = 'default_tasks'
version = '1.0'
def run(self):
history = self.poll()
if 'events' in history:
decisions = swf.Layer1Decisions()
print 'got the decision task, doing the work'
decisions.complete_workflow_execution()
self.complete(decisions=decisions)
return False
return True
启动决策者的方法如下:
$ ipython -i decider.py
In [1]: while OneShotDecider().run(): print 'polling SWF for decision tasks'
最后,启动工作流:
$ ipython
In [1]: wf_type = swf.WorkflowType(domain='stackoverflow', name='MyWorkflow', version='1.0', task_list='default_tasks')
In [2]: wf_type.start()
Out[2]: <WorkflowExecution 'MyWorkflow-1.0' at 0x32e2a10>
在决策窗口中,你会看到类似以下内容的东西:
polling SWF for decision tasks
polling SWF for decision tasks
got the decision task, doing the work
如果您的工作流程可能会发展其业务逻辑或增加活动数量,最好坚持使用标准方式,即由决策者执行业务逻辑,工作者解决任务。