-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathbulkAction.js
More file actions
56 lines (49 loc) · 1.57 KB
/
bulkAction.js
File metadata and controls
56 lines (49 loc) · 1.57 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
const includes = require('lodash.includes');
const isEmpty = require('lodash.isempty');
const ACTIONS = ['remove', 'retry', 'promote'];
function bulkAction(action) {
return async function handler(req, res) {
if (!includes(ACTIONS, action)) {
res.status(401).send({
error: 'unauthorized action',
details: `action ${action} not permitted`,
});
}
const {queueName, queueHost} = req.params;
const {Queues} = req.app.locals;
const queue = await Queues.get(queueName, queueHost);
if (!queue) return res.status(404).send({error: 'queue not found'});
const {jobs, queueState} = req.body;
try {
if (!isEmpty(jobs)) {
const jobsPromises = jobs.map((id) =>
queue.getJob(decodeURIComponent(id))
);
const fetchedJobs = await Promise.all(jobsPromises);
const actionPromises =
action === 'retry'
? fetchedJobs.map((job) => {
if (
queueState === 'failed' &&
typeof job.retry === 'function'
) {
return job.retry();
} else {
return Queues.set(queue, job.data, job.name);
}
})
: fetchedJobs.map((job) => job[action]());
await Promise.all(actionPromises);
return res.sendStatus(200);
}
} catch (e) {
const body = {
error: 'queue error',
details: e.stack,
};
return res.status(500).send(body);
}
return res.sendStatus(200);
};
}
module.exports = bulkAction;