Easier way of using AWS Simple Workflow
easy-swf is a module made to make using the AWS Simple Workflow Service a little easier to use.
You need to be familiar with how SWF works. This link is useful for understanding the core concepts.
$ npm install easy-swf
To manage a SWF Workflow Execution, we require a ActivityHost to handle the activities and a DeciderHost to handle the decisions. An ActivityHost and a DeciderHost do not need to run in the same process.
First, we configure our client:
var easy = require"easy-swf";var workflow ="domain": "ExampleDomain""taskList": "taskList";var awsConfig ="accessKeyId": "[YourAccessKeyId]""secretAccessKey": "[YourSecretAccessKey]""region": "[YourRegion]";var client = workflow awsConfig;
awsConfig parameter is optional. If the argument is not provided it will default to the AWS settings in your environment. Even if you want to have your application pass in some AWS settings (like proxy settings) you can omit the Credentials as long as they are available in your environment.
The ActivityHost, unsurprisingly, handles activities. Tasks scheduled by the Decider will be routed here. The
data parameter of the callback will contain whatever value is passed from the decider.
Here's an example:
var acts = clientcreateActivityHost"taskList";actshandleActivity"taskOne" "1"nextnull "one";;actshandleActivity"taskTwo" "1"nextnull data + " two";;actshandleActivity"taskThree" "1"nextnull data + " three";;actslistenconsole.logmessage;;
Any errors raised by easy-swf will be return via the
listen() callback and you can keep track of what SWF is doing via the
message in the callback if you like.
A DeciderHost is created to handle all decisions. A decision handler is created for each workflow. Any framework errors will returned via the
listen() method just like the ActivityHost.
Here is an example decider:
var decider = clientcreateDeciderHost"taskList";deciderhandleWorkflow"example" "1"var taskOne = contextgetFunction"taskOne" "1";var taskTwo = contextgetFunction"taskTwo" "1";var taskThree = contextgetFunction"taskThree" "1";taskOne"input"if feedErr != null contextfailWorkflowfeedErr; return;taskTwofeedDataif summaryErr != null contextfailWorkflowsummaryErr; return;taskThreesummaryDataif finalErr != null contextfailWorkflowfinalErr; return;console.logfinalData;contextcompleteWorkflow;;;;;deciderlistenif err != nullconsole.log"[Framework Error]" err;if context != nullcontextfailWorkflowerr;return;console.logmessage;;
context object represents the current state of the workflow and is used to interact with SWF. The state of the
context object is rebuilt from the WorkflowExecutionHistory on every decision.
context object has the following methods:
This returns different functions based on the current WorkflowExecutionHistory. These functions will schedule tasks, return data from activities, or raise errors depending on what is appropriate given the WorkflowExecutionHistory.
This allows you to write simpler logic in your decider using a traditional callback structure.
Tells SimpleWorkflow to complete the WorkflowExecution.
Tells SimpleWorkflow to fails the WorkflowExecution and stores err.message in the WorkflowExecution.
You must always return a response to SWF even if you don't want to make any decisions. This method tells SWF that you don't want to make a decision just yet.
This is to support the execution of actvities in parallel.
- The callback for ActivityHost.handleActivity() no longer includes an error. Errors are returned via ActivityHost.listen()
- The callback for DeciderHost.handleWorkflow() no longer includes an error. Errors are returned via DeciderHost.listen()
- DeciderContext.getFunction now requires a version parameter. This removed the need for explicit tast configuration.