Loopback: How to run task in only one worker when deploying on cluster

Created on 6 Sep 2016  路  4Comments  路  Source: strongloop/loopback

I have a schedule task which run once per day. I write the code in boot/schedule.js. When I deploy on cluster, each worker will execute the same code.

I have red the How-to cluster node.js in Production, I cannot figure out how to achieve this.

I have tried the solution. However, strong-pm start a new worker with a new id if the original worker exit, which means the worker id will changed.

How can I ensure that there is one worker running those code.

Most helpful comment

@JoeShi Do you control your production environment? Can you create a cron job? My recommendation is to create an HTTP route with a handler that will perform the desired task. Then you can schedule a periodic cron job to call this HTTP route. Because each HTTP request is always handled by a single worker, your periodic task will be run only once.

Alternatively, you can implement some sort of a locking scheme using database or filesystem, which will be used by workers to ensure the task is run only once. However, this is more fragile than the cron-based approach, because you need to take care of error cases like what happens when the worker does not release the lock.

All 4 comments

@superkhau @bajtos I feel this is more like a strong-cluster-control or strong-pm issue other than loopback, I find the article mentioned above is written by Alex Gorbatchev, but not sure is he still working with strongloop or is it fine to mention him here. Any suggestion? Thanks!

And @JoeShi could you fork https://github.com/strongloop/loopback-sandbox and replace with your code for a better debug? Thanks!

@JoeShi Please do not ask questions here, we mainly use GitHub for tracking features/bugs, see https://github.com/strongloop/loopback/wiki/Questions.

That said, please post your question at https://github.com/strongloop/strong-cluster-control/issues. @sam-github I assume you can take it from here.

@JoeShi Do you control your production environment? Can you create a cron job? My recommendation is to create an HTTP route with a handler that will perform the desired task. Then you can schedule a periodic cron job to call this HTTP route. Because each HTTP request is always handled by a single worker, your periodic task will be run only once.

Alternatively, you can implement some sort of a locking scheme using database or filesystem, which will be used by workers to ensure the task is run only once. However, this is more fragile than the cron-based approach, because you need to take care of error cases like what happens when the worker does not release the lock.

Thank you @bajtos . Seems like the cron job is the proper way so far.

Was this page helpful?
0 / 5 - 0 ratings