Skip to content

Instantly share code, notes, and snippets.

@twslankard
Created March 20, 2013 23:24
Show Gist options
  • Save twslankard/5209452 to your computer and use it in GitHub Desktop.
Save twslankard/5209452 to your computer and use it in GitHub Desktop.
Simple Workflow node.js-based Decider test
var events = require('events');
var util = require('util');
function Decider(_domain, task_list) {
var aws = require('aws-sdk');
this._domain = _domain;
this.task_list = task_list;
this.service = new aws.SimpleWorkflow();
events.EventEmitter.call(this);
}
util.inherits(Decider, events.EventEmitter);
Decider.prototype.start = function() {
var self = this;
self.on('loop', self.loop);
self.loop();
}
Decider.prototype.handleDecision = function(decisionTask) {
var self = this;
// XXX handle paginated events
var new_events = self.getNewEventsForDecisionTask(decisionTask);
for(e in new_events) {
var event_type = new_events[e].eventType;
self.emit(event_type, decisionTask);
}
self.emit('loop');
}
Decider.prototype.loop = function() {
var self = this;
console.log("polling " + self._domain + ":" + self.task_list);
self.service.client.pollForDecisionTask({domain:self._domain, taskList:{name:self.task_list}}, function(err, decisionTask) {
try {
if(err) {
console.log(err);
} else {
self.handleDecision(decisionTask);
}
} catch (e) {
console.log(e);
}
});
}
Decider.prototype.getNewEventsForDecisionTask = function(decisionTask) {
// XXX handle paginated events
return decisionTask.events.slice( decisionTask.previousStartedEventId );
}
Decider.prototype.scheduleActivityTask = function(activity_type, task_list_name, decisionTaskToken) {
console.log("scheduling activity task");
var self = this;
var activity_id = "bullshit";
var task_list = {name:task_list_name};
var attributes = {activityType:activity_type, activityId:activity_id, taskList:task_list};
var decisions = [{decisionType:"ScheduleActivityTask", scheduleActivityTaskDecisionAttributes:attributes}];
var parameters = {taskToken:decisionTaskToken, decisions:decisions};
self.service.client.respondDecisionTaskCompleted(parameters, function(err, data){
if(err) {
console.log("Error scheduling activity task: " + err);
} else {
console.log("Successfully scheduled activity task: " + data);
}
});
}
var decider = new Decider("test-domain", "hello-tasklist");
decider
.on('WorkflowExecutionStarted', function(decisionTask) {
console.log("yay! workflow execution started handler");
var activity_type = {name:"get-name", version:"1"};
decider.scheduleActivityTask(activity_type, "hello-tasklist", decisionTask.taskToken);
})
.on('ActivityTaskCompleted', function(decisionTask) {
console.log("Activity task completed: " + decisionTask.activityTaskCompletedEventAttributes );
})
.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment