How can I handle this mutex operation?

Hello, I have some question about a probably issue running right now, and maybe I'm not seeing clearly how to solve it..

I have on nodejs, and mysql 3 tables
Table 1 is full of records (10.000) with latitude and longitude and a push notification id
Table 2 has a record with a latitude and longitude that is inserted on a form.
Table 3 has the same record that table 2 and it's stored when the next operation ends the job:

A schedule every 3 minutes inspects records that are on table 2 but not on table 3 and will compare distances with table 1
When distance < 3 km then send a push notification to that user.
After all record were searched, it will store the id on table 3, so schedule wont execute that again, or at least, that's the idea..

The problem comes when multiple operations are running at the same time because schedule will execute it, (not only from new post, but also same operation that has not finished yet) and then mysql crash..

So I need to figure out how can I block that API when it's already running..

I was thinking on a new table like: running operations with taskid and 1/0 but in case of crash it will stop new jobs until I manually change it..

I hope I made myself clear and thanks :slight_smile:

We have another scheduler which periodically checks if this tasks table seems to be stuck, then mark it as available or not running again.

In our case, no job runs for more than 30mins. So our revival scheduler checks for tasks running for more than an hour.

1 Like

Maybe using a Bull queue or something to process sequentially, but I don't know anything about that