-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·77 lines (60 loc) · 1.6 KB
/
server.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
#!/usr/bin/env node
"use strict";
/* eslint-disable no-process-exit */
var os = require("os");
var async = require("async"),
env = require("require-env"),
exquisite = require("exquisite"),
raven = require("raven");
var worker = require("./lib/worker");
var QUEUE_NAME = env.require("SQS_QUEUE_NAME");
var sentry = new raven.Client(),
workers = [];
// TODO set this to (<task count> * 2) + 2 once shell.js learns how to clean
// up after itself
process.setMaxListeners(0);
if (process.env.SENTRY_DSN) {
raven.patchGlobal(function(logged, err) {
console.log("Uncaught error. Reporting to Sentry and exiting.");
console.error(err.stack);
process.exit(1);
});
}
// Start 2 * <nprocs> workers
async.times(os.cpus().length / 2, function(n, callback) {
workers.push(exquisite({
name: QUEUE_NAME
}, function(task, cb) {
return worker(n, task, function(err) {
if (err) {
console.warn(err.message);
sentry.captureError(err, {
extra: {
task: task
}
});
}
return cb.apply(null, arguments);
});
}));
return callback();
});
// Signal handlers
var cleanup = function() {
// cancel all workers (prevent them from picking up new jobs so we can quit
// gracefully)
workers.forEach(function(x) {
x.cancel();
});
};
// Quit immediately when receiving 2+ signals within 5s
["SIGINT", "SIGTERM"].forEach(function(signal) {
process.once(signal, function() {
cleanup();
setTimeout(function() {
process.once(signal, function() {
cleanup();
});
}, 5000).unref();
});
});