forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
66 lines (59 loc) · 1.96 KB
/
worker.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
'use strict';
const utils = require('./utils');
const clientCommandMessageReg = /ERR unknown command ['`]\s*client\s*['`]/;
module.exports = function(Queue) {
// IDEA, How to store metadata associated to a worker.
// create a key from the worker ID associated to the given name.
// We keep a hash table bull:myqueue:workers where every worker is a hash key workername:workerId with json holding
// metadata of the worker. The worker key gets expired every 30 seconds or so, we renew the worker metadata.
//
Queue.prototype.setWorkerName = function() {
return utils
.isRedisReady(this.client)
.then(() => {
return this.client.client('setname', this.clientName());
})
.catch(err => {
if (!clientCommandMessageReg.test(err.message)) throw err;
});
};
Queue.prototype.getWorkers = function() {
return utils
.isRedisReady(this.client)
.then(() => {
return this.client.client('list');
})
.then(clients => {
return this.parseClientList(clients);
})
.catch(err => {
if (!clientCommandMessageReg.test(err.message)) throw err;
});
};
Queue.prototype.base64Name = function() {
return Buffer.from(this.name).toString('base64');
};
Queue.prototype.clientName = function() {
return this.keyPrefix + ':' + this.base64Name();
};
Queue.prototype.parseClientList = function(list) {
const lines = list.split('\n');
const clients = [];
lines.forEach(line => {
const client = {};
const keyValues = line.split(' ');
keyValues.forEach(keyValue => {
const index = keyValue.indexOf('=');
const key = keyValue.substring(0, index);
const value = keyValue.substring(index + 1);
client[key] = value;
});
const name = client['name'];
if (name && name.startsWith(this.clientName())) {
client['name'] = this.name;
clients.push(client);
}
});
return clients;
};
};