Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice for handling concurrent jobs in a multi-instance Express app #1015

Open
kbzowski opened this issue Dec 3, 2023 · 1 comment
Assignees

Comments

@kbzowski
Copy link

kbzowski commented Dec 3, 2023

I am currently using Nginx Unit to run multiple instances of an Express application. I have several cron jobs that perform database operations (using node-cron). These jobs need to be managed in such a way that they do not run simultaneously to avoid conflicts or data integrity issues. I am looking for the best practices or solutions to ensure that cron jobs are handled by only one process at a time.

Is there a way to designate an instance of the application under nginx unit to handle specific task (can I pass process id or some other value dynamically to identify first/main process)?

The only alternative I can see is to develop some kind of database locking mechanism that only allows one instance to do the task.

@tippexs
Copy link
Contributor

tippexs commented Dec 6, 2023

Hi @kbzowski
sorry for the late response but I have questions.

Are we talking about the use of something like

var cron = require('node-cron');

cron.schedule('1-5 * * * *', () => {
  console.log('running every minute to 1 from 5');
});

inside of your express application?

If so the trigger for the cron comes from inside the application. Even if you have multiple processes of this application instance it should be quite easy to have a lock for crons by introducing lock-files, db or keyvalue-store entries.

I would check if a given task / cron is currently locked as a first action in my cron.schedule body.

Another option is to have a deticated application instance just for your crons, but this might not work at scale.

@tippexs tippexs self-assigned this Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants