admin管理员组文章数量:1022777
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?
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?
Share Improve this question asked Sep 1, 2019 at 17:25 Carol.KarCarol.Kar 5,23538 gold badges148 silver badges298 bronze badges1 Answer
Reset to default 5Correctly initialising run identifier fixes the problem. Create global variable taskRunning initialised to saying not running. Then just before calling long running task make it in run state. Before long running task finishes again set it to not running. Added extra console log in case next fire time occurs before long running task is pleted and returning without executing it.
const CronJob = require('cron').CronJob;
taskRunning=false
console.log('Before job instantiation');
const job3 = new CronJob(
'*/2 * * * * *', async () => {
if (taskRunning) {
console.log('returning')
return
}
taskRunning=true
try {
//run longer task here
await setTimeout(() => {
const d = new Date();
console.log('JOB 3 - ', d);
taskRunning = false
}, 6000);
} catch (err) {
console.log(err);
}
}
)
console.log('After job instantiation');
job3.start();
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?
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?
Share Improve this question asked Sep 1, 2019 at 17:25 Carol.KarCarol.Kar 5,23538 gold badges148 silver badges298 bronze badges1 Answer
Reset to default 5Correctly initialising run identifier fixes the problem. Create global variable taskRunning initialised to saying not running. Then just before calling long running task make it in run state. Before long running task finishes again set it to not running. Added extra console log in case next fire time occurs before long running task is pleted and returning without executing it.
const CronJob = require('cron').CronJob;
taskRunning=false
console.log('Before job instantiation');
const job3 = new CronJob(
'*/2 * * * * *', async () => {
if (taskRunning) {
console.log('returning')
return
}
taskRunning=true
try {
//run longer task here
await setTimeout(() => {
const d = new Date();
console.log('JOB 3 - ', d);
taskRunning = false
}, 6000);
} catch (err) {
console.log(err);
}
}
)
console.log('After job instantiation');
job3.start();
本文标签: javascriptNodecron Wait for job finished to execute the next oneStack Overflow
版权声明:本文标题:javascript - Node-cron: Wait for job finished to execute the next one - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745510934a2153822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论