-
Notifications
You must be signed in to change notification settings - Fork 6
/
runtask.js
executable file
·87 lines (81 loc) · 2.4 KB
/
runtask.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
#!/usr/bin/env node
var _ = require('lodash');
var async = require('async-chainable');
var colors = require('colors');
var program = require('commander');
global.app = require('./units/core/backend.js');
app.db = require('./config/db.conf.js');
program
.version(require('./package.json').version)
.option('-f, --force', 'Run a task even if it is already marked as completed')
.option('-t, --task [id]', 'The Task ID to execute')
.parse(process.argv);
var Tasks = require('./models/tasks');
async()
.set('prefix', colors.blue('[TASK RUNNER' + (program.task ? '/' + program.task : '') + ']'))
// Fetch the task {{{
.then('task', function(next) {
if (!program.task) return next('No Task ID specified');
Tasks.findOne({_id: program.task}, next);
})
// }}}
// Sanity checks {{{
.then(function(next) {
if (!this.task || !this.task._id) return next('Task ID not found: ' + program.task);
if (this.task.status != 'pending') {
if (program.force) {
console.log(this.prefix, 'Task status is', colors.cyan(this.task.status), 'but forcing anyway');
} else {
return next('Task status is ' + this.task.status + '. Refusing to run without --force');
}
}
next();
})
// }}}
// Mark as processing so the next task-check cycle doesn't grab it {{{
.then(function(next) {
this.task.status = 'processing';
this.task.save(next);
})
// }}}
// Actually run the task {{{
.then(function(next) {
var task = this.task;
try {
var worker = require('./tasks/' + task.worker);
} catch (e) {
return next('Error loading worker ' + task.worker + ' - ' + e.toString());
}
console.log(this.prefix, 'Starting task', colors.cyan(task._id), 'with worker', colors.cyan(task.worker));
worker(function(err) {
if (err) {
task.status = 'error';
task.touched = new Date();
task.history.push({
type: 'error',
response: err.toString(),
});
task.save(next);
} else if (task.status == 'error') { // Error already set by something upstream
// Do nothing
task.touched = new Date();
task.save(next);
} else {
task.touched = new Date();
task.status = 'completed';
task.save(next);
}
}, task);
})
// }}}
// End - terminate the process {{{
.end(function(err) {
if (err) {
console.log(this.prefix, colors.red('ERROR'), err.toString());
process.exit(1);
} else {
console.log(this.prefix, colors.green('COMPLETE'));
process.exit(0);
}
});
// }}}