Попробуйте использовать в качестве планировщика задач
node-schedule. Написал пример, как можно решить вашу задачу
const schedule = require('node-schedule');
const lowdb = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
// Local database for saving jobs state
const db = lowdb(new FileSync('./jobs.json'));
db.defaults({ jobs: [] }).write();
const jobManager = {
// Retrieving jobs from database and check if has active job
hasActive: () => db.get('jobs').find((job) => job.isActive).value(),
// Add active job
add: () => {
const [lastJob] = db.get('jobs').takeRight(1).value();
db.get('jobs').push({ id: lastJob ? lastJob.id + 1 : 0, isActive: true }).write();
},
// Finish active job
finish: () => {
const [lastJob] = db.get('jobs').takeRight(1).value();
db.get('jobs').find({ id: lastJob.id }).assign({ isActive: false }).write();
}
};
// Your heavy async job
const job = async () => {
if (!jobManager.hasActive()) {
jobManager.add()
console.log('Heavy job ...');
jobManager.finish()
} else {
return 'Skipped, because you have active job now. Expect for current job finish';
}
};
// Recurrence job at every 5 minutes
schedule.scheduleJob('*/5 * * * *', async () => {
job().then((response) => console.log(response));
});