T
T
topuserman2019-12-02 00:05:52
PHP
topuserman, 2019-12-02 00:05:52

How to control the life of php consumers?

We often have to work with queue systems in php.
To develop a script that will listen to a specific queue (consumer), we simply create a script that listens to this queue in an infinite loop.
The script itself is daemonized - run via supervisor, or systemd.
If for some reason the script crashes, the system restarts.
Questions:
1. I often hear from other developers or articles that php is not meant to last.
They criticize such decisions as ours, because they are not reliable.
How then do they solve such problems?
And how is it generally accepted to solve such problems?
Can you recommend reading articles or some case studies?
2. I understand that there may be cases when the script hangs, and as a result, the consumer itself does not work, but it does not crash, so that the system restarts. What are the solutions in these cases? What can cause the script to rotate?
I thought there are two states: working and not (completed successfully or with an error).
And how do others decide?
Such a decision comes to my mind:
Each consumer is periodically obliged to submit signs of life - to record the date and time of activity in a certain database table.
And by cron, another script checks the records in the table, if some consumer has not updated the record for a long time - kill the process by pid.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
OnYourLips, 2019-12-02
@topuserman

1. Bad developers say so.
But it depends on the amount of logic in the workers. If there is a lot of it, then PHP is a good choice.
If it is not enough, but often - it is better to look towards go, for example.
2. Monitor the status of queues.

S
Stalker_RED, 2019-12-02
@Stalker_RED

Each consumer is periodically obliged to submit signs of life - to record the date and time of activity in a certain database table.

You seem to be close to inventing gearman.
If you do homemade, then usually a worker, when taking a task, notes in the table the start time and his pid, and the end time. And some watcher checks for tasks that are too long in operation and restarts.

A
Alexander Kubintsev, 2019-12-25
@akubintsev

And how do others decide?
Such a decision comes to my mind:
Each consumer is periodically obliged to submit signs of life - to record the date and time of activity in a certain database table.
And by cron, another script checks the records in the table, if some consumer has not updated the record for a long time - kill the process by pid.

Basically, you've already been answered. I just want to add a little.
Take a look at systemd. It has a mechanism to check for signs of life watchdog. You can ping yourself with a test message, for example. And restart on failure.
[Service]
...
WatchdogSec=30s
Restart=on-failure

And of course, turn on monitoring to watch through Grafana or the like.

I
Ilya, 2019-12-02
@New_Horizons

And how do others decide?

If I understand correctly, then Laravel Queues

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question