forked from Automattic/kue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
executable file
·127 lines (108 loc) · 2.6 KB
/
Copy pathevents.js
File metadata and controls
executable file
·127 lines (108 loc) · 2.6 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
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
/*!
* kue - events
* Copyright (c) 2011 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var redis = require('../redis');
/**
* Job map.
*/
exports.jobs = {};
/**
* Pub/sub key.
*/
exports.key = 'events';
/**
* Add `job` to the jobs map, used
* to grab the in-process object
* so we can emit relative events.
*
* @param {Job} job
* @api private
*/
exports.callbackQueue = [];
exports.add = function (job, callback) {
if (job.id) exports.jobs[job.id] = job;
// if (!exports.subscribed) exports.subscribe();
if (!exports.subscribeStarted) exports.subscribe();
if (!exports.subscribed) {
exports.callbackQueue.push(callback);
} else {
callback();
}
};
/**
* Remove `job` from the jobs map.
*
* @param {Job} job
* @api private
*/
exports.remove = function (job) {
delete exports.jobs[job.id];
};
/**
* Subscribe to "q:events".
*
* @api private
*/
exports.subscribe = function () {
// if (exports.subscribed) return;
if (exports.subscribeStarted) return;
var client = redis.pubsubClient();
client.on('message', exports.onMessage);
client.on('subscribe', function () {
exports.subscribed = true;
while (exports.callbackQueue.length) {
process.nextTick(exports.callbackQueue.shift());
}
});
client.subscribe(client.getKey(exports.key));
exports.queue = require('../kue').singleton;
// exports.subscribed = true;
exports.subscribeStarted = true;
};
exports.unsubscribe = function () {
var client = redis.pubsubClient();
client.unsubscribe();
client.removeAllListeners();
exports.subscribeStarted = false;
};
/**
* Message handler.
*
* @api private
*/
exports.onMessage = function (channel, msg) {
// TODO: only subscribe on {Queue,Job}#on()
msg = JSON.parse(msg);
// map to Job when in-process
var job = exports.jobs[msg.id];
if (job) {
job.emit.apply(job, msg.args);
if (['complete', 'failed'].indexOf(msg.event) !== -1) exports.remove(job);
}
// emit args on Queues
msg.args[0] = 'job ' + msg.args[0];
msg.args.push(msg.id);
if ( exports.queue ) {
exports.queue.emit.apply(exports.queue, msg.args);
}
};
/**
* Emit `event` for for job `id` with variable args.
*
* @param {Number} id
* @param {String} event
* @param {Mixed} ...
* @api private
*/
exports.emit = function (id, event) {
var client = redis.client()
, msg = JSON.stringify({
id: id, event: event, args: [].slice.call(arguments, 1)
});
client.publish(client.getKey(exports.key), msg);
};