InquisitiveTom
InquisitiveTom

Reputation: 483

Best Way to Handle Long Running Process Nodejs

I am developing an app that updates products in Shopify. On the React front end, the user can "Activate" the app and run the logic that checks and updates every product in their online store.

I'm also running a cron-job at night that runs the same process to update the user's products.

This process can take up to 10+ minutes so I'm returning a 202 response immediately.

Here's the problem: If the user "Activates" the app while the cron-job is running, then I get throttled by Shopify for hitting their API too many times (2 identical product updating processes running side by side).

What I'm struggling to create is a system that prevents the user on the front end from running the process if A) the cron-job is running or B) they already activated it and it's running.

Basically, if backend process is running, prevent the user from being able to activate it.

Any thoughts on how to best approach this?

Through my research, I'm considering the following solution:

Adding a column in my DB for "can_activate." Whenever the process starts, it updates the DB to false. Then at the end it sets the DB to true. Then I'll use polling from node to DB to check when it updates. From node to React I'll use websockets to check and update state that prevents the button from being clickable or not.

Am I overcomplicating this?

Upvotes: 1

Views: 842

Answers (1)

David Lazar
David Lazar

Reputation: 11427

Most people run what are commonly termed background jobs. So you activate a background job to run when your web app receives a command to do stuff that takes a while. So if you have a scheduler at your disposal (a CRON-like utility) you spawn these jobs at a time(s). To check if a job is running or not, your job runner tells you. This way, if your App has a button that activates the task or job, you can choose to check if one is already running or not.

I have been doing that pattern with Shopify for 12+ years now and it works perfectly. Making tables with flags is clunky, but if that is all you can do, so be it.

I am sure the Node world has all the tools you need to process background jobs. It is like the 101 tool of cloud computing. emphasized text

Upvotes: 1

Related Questions