UNPKG

usher

Version:

Simple DSL for composing decision workflows for AWS Simple Workflow

167 lines (124 loc) 4.99 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: activity/task.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: activity/task.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/*! * Usher * Copyright(c) 2014 meltmedia &lt;mike@meltmedia.com> */ 'use strict'; var winston = require('winston'); module.exports = ActivityTask; /** * The context for the execution of the activity * @constructor * @param {string} task - The raw `aws-swf` activity task */ function ActivityTask(task) { if (!(this instanceof ActivityTask)) { return new ActivityTask(task); } this.input = {}; if (task.config.input) { this.input = JSON.parse(task.config.input); } // Depending on the language of the Decider impl, this can be an Array or Object // This is a big assumption, maybe make this configurable if (Array.isArray(this.input)) { this.input = this.input[0]; } this.activityId = task.config.activityId; this.activityType = task.config.activityType; this.startedEventId = task.config.startedEventId; this.taskToken = task.config.taskToken; this.workflowExecution = task.config.workflowExecution; this._task = task; // Register a hearbeat callback every 10 seconds this.hearbeatCount = 0; this.heartbeatTimer = setInterval(function () { var self = this; self._task.recordHeartbeat( { activity: self.activityType.name, id: self.activityId, count: self.hearbeatCount++ }, function (err) { if (err) { winston.log('debug', 'Heartbead failed for task: %s (%s) due to:', self.activityType.name, self.activityId, err); } }); }.bind(this), 10000); } /** * Execute once the activity has completed successfuly * * @param {object} output - The results of the activity, if any * @param {function} [cb] - Callback to run once we have successfuly informed SWF of our status */ ActivityTask.prototype.success = function (output, cb) { var self = this; cb = cb || function () {}; winston.log('debug', 'Activity task: %s (%s) completed successfuly', this.activityType.name, self.activityId); // Stop heartbeat timer clearInterval(self.heartbeatTimer); this._task.respondCompleted(output, function (err) { if (err) { return winston.log('warn', 'Unable to complete activity task: %s (%s) due to: %s', self.activityType.name, self.activityId, err); } winston.log('verbose', 'Completed activity task: %s (%s)', self.activityType.name, self.activityId); cb(err); }); }; /** * Execute if the activity failed * * @param {string} name - A unique name for the failure * @param {object} [err] - The reason for the failure * @param {boolean} [retriable] - A hint to if the activity should be attempted again * @param {function} [cb] - Callback to run once we have successfuly informed SWF of our status */ ActivityTask.prototype.failed = function (name, err, retriable, cb) { var self = this; cb = cb || function () {}; winston.log('debug', 'Activity task: %s (%s) failed, due to: %s', this.activityType.name, this.activityId, name); // Stop heartbeat timer clearInterval(self.heartbeatTimer); var message = { name: name, retriable: retriable || false, message: err.message || err }; this._task.respondFailed(name, JSON.stringify(message), function (e) { if (e) { return winston.log('warn', 'Unable to fail activity task: %s (%s) due to:', self.activityType.name, self.activityId, e); } winston.log('warn', 'Failed activity task: %s (%s) due to:', self.activityType.name, self.activityId, message); cb(e); }); }; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="Accumulator.html">Accumulator</a></li><li><a href="ActivityPoller.html">ActivityPoller</a></li><li><a href="ActivityTask.html">ActivityTask</a></li><li><a href="DecisionPoller.html">DecisionPoller</a></li><li><a href="Fragment.html">Fragment</a></li><li><a href="Loop.html">Loop</a></li><li><a href="Usher.html">Usher</a></li><li><a href="WhileLoop.html">WhileLoop</a></li><li><a href="WorkflowVersion.html">WorkflowVersion</a></li></ul> </nav> <br clear="both"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Mon Sep 12 2016 14:59:00 GMT-0700 (MST) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>