-
Notifications
You must be signed in to change notification settings - Fork 66
/
example.js
134 lines (122 loc) · 4.23 KB
/
example.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2013 Pedro P. Candel <[email protected]>. All rights reserved.
// Copyright (c) 2017, Joyent, Inc.
// First part of the example: You create workflows, add tasks, queue jobs
// anywhere in your code using NodeJS.
var assert = require('assert');
var util = require('util');
var wf = require('./lib/index');
// With modules, it would be require('workflow');
var Factory = wf.Factory;
var Backend = wf.Backend;
var backend, factory;
// Some globals:
var aWorkflow, aJob;
// We'll use 'vasync' module to simplify definitions a bit, and avoid nesting
// stuff for clarity:
var vasync = require('vasync');
// Our serie of functions to execute:
var pipeline = [
function createWorkflow(_, callback) {
// A workflow definition:
factory.workflow({
name: 'Sample Workflow',
chain: [ {
// A task. It will fail on first retry, but succeed on 2nd one:
name: 'A Task',
timeout: 30,
retry: 2,
body: function (job, cb) {
if (!job.foo) {
job.foo = true;
return cb('Foo was not defined');
}
return cb(null);
}
},
{
// This task will fail, but it will succeed when the task's
// fallback function is called:
name: 'Another task',
body: function (job, cb) {
return cb('Task body error');
},
// Note that the `fallback` function takes the error as its
// first argument:
fallback: function (err, job, cb) {
job.the_err = err;
return cb(null);
}
},
{
// This task will fail and, given there isn't an fallback
// callback, the workflow will call the `onerror` chain:
name: 'A task which will fail',
body: function (job, cb) {
job.this_failed_because = 'We decided it.';
return cb('Task body error');
}
}],
timeout: 180,
onError: [ {
name: 'A fallback task',
body: function (job, cb) {
// Some task failed and, as a consequence, this task is
// being executed
if (job.error) {
// Do something here ...
cb(job.error);
}
cb(null);
}
}]
}, function (err, workflow) {
if (err) {
callback(err);
}
aWorkflow = workflow;
callback(null, workflow);
});
},
function createJob(_, callback) {
// A Job based on the workflow:
factory.job({
workflow: aWorkflow.uuid,
exec_after: '2012-01-03T12:54:05.788Z'
}, function (err, job) {
if (err) {
callback(err);
}
aJob = job;
callback(null, job);
});
}
];
function main() {
backend = Backend({});
backend.init(function () {
factory = Factory(backend);
assert.ok(factory);
vasync.pipeline({
funcs: pipeline
}, function (err, results) {
if (err) {
console.error(err);
return;
}
// At this point, we should have a results array with all the
// tasks, the workflow and the job, on the same order we defined
// them but,given we've set the objects to globals, we couldn't
// care less about this async's results array.
//
// Our tasks and workflow should have been created, and our job
// should have been created and queued:
assert.ok(aWorkflow);
assert.ok(aWorkflow.uuid);
assert.ok(aJob);
// We need the UUID in order to be able to check Job Status
assert.ok(aJob.uuid);
console.log(util.inspect(results.operations, false, 8));
});
});
}
main();