Reputation: 3091
Description:
I have node module that parses external monitoring pages and pushes daily data to db. This task can take about 10min and is scheduled to run once per day via node-cron. I have also page where you can start this task manually with extra arguments (like id or date). This is accessed by express route (lets say app.post('/manual-task', runTask)
). Cron job and http route is part of express restfull api app.
Question: I want only one running instance of this module (its actually class module) , f.e. somebody starts this task on page, but there is already running cron job for this task. It should alert user that job is already in progress. I have some ideas, but I would like to avoid bad practicies:
What is the best way and practice how to achieve that?
Thanks
Upvotes: 2
Views: 2264
Reputation: 1152
It sounds like you need a persistent way for multiple instances of your process to share state. If possible, the best approach would be system-independent (ie not something on the machine running, like pid
or env variable) so your solution could scale to multiple machines, for deploying the API separately from the cron job.
There are a few approaches depending on your requirements but they all revolve around your process:
The best approach to do this would be to have each process acquire a Lock
when it is started, perhaps using Redis as a lock. There's even a library for node.
If you're not too worried about a Race Condition, you don't have to implement the locking system. I'd still recommend something like Redis to store when a process is running, but you could also just write to a file or something similar.
Upvotes: 3