This repository was archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqueues.js
More file actions
61 lines (52 loc) · 1.43 KB
/
queues.js
File metadata and controls
61 lines (52 loc) · 1.43 KB
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
'use strict';
module.exports = function(app) {
var _ = require('underscore');
var async = require('async');
var queues = {
onStart: async.queue(function(task, next) {
// On-start callbacks are asynchronous.
task.fn(next);
}, 1/* concurrency */),
onReady: async.queue(function(task, next) {
// On-ready callbacks are synchronous.
task.fn();
next();
}, 1/* concurrency */),
onExit: async.queue(function(task, next) {
// On-exit callbacks are asynchronous.
task.fn(next);
}, 1/* concurrency */)
};
// Pause all queues.
// This prevents execution of queued items until queue.resume() is called.
_.invoke(queues, 'pause');
// Add a function to the app context for each queue (to make it easier to add tasks).
_.each(_.keys(queues), function(name) {
app[name] = function(fn) {
queues[name].push({ fn: fn });
};
});
queues.onStart.drain = function() {
// All on-start callbacks have been executed.
// Resume the on-ready queue.
queues.onReady.resume();
queues.onStart = null;
app.log('app started');
};
queues.onStart.error = function(error) {
app.error(error);
};
process.on('SIGINT', function() {
if (!(queues.onExit.length() > 0)) {
// Nothing in the queue. Exit the process immediately.
process.exit(0);
} else {
queues.onExit.drain = function() {
// All on-exit callbacks have been executed.
process.exit(0);
};
queues.onExit.resume();
}
});
return queues;
};