V
V
Victor08162022-04-22 00:04:22
PHP
Victor0816, 2022-04-22 00:04:22

How to properly organize the work of 2 scripts with 1 table?

Good day.
I ask for advice. Previously did not perform this

There is a table, and there are 2 php scripts
Both scripts are called simultaneously by the cron and get the information, perform the necessary actions and delete the row from the table.

Actually the question itself. How can I protect against duplicate execution?
If the 1st or 2nd script has already performed a select for a given row in the table, the other one could no longer execute
There was an idea to put the status in the database, but something tells me that this will not help
What logical solution is possible7

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Lykasov, 2022-04-22
@lykasov-aleksandr

It is not very clear what kind of processing you are doing, but you can suggest options:
1. Add a field "in processing" to the same table and ignore such records when selecting.
2. Same thing, but create a third table under the processing queue/status.
3. It may be possible to somehow divide the table between scripts (for example, the first half is one script, the other is the second).

B
Boris Korobkov, 2022-04-24
@BorisKorobkov

1. Lock the line being processed.
For PostrgeSQL - pg_try_advisory_lock() https://www.postgresql.org/docs/9.1/functions-admi...
There is no analogue for MySQL. Other locks (for example, a new field "in processing") give unpredictable results in the "Race condition" https://ru.wikipedia.org/wiki/%D0%A1%D0%BE%D1%81%D...
2 . instead of a database, use more suitable means - in this case, "queue" (for example, RabbitMQ)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question