-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapi.js
75 lines (65 loc) · 2.62 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2014 Pedro P. Candel <[email protected]>. All rights reserved.
// Usage example for node-workflow using the REST API to create
// workflows, queue jobs and obtain the results.
// NOTE it needs `./node_modules/.bin/workflow-api` and
// `./node_modules/.bin/workflow-runner` running
// before you run this file.
// Call from parent directory with:
// `node api.js $login $password`
if (process.argv.length < 4) {
console.error('Github username and password/token required as arguments');
process.exit(1);
}
var $login = process.argv[2];
var $password = process.argv[3];
var restify = require('restify'),
util = require('util'),
assert = require('assert');
var client = restify.createJsonClient({
url: 'http://127.0.0.1:8080'
});
var aWorkflow = require('./shared-workflow');
assert.ok(aWorkflow);
// API needs everything being JSON, while node-module takes care of
// this by itself so, stringify things here:
aWorkflow.chain[0].body = aWorkflow.chain[0].body.toString();
aWorkflow.chain[1].body = aWorkflow.chain[1].body.toString();
aWorkflow.chain[1].fallback = aWorkflow.chain[1].fallback.toString();
aWorkflow.onerror[0].body = aWorkflow.onerror[0].body.toString();
client.post('/workflows', aWorkflow, function (err, req, res, wf) {
assert.ifError(err);
assert.ok(wf.uuid);
var aJob = {
target: '/gists',
workflow: wf.uuid,
login: $login,
password: $password
};
client.post('/jobs', aJob, function (err, req, res, job) {
assert.ifError(err);
assert.ok(job);
assert.equal(job.execution, 'queued');
assert.ok(job.uuid);
var intervalId = setInterval(function () {
client.get('/jobs/' + job.uuid, function (err, req, res, 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:
client.del('/workflows/' + wf.uuid,
function (err, req, res, obj) {
assert.ifError(err);
clearInterval(intervalId);
process.exit(0);
});
}
});
}, 3000);
});
});