Skip to content

Commit

Permalink
feat: delete job by body-path
Browse files Browse the repository at this point in the history
  • Loading branch information
dhohirpradana committed Oct 30, 2023
1 parent da0d273 commit ab7222b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
42 changes: 30 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,40 @@ app.delete('/job/:id', (req, res) => {
saveJobsToFile();
});

// delete all job where key = value
app.delete('/job/:key/:value', (req, res) => {
const key = req.params.key;
// delete all job where job.data.path = value
app.delete('/job/body-path/:value', (req, res) => {
const value = req.params.value;
// delete job where key = value loop
for (var i = 0; i < jobsString.length; i++) {
if (jobsString[i][key] == value) {
// stop cron job
jobs[i].task.stop();
jobs.splice(i, 1);
jobsString.splice(i, 1);
i--;
const decodedValue = decodeURIComponent(value);
var jobsDeleted = 0;

// check exist job with path body = value using filter
const jobsFilter = jobsString.filter((job) => job.data.body.path === decodedValue);

if (jobsFilter.length == 0) {
return res.status(404).json({ message: `Job with path body ${decodedValue} not found` });
}

function removeJob(index) {
try {
jobs[index].task.stop();
jobs.splice(index, 1);
jobsString.splice(index, 1);
jobsDeleted++;
} catch (error) {
console.log("Retry remove job");
removeJob();
}
}

// loop jobsFilter to delete job
jobsFilter.forEach((job) => {
// get index job
index = jobsString.indexOf(job);
removeJob(index);
});

saveJobsToFile();
return res.json({ success: true, message: `Job ${key} = ${value} deleted` });
return res.json({ message: `Job with path body ${decodedValue} deleted`, total: jobsDeleted });
});

// update job
Expand Down
2 changes: 1 addition & 1 deletion jobsData.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":"05613ec0-b759-4db4-9c7e-cda7c5a85b30","type":"httpRequest","data":{"url":"http://10.10.65.5:3666/execute","method":"POST","headers":{},"body":{"user":"yusuf","path":"jobs/scheduler.ipynb","scheduler-id":"ZFvwuR50SSuXdhO"}},"schedule":"* * * * *","created_at":"2023-10-30T11:20:40.363Z","status":"running"},{"id":"73f7f07f-d180-4026-95a2-4c577f67dfb3","type":"httpRequest","data":{"url":"http://google.com","method":"GET","headers":{},"body":{}},"schedule":"* 1 * * *","created_at":"2023-10-30T11:28:27.414Z","status":"running"},{"id":"15e45f3d-3732-4354-becf-45c20a6a9491","type":"httpRequest","data":{"url":"http://10.10.65.5:3666/execute","method":"POST","headers":{},"body":{"user":"yusuf","path":"jobs/scheduler.ipynb","scheduler-id":"ZFvwuR50SSuXdhO"}},"schedule":"* * * * *","created_at":"2023-10-30T11:29:10.645Z","status":"running"}]
[]

0 comments on commit ab7222b

Please sign in to comment.