I'm working with node and typescript, using node-cron 2.0 to schedule a background operation to run every hour.
cron.schedule("0 0 * * * *", () => {
purgeResponsesSurveys();
});
I'm concerned about what happens if the method doesn't finish within 1 hour since I don't want two instances of my method to run at the same time.
What are the best practices to prevent the scheduler from invoking the function purgeResponsesSurveys if it is already running from the previous hourly invocation?
You can use a
Semaphoreto prevent parallel calls.You will need to know when
purgeResponsesSurveysis done. So if it's asynchronous you will need to returnPromiseor receive acallbackthat will be called whenpurgeResponsesSurveysis done.I used semaphore npm package. Here is a small example/simulation.