P
P
Pavel Novikov2018-07-10 13:15:53
symfony
Pavel Novikov, 2018-07-10 13:15:53

How to execute a PHP script in the background after submitting a form once a minute for N minutes?

Good afternoon,
there is such a task - a client can order a rental of a box for things that is equipped with a "smart lock" through the site. When placing an order, the order process itself is divided into two parallel processes - one process makes the necessary changes to the database (creates a new record in the Orders table, changes the status of a box in the Boxes table, etc.), the second process must, within a certain interval (for example, in tech 5 minutes) once a minute, execute a request via a third-party API to the gateway that controls smart locks in order to receive a PIN code from it. If the PIN is received, change some entries in the database again, if after 5 times (1 time per minute for 5 times) the PIN code is not received - do nothing. Does PHP (or Symphony) have any means to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BoShurik, 2018-07-10
@BoShurik

I would use queues ( RabbitMQ , Gearman , beanstalkd )
When submitting the form, we execute the first process (which "creates a new record in the Orders table, changes the box status in the Boxes table, etc"). After it, we add a task to check the API to the queue.
The consumer receives the task, checks the API and, if everything is OK, then performs the necessary actions, otherwise it copies the task (increasing the counter of attempts by one) and sends it to the queue again in a minute. If the counter has already exceeded 5, it ignores.
Instead of queues, you can create a console command and run it in the background.
In the command already use the option suggested by nozzy

N
nozzy, 2018-07-10
@nozzy

$i=1;
while($i<=5) {
   // call api     
   sleep(60);
   $i++;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question