-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathbulkAction.js
More file actions
40 lines (33 loc) · 1.08 KB
/
bulkAction.js
File metadata and controls
40 lines (33 loc) · 1.08 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
const _ = require('lodash');
function bulkAction(action) {
return async function handler(req, res) {
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'});
if (!queue.isActionSupported(action)) {
return res.status(401).send({
error: 'unauthorized action',
details: `queue does not support action ${action}`
});
}
const {jobs} = req.body;
try {
if (!_.isEmpty(jobs)) {
const jobsPromises = jobs.map((id) => queue.getJob(decodeURIComponent(id)));
const fetchedJobs = await Promise.all(jobsPromises);
const actionPromises = 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;