Skip to content

Commit

Permalink
feat(bull): add clean jobs button for completed and failed (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored May 3, 2024
1 parent 956c5f6 commit e62aef0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
72 changes: 47 additions & 25 deletions public/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,38 +210,60 @@ $(document).ready(() => {
queueState,
};

$bulkActionContainer.each((index, value) => {
const isChecked = $(value).find('[name=jobChecked]').is(':checked');
const id = encodeURIComponent($(value).find('[name=jobId]').val());
if (action !== 'clean') {
$bulkActionContainer.each((index, value) => {
const isChecked = $(value).find('[name=jobChecked]').is(':checked');
const id = encodeURIComponent($(value).find('[name=jobId]').val());

if (isChecked) {
data.jobs.push(id);
}
});
}

if (isChecked) {
data.jobs.push(id);
}
});
const count = action === 'clean' ? 1000 : data.jobs.length;

const r = window.confirm(
`${capitalize(action)} ${data.jobs.length} ${
data.jobs.length > 1 ? 'jobs' : 'job'
`${capitalize(action)} ${count} ${
count > 1 ? 'jobs' : 'job'
} in queue "${queueHost}/${queueName}"?`
);
if (r) {
$.ajax({
method: action === 'remove' ? 'POST' : 'PATCH',
url: `${basePath}/api/queue/${encodeURIComponent(
queueHost
)}/${encodeURIComponent(queueName)}/${
action === 'promote' ? 'delayed/' : ''
}job/bulk`,
data: JSON.stringify(data),
contentType: 'application/json',
})
.done(() => {
window.location.reload();
if (action === 'clean') {
$.ajax({
method: 'DELETE',
url: `${basePath}/api/queue/${encodeURIComponent(
queueHost
)}/${encodeURIComponent(queueName)}/jobs/bulk`,
data: JSON.stringify(data),
contentType: 'application/json',
})
.fail((jqXHR) => {
window.alert(`Request failed, check console for error.`);
console.error(jqXHR.responseText);
});
.done(() => {
window.location.reload();
})
.fail((jqXHR) => {
window.alert(`Request failed, check console for error.`);
console.error(jqXHR.responseText);
});
} else {
$.ajax({
method: action === 'remove' ? 'POST' : 'PATCH',
url: `${basePath}/api/queue/${encodeURIComponent(
queueHost
)}/${encodeURIComponent(queueName)}/${
action === 'promote' ? 'delayed/' : ''
}job/bulk`,
data: JSON.stringify(data),
contentType: 'application/json',
})
.done(() => {
window.location.reload();
})
.fail((jqXHR) => {
window.alert(`Request failed, check console for error.`);
console.error(jqXHR.responseText);
});
}
} else {
$(this).prop('disabled', false);
}
Expand Down
7 changes: 5 additions & 2 deletions src/server/views/api/bulkAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require('lodash');

const ACTIONS = ['remove', 'retry', 'promote'];
const ACTIONS = ['clean', 'remove', 'retry', 'promote'];

function bulkAction(action) {
return async function handler(req, res) {
Expand All @@ -19,7 +19,7 @@ function bulkAction(action) {
const {jobs, queueState} = req.body;

try {
if (!_.isEmpty(jobs)) {
if (!_.isEmpty(jobs) && job.length > 0) {
const jobsPromises = jobs.map((id) =>
queue.getJob(decodeURIComponent(id))
);
Expand All @@ -39,6 +39,9 @@ function bulkAction(action) {
: fetchedJobs.map((job) => job[action]());
await Promise.all(actionPromises);
return res.sendStatus(200);
} else if (action === 'clean') {
await queue.clean(1000, queueState);
return res.sendStatus(200);
}
} catch (e) {
const body = {
Expand Down
3 changes: 3 additions & 0 deletions src/server/views/api/bulkJobsClean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const bulkAction = require('./bulkAction');

module.exports = bulkAction('clean');
2 changes: 2 additions & 0 deletions src/server/views/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const jobRetry = require('./jobRetry');
const jobRemove = require('./jobRemove');
const jobDataUpdate = require('./jobDataUpdate');
const repeatableJobRemove = require('./repeatableJobRemove');
const bulkJobsClean = require('./bulkJobsClean');
const bulkJobsPromote = require('./bulkJobsPromote');
const bulkJobsRemove = require('./bulkJobsRemove');
const bulkJobsRetry = require('./bulkJobsRetry');
Expand All @@ -30,5 +31,6 @@ router.patch('/queue/:queueHost/:queueName/job/:id', jobRetry);
router.put('/queue/:queueHost/:queueName/pause', queuePause);
router.put('/queue/:queueHost/:queueName/resume', queueResume);
router.delete('/queue/:queueHost/:queueName/job/:id', jobRemove);
router.delete('/queue/:queueHost/:queueName/jobs/bulk', bulkJobsClean);

module.exports = router;
6 changes: 6 additions & 0 deletions src/server/views/dashboard/queueJobsByState.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ async function _html(req, res) {
state === 'failed' ||
(state === 'delayed' && !queue.IS_BEE)
);
const disableClean = !(
state === 'failed' ||
state === 'completed' ||
!queue.IS_BULL
);

return res.render('dashboard/templates/queueJobsByState', {
basePath,
Expand All @@ -164,6 +169,7 @@ async function _html(req, res) {
disablePagination:
queue.IS_BEE && (state === 'succeeded' || state === 'failed'),
disableOrdering: queue.IS_BEE,
disableClean,
disablePromote,
disableRetry,
currentPage: page,
Expand Down
6 changes: 6 additions & 0 deletions src/server/views/dashboard/templates/queueJobsByState.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
data-queue-state="{{ state }}" class="js-bulk-action btn btn-danger">
Remove Jobs
</button>
{{#unless disableClean}}
<button type="button" data-action="clean" data-queue-name="{{ queueName }}" data-queue-host="{{ queueHost }}"
data-queue-state="{{ state }}" class="js-bulk-action btn btn-danger">
Clean 1000 Jobs
</button>
{{/unless}}
{{#unless disableRetry}}
<button type="button" data-action="retry" data-queue-name="{{ queueName }}" data-queue-host="{{ queueHost }}"
data-queue-state="{{ state }}" class="js-bulk-action btn btn-success">
Expand Down

0 comments on commit e62aef0

Please sign in to comment.