forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetters.js
195 lines (162 loc) · 5.12 KB
/
getters.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
const _ = require('lodash');
const Job = require('./job');
module.exports = function(Queue) {
Queue.prototype.getJob = function(jobId) {
return Job.fromId(this, jobId);
};
Queue.prototype._commandByType = function(types, count, callback) {
return _.map(types, type => {
type = type === 'waiting' ? 'wait' : type; // alias
const key = this.toKey(type);
switch (type) {
case 'completed':
case 'failed':
case 'delayed':
case 'repeat':
return callback(key, count ? 'zcard' : 'zrange');
case 'active':
case 'wait':
case 'paused':
return callback(key, count ? 'llen' : 'lrange');
}
});
};
/**
Returns the number of jobs waiting to be processed.
*/
Queue.prototype.count = function() {
return this.getJobCountByTypes('wait', 'paused', 'delayed');
};
// Job counts by type
// Queue#getJobCountByTypes('completed') => completed count
// Queue#getJobCountByTypes('completed,failed') => completed + failed count
// Queue#getJobCountByTypes('completed', 'failed') => completed + failed count
// Queue#getJobCountByTypes('completed,waiting', 'failed') => completed + waiting + failed count
Queue.prototype.getJobCountByTypes = function() {
return this.getJobCounts.apply(this, arguments).then(result => {
return _.chain(result)
.values()
.sum()
.value();
});
};
/**
* Returns the job counts for each type specified or every list/set in the queue by default.
*
*/
Queue.prototype.getJobCounts = function() {
const types = parseTypeArg(arguments);
const multi = this.multi();
this._commandByType(types, true, (key, command) => {
multi[command](key);
});
return multi.exec().then(res => {
const counts = {};
res.forEach((res, index) => {
counts[types[index]] = res[1] || 0;
});
return counts;
});
};
Queue.prototype.getCompletedCount = function() {
return this.getJobCountByTypes('completed');
};
Queue.prototype.getFailedCount = function() {
return this.getJobCountByTypes('failed');
};
Queue.prototype.getDelayedCount = function() {
return this.getJobCountByTypes('delayed');
};
Queue.prototype.getActiveCount = function() {
return this.getJobCountByTypes('active');
};
Queue.prototype.getWaitingCount = function() {
return this.getJobCountByTypes('wait', 'paused');
};
// TO BE DEPRECATED
Queue.prototype.getPausedCount = function() {
return this.getJobCountByTypes('paused');
};
Queue.prototype.getWaiting = function(start, end) {
return this.getJobs(['wait', 'paused'], start, end, true);
};
Queue.prototype.getActive = function(start, end) {
return this.getJobs('active', start, end, true);
};
Queue.prototype.getDelayed = function(start, end) {
return this.getJobs('delayed', start, end, true);
};
Queue.prototype.getCompleted = function(start, end) {
return this.getJobs('completed', start, end, false);
};
Queue.prototype.getFailed = function(start, end) {
return this.getJobs('failed', start, end, false);
};
Queue.prototype.getRanges = function(types, start, end, asc) {
start = _.isUndefined(start) ? 0 : start;
end = _.isUndefined(end) ? -1 : end;
const multi = this.multi();
const multiCommands = [];
this._commandByType(parseTypeArg(types), false, (key, command) => {
switch (command) {
case 'lrange':
if (asc) {
multiCommands.push('lrange');
multi.lrange(key, -(end + 1), -(start + 1));
} else {
multi.lrange(key, start, end);
}
break;
case 'zrange':
multiCommands.push('zrange');
if (asc) {
multi.zrange(key, start, end);
} else {
multi.zrevrange(key, start, end);
}
break;
}
});
return multi.exec().then(responses => {
let results = [];
responses.forEach((response, index) => {
const result = response[1] || [];
if (asc && multiCommands[index] === 'lrange') {
results = results.concat(result.reverse());
} else {
results = results.concat(result);
}
});
return results;
});
};
Queue.prototype.getJobs = function(types, start, end, asc) {
return this.getRanges(types, start, end, asc).then(jobIds => {
return Promise.all(jobIds.map(this.getJobFromId));
});
};
Queue.prototype.getJobLogs = function(jobId, start, end) {
start = _.isUndefined(start) ? 0 : start;
end = _.isUndefined(end) ? -1 : end;
const multi = this.multi();
const logsKey = this.toKey(jobId + ':logs');
multi.lrange(logsKey, -(end + 1), -(start + 1));
multi.llen(logsKey);
return multi.exec().then(result => ({
logs: result[0][1],
count: result[1][1]
}));
};
};
function parseTypeArg(args) {
const types = _.chain([])
.concat(args)
.join(',')
.split(/\s*,\s*/g)
.compact()
.value();
return types.length
? types
: ['waiting', 'active', 'completed', 'failed', 'delayed', 'paused'];
}