I am using the package "cron": "^1.7.1"
.
I want to finish a task that can take longer than the scheduled cron-job.
Find below my minimum viable example:
const CronJob = require('cron').CronJob;
console.log('Before job instantiation');
const job3 = new CronJob(
'*/2 * * * * *', async () => {
if (job3.taskRunning) {
return
}
try {
//run longer task here
await setTimeout(() => {
const d = new Date();
console.log('JOB 3 - ', d);
job3.taskRunning = true
}, 6000);
} catch (err) {
console.log(err);
}
job3.taskRunning = false
}
)
console.log('After job instantiation');
job3.start();
As you can see my job runs every 2 seconds
and prints:
JOB 3 - 2019-09-01T17:06:22.006Z
JOB 3 - 2019-09-01T17:06:24.001Z
JOB 3 - 2019-09-01T17:06:26.002Z
JOB 3 - 2019-09-01T17:06:28.001Z
However, I would like to get the message only every 6 seconds
as the task needs to run 6 seconds
.
Any suggestions what I am doing wrong?