Browse files

Updated to work with wf@0.8.3/wf-redis-backend@0.8.0

  • Loading branch information...
1 parent 5c77d1b commit aa484bb9c84f48de521b8426537b44ddb7aa470e @kusor committed Mar 27, 2013
Showing with 176 additions and 172 deletions.
  1. +1 −1 README.md
  2. +4 −2 config.redis.json
  3. +58 −58 module.js
  4. +8 −6 package-redis.json
  5. +105 −105 shared-workflow.js
View
2 README.md
@@ -77,7 +77,7 @@ Contents for the other files are:
# LICENSE
-The MIT License (MIT) Copyright (c) 2012 Pedro Palazón Candel
+The MIT License (MIT) Copyright (c) 2013 Pedro Palazón Candel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
View
6 config.redis.json
@@ -15,8 +15,10 @@
"forks": 10,
"run_interval": 60,
"sandbox": {
- "restify": "restify",
- "uuid": "node-uuid"
+ "modules": {
+ "restify": "restify",
+ "uuid": "node-uuid"
+ }
}
}
}
View
116 module.js
@@ -1,83 +1,83 @@
-// Copyright 2012 Pedro P. Candel <kusorbox@gmail.com>. All rights reserved.
+// Copyright 2013 Pedro P. Candel <kusorbox@gmail.com>. All rights reserved.
// Usage example for wf using it as a node module to create
// workflows, queue jobs and obtain the results.
-// NOTE it needs `./node_modules/.bin/workflow-runner.js` running before you
+// NOTE it needs `./node_modules/.bin/workflow-runner` running before you
// run this file.
// Call from parent directory with:
// `node module.js $login $password`
if (process.argv.length < 4) {
- console.error('Github username and password required as arguments');
- process.exit(1);
+ console.error('Github username and password required as arguments');
+ process.exit(1);
}
-var $login = process.argv[2],
- $password = process.argv[3];
+var $login = process.argv[2];
+var $password = process.argv[3];
-var util = require('util'),
- assert = require('assert'),
- path = require('path'),
- fs = require('fs'),
- Factory = require('wf').Factory,
- aWorkflow = require('./shared-workflow');
+var util = require('util');
+var assert = require('assert');
+var path = require('path');
+var fs = require('fs');
+var Factory = require('wf').Factory;
+var aWorkflow = require('./shared-workflow');
aWorkflow.name = 'a gist created using wf module';
var config_file = path.normalize(__dirname + '/config.json');
fs.readFile(config_file, 'utf8', function (err, data) {
- if (err) {
- throw err;
- }
+ if (err) {
+ throw err;
+ }
- var config = JSON.parse(data),
- Backend = require(config.backend.module),
- backend = new Backend(config.backend.opts),
- factory;
+ var config = JSON.parse(data),
+ Backend = require(config.backend.module),
+ backend = new Backend(config.backend.opts),
+ factory;
- backend.init(function () {
- factory = Factory(backend);
- factory.workflow(aWorkflow, function (err, wf) {
- assert.ifError(err);
- assert.ok(wf);
- var aJob = {
- target: '/gists',
- workflow: wf.uuid,
- params: {
- login: $login,
- password: $password
- }
- };
- factory.job(aJob, function (err, job) {
- assert.ifError(err);
- assert.ok(job);
- assert.equal(job.execution, 'queued');
- assert.ok(job.uuid);
-
- var intervalId = setInterval(function () {
- backend.getJob(job.uuid, function (err, obj) {
+ backend.init(function () {
+ factory = Factory(backend);
+ factory.workflow(aWorkflow, function (err, wf) {
assert.ifError(err);
- if (obj.execution === 'queued') {
- console.log('Job waiting to be processed');
- } else if (obj.execution === 'running') {
- console.log('Job in progress ...');
- } else {
- console.log('Job finished. Here come the results:');
- console.log(util.inspect(obj, false, 8));
- // Only one workflow with the same name, need to delete it
- // to allow creating it again:
- backend.deleteWorkflow(wf, function (err, res) {
+ assert.ok(wf);
+ var aJob = {
+ target: '/gists',
+ workflow: wf.uuid,
+ params: {
+ login: $login,
+ password: $password
+ }
+ };
+ factory.job(aJob, function (err, job) {
assert.ifError(err);
- assert.ok(res);
- clearInterval(intervalId);
- process.exit(0);
- });
- }
- });
- }, 3000);
- });
+ assert.ok(job);
+ assert.equal(job.execution, 'queued');
+ assert.ok(job.uuid);
+
+ var intervalId = setInterval(function () {
+ backend.getJob(job.uuid, function (err, obj) {
+ assert.ifError(err);
+ if (obj.execution === 'queued') {
+ console.log('Job waiting to be processed');
+ } else if (obj.execution === 'running') {
+ console.log('Job in progress ...');
+ } else {
+ console.log('Job finished. Here come the results:');
+ console.log(util.inspect(obj, false, 8));
+ // Only one workflow with the same name, need to delete it
+ // to allow creating it again:
+ backend.deleteWorkflow(wf, function (err, res) {
+ assert.ifError(err);
+ assert.ok(res);
+ clearInterval(intervalId);
+ process.exit(0);
+ });
+ }
+ });
+ }, 3000);
+ });
});
});
});
View
14 package-redis.json
@@ -2,21 +2,23 @@
"author": "Pedro Palazón Candel <kusorbox@gmail.com> (http://www.joyent.com)",
"name": "wf-redis-example",
"description": "Node Workflow usage examples",
- "version": "0.2.3",
+ "version": "0.8.0",
"private": true,
"homepage": "https://github.com/kusor/node-workflow-example",
"repository": {
"type": "git",
"url": "git://github.com/kusor/node-workflow-example.git"
},
"engines": {
- "node": ">=0.6"
+ "node": ">=0.8"
},
"dependencies": {
- "wf": "0.2.5",
- "wf-redis-backend": "0.2.3",
- "restify": "1.0.0",
- "node-uuid": "1.3.3"
+ "wf": "0.8.3",
+ "wf-redis-backend": "0.8.0",
+ "bunyan": "0.20.0",
+ "restify": "2.3.5",
+ "node-uuid": "1.4.0",
+ "pooling": "0.1.0"
},
"devDependencies": {}
}
View
210 shared-workflow.js
@@ -1,118 +1,118 @@
-// Copyright 2012 Pedro P. Candel <kusorbox@gmail.com>. All rights reserved.
+// Copyright 2013 Pedro P. Candel <kusorbox@gmail.com>. All rights reserved.
// This is shared by api.js and module.js examples
// This is not really needed, but javascriptlint will complain otherwise:
var restify = require('restify');
var workflow = module.exports = {
- name: 'Sample workflow using gist API',
- chain: [ {
- name: 'Post gist',
- timeout: 30,
- retry: 1,
- body: function (job, cb) {
- if (!job.params.login || !job.params.password) {
- return cb('No login/password provided');
- }
- var client = restify.createJsonClient({
- url: 'https://api.github.com'
- });
- client.basicAuth(job.params.login, job.params.password);
- return client.post('/gists', {
- 'description': 'a gist created using node-workflow',
- 'public': false,
- 'files': {
- 'example.json': {
- 'content': JSON.stringify({
- foo: 'bar',
- bar: 'baz'
- })
- }
- }
- }, function (err, req, res, obj) {
- if (err) {
- return cb(err.name + ': ' + err.body.message);
- } else {
- job.gist_id = obj.id;
- return cb(null, 'Gist created: ' + obj.html_url);
+ name: 'Sample workflow using gist API',
+ chain: [ {
+ name: 'Post gist',
+ timeout: 30,
+ retry: 1,
+ body: function (job, cb) {
+ if (!job.params.login || !job.params.password) {
+ return cb('No login/password provided');
+ }
+ var client = restify.createJsonClient({
+ url: 'https://api.github.com'
+ });
+ client.basicAuth(job.params.login, job.params.password);
+ return client.post('/gists', {
+ 'description': 'a gist created using node-workflow',
+ 'public': false,
+ 'files': {
+ 'example.json': {
+ 'content': JSON.stringify({
+ foo: 'bar',
+ bar: 'baz'
+ })
+ }
+ }
+ }, function (err, req, res, obj) {
+ if (err) {
+ return cb(err.name + ': ' + err.body.message);
+ } else {
+ job.gist_id = obj.id;
+ return cb(null, 'Gist created: ' + obj.html_url);
+ }
+ });
}
- });
- }
- }, {
- name: 'Get starred gist',
- timeout: 60,
- retry: 2,
- body: function (job, cb) {
- if (!job.params.login || !job.params.password) {
- return cb('No login/password provided');
- }
- if (!job.gist_id) {
- return cb('Unknown gist id');
- }
- var client = restify.createJsonClient({
- url: 'https://api.github.com'
- });
- client.basicAuth(job.params.login, job.params.password);
- return client.get(
- '/gists/' + job.gist_id + '/star',
- function (err, req, res, obj) {
- // Indeed, there should be an error here, which will triger our
- // fallback, where we'll add the star to our gist
- if (err) {
- return cb(err);
- } else {
- // It shouldn't be starred, but ...
- return cb(null, 'What!?, gist was already starred');
- }
- });
- },
- fallback: function (err, job, cb) {
- if (err.statusCode && err.statusCode === 404) {
- // We know how to fix this: star it!:
- var client = restify.createJsonClient({
- url: 'https://api.github.com'
- });
- client.basicAuth(job.params.login, job.params.password);
- return client.put(
- '/gists/' + job.gist_id + '/star', {},
- function (err, req, res, obj) {
- if (err) {
- return cb(err);
+ }, {
+ name: 'Get starred gist',
+ timeout: 60,
+ retry: 2,
+ body: function (job, cb) {
+ if (!job.params.login || !job.params.password) {
+ return cb('No login/password provided');
+ }
+ if (!job.gist_id) {
+ return cb('Unknown gist id');
+ }
+ var client = restify.createJsonClient({
+ url: 'https://api.github.com'
+ });
+ client.basicAuth(job.params.login, job.params.password);
+ return client.get(
+ '/gists/' + job.gist_id + '/star',
+ function (err, req, res, obj) {
+ // Indeed, there should be an error here, which will triger our
+ // fallback, where we'll add the star to our gist
+ if (err) {
+ return cb(err);
+ } else {
+ // It shouldn't be starred, but ...
+ return cb(null, 'What!?, gist was already starred');
+ }
+ });
+ },
+ fallback: function (err, job, cb) {
+ if (err.statusCode && err.statusCode === 404) {
+ // We know how to fix this: star it!:
+ var client = restify.createJsonClient({
+ url: 'https://api.github.com'
+ });
+ client.basicAuth(job.params.login, job.params.password);
+ return client.put(
+ '/gists/' + job.gist_id + '/star', {},
+ function (err, req, res, obj) {
+ if (err) {
+ return cb(err);
+ } else {
+ return cb(null, 'Gist starred!.');
+ }
+ });
} else {
- return cb(null, 'Gist starred!.');
+ return cb(err);
}
- });
- } else {
- return cb(err);
- }
- }
- }],
- timeout: 180,
- onerror: [ {
- name: 'Delete gist if exists',
- body: function (job, cb) {
- if (!job.params.login || !job.params.password) {
- return cb('No login/password provided');
- }
- // If we created a gist and something failed later, let's remove it:
- if (job.gist_id) {
- var client = restify.createJsonClient({
- url: 'https://api.github.com'
- });
- client.basicAuth(job.params.login, job.params.password);
- return client.del(
- '/gists/' + job.gist_id,
- function (err, req, res, obj) {
- if (err) {
- return cb(err);
+ }
+ }],
+ timeout: 180,
+ onerror: [ {
+ name: 'Delete gist if exists',
+ body: function (job, cb) {
+ if (!job.params.login || !job.params.password) {
+ return cb('No login/password provided');
+ }
+ // If we created a gist and something failed later, let's remove it:
+ if (job.gist_id) {
+ var client = restify.createJsonClient({
+ url: 'https://api.github.com'
+ });
+ client.basicAuth(job.params.login, job.params.password);
+ return client.del(
+ '/gists/' + job.gist_id,
+ function (err, req, res, obj) {
+ if (err) {
+ return cb(err);
+ } else {
+ return cb('Cleanup done');
+ }
+ });
} else {
- return cb('Cleanup done');
+ return cb('Nothing to cleanup');
}
- });
- } else {
- return cb('Nothing to cleanup');
- }
- }
- }]
+ }
+ }]
};

0 comments on commit aa484bb

Please sign in to comment.