4

I need Amazon SWF to distribute some work, make sure it's done asynchronously, make sure it's store in a reliable way and that it's automatically restarted. However, the workflow logic I need is extremely simple: it's just to get a single task executed.

I implemented it now the way it's supposed to be done:

  1. Request workflow execution
  2. Decider founds out about it and schedules an activity
  3. Workers finds out about the activity request, performs the results and returns the results
  4. Decider notices a result and copies it over in a workflow completion

It seems to me that I can just have the decider do the work – as it were – and complete the workflow execution immediately. That would take care of a lot of code. (The activity might also fail, timeout, etc. All things that I currently need to cater for.)

So back to my question: can I have a decider that performs the work itself and completes the 'workflow' immediately?

2 Answers 2

2

Yes. Actually, I think you came up with an interesting use case: using a minimal workflow as a centralized locking mechanism for one-off actions in a distributed system - such as cron jobs executed from a single host in a fleet of many (the hosts have to first undergo election and whichever wins the lock gets to execute an action). The same could be achieved with Amazon SWF and minimum amount of code:

A small Python example, using boto.swf (use 1. from this post to setup the domain):

To code the decider:

#MyDecider.py
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

To start the decider:

$ ipython -i decider.py
In [1]: while OneShotDecider().run(): print 'polling SWF for decision tasks'

Finally, to start the workflow:

$ 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>

Back in the decider window, you you'll see something like:

polling SWF for decision tasks
polling SWF for decision tasks
got the decision task, doing the work

If your workflow is likely to evolve its business logic or grow in the number of activities, it's probably best to stick to the standard way of having Deciders doing the business logic and Workers solving the tasks.

1

While yes, you can do this (as pointed out by the other answer), there are some things to consider before doing so:

  1. Why are you using SWF to execute this task? Why bother setting it up as a workflow and paying for "StartWorkflow" executions if you can get the same benefit by just invoking your code more directly? If you need to track execution submissions and completions, you can just use an SQS queue for this and get the same results for cheaper.
  2. Your workflows might be extremely simple right now, but they often can and do evolve to be more complex over time. Designing it right from the start can save time in the long run. Do you want future developers working on your code thinking that they should just add more logic to the workflow? Will they know to lookup how to use activities, or just follow the existing pattern you've started with? (Hint - they'll be likely to copy your pattern - developers are lazy :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.