GithubHelp home page GithubHelp logo

aws-swf's Introduction

A Node.js library for accessing Amazon Simple Workflow (SWF)

Requirements

Installation

npm install aws-swf

See also

Documentation

aws-swf uses the official Node.js aws-sdk for the low-level API calls. You can find the full API reference here

aws-swf provide classes to wrap higher-level concepts of the AWS SWF API :

  • ActivityPoller
  • ActivityTask
  • Decider
  • DecisionTask
  • Workflow
  • WorkflowExecution

Global configuration

Don't hardcode your Amazon credentials :

If no config is passed to createClient (see below), aws-swf will walk up the directory hierarchy until it finds a .aws-swf.json file.

{
    "accessKeyId": "xxxxxx",
    "secretAccessKey": "xxxxxx",
    "region": "us-east-1",
    "defaultDomain": "aws-swf-test-domain",
    "defaultTasklist": "aws-swf-tasklist"
}

Creating an SWF client object

var swf = require("aws-swf");
var swfClient = swf.createClient({
    accessKeyId: "... access key id here ...",
    secretAccessKey: "... secret key here ...",
    region: "us-east-1"
});

or using the global configuration file :

var swf = require("aws-swf"),
    swfClient = swf.createClient();

Creating an ActivityPoller

An ActivityPoller polls Amazon SWF for new tasks to be done.

An ActivityTask is instantiated by an ActivityPoller when it receives a task from SWF.

It is passed to your callback and adds the respondCompleted() and respondFailed() methods.

Example :

var swf = require("aws-swf");

var activityPoller = new swf.ActivityPoller(swfClient, {
    
   "domain": "test-domain",
   "taskList": { "name": "test-taskList" },
   "identity": "ActivityPoller-1"
   
}, function (activityTask, cb) {
   
   console.log('A new task is available !');
   // Do something here...
   
   // sends "RespondActivityTaskCompleted" to SWF
   // result: Maximum length of 32768.
   activityTask.respondCompleted(result, function (err) {
      cb(true); // free the poller for new activities
   }); 
   
   // or :
   
   // sends "RespondActivityTaskFailed" to SWF
   // parameters: 
   // reason (string or undefined): Maximum length of 256
   // details (string or undefined): Maximum length of 32768
   activityTask.respondFailed(reason, details, function (err) {
       cb(true); // free the poller for new activities
   });
   
});

activityPoller.start();

It is not recommanded to stop the poller in the middle of a long-polling request, because SWF might schedule an ActivityTask to this poller anyway, which will obviously timeout. The .stop() method will wait for the end of the current polling request, eventually wait for a last activity execution, then stop properly :

// on SIGINT event, close the poller properly
process.on('SIGINT', function () {
   console.log('Got SIGINT ! Stopping activity poller after this request...please wait...');
   activityPoller.stop();
});

Creating a Decider

A Decider will poll Amazon SWF for new decision tasks.

A DecisionTask is instantiated by a Decider when it receives a decision task from SWF.

var swf = require("aws-swf");

var myDecider = new swf.Decider(swfClient, {
    
   "domain": "test-domain",
   "taskList": {"name": "my-workflow-tasklist"},
   "identity": "Decider-01",
   
   "maximumPageSize": 500,
   "reverseOrder": false // IMPORTANT: must replay events in the right order, ie. from the start
   
}, function (decisionTask, cb) {
    
    // do something here and send decisions...
    
    decisionTask.complete_workflow_execution("details of ending here ?", function (err) {
        
    });
    
    cb(true); // to continue polling
});

It is not recommanded to stop the poller in the middle of a long-polling request, because SWF might schedule an ActivityTask to this poller anyway, which will obviously timeout. The .stop() method will wait for the end of the current polling request, eventually wait for a last activity execution, then stop properly :

// on SIGINT event, close the poller properly
process.on('SIGINT', function () {
   console.log('Got SIGINT ! Stopping decider poller after this request...please wait...');
   myDecider.stop();
});

Starting a Workflow

var swf = require("aws-swf");

var workflow = new swf.Workflow(swfClient, {
   "domain": "test-domain",
   "workflowType": {
      "name": name,
      "version": version
   },
   "taskList": { "name": "my-workflow-tasklist" },

   "executionStartToCloseTimeout": "1800",
   "taskStartToCloseTimeout": "1800",

   "tagList": ["music purchase", "digital"],
   "childPolicy": "TERMINATE"
});

To start a new workflowExecution :

var workflowExecution = workflow.start({ input: "{}"}, function (err, runId) {

   if (err) {
      console.log("Cannot start workflow : ", err);
      return;
   }

   console.log("Workflow started, runId: " +runId);

});

Examples

Those examples should be executed in order :

Decider API

  • TODO: decider helper methods to make it easier to query the event history (dt.completed('step1'), dt.failed('step1'), dt.results('step1'))

    if (dt.failed()) { ... dt.scheduled() }

  • ... to schedule tasks/timer/workflows etc... dt.schedule({ ... })

  • to stop, or signal, or ...

API Documentation

jsdoc lib/*.js README.md

License

MIT License

aws-swf's People

Contributors

ericabouaf avatar melnicki avatar

Watchers

Navid Nikpour avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.