T
T
topuserman2021-08-04 23:16:09
PHP
topuserman, 2021-08-04 23:16:09

RabbitMQ: how to properly perform heavy tasks?

Hello!
At our project, jobs are sent to generate reports in the rabbitmq queue.
The queue itself is served by a php-consumer (works via php-amqplib).

The problem is that generating reports can take 10 minutes, some even an hour. The

consumer works as follows:
it takes N messages from the queue and starts generating reports immediately.

At the moment when the consumer is busy generating a report, as I understand it, rabbitmq sends some checks for the viability of the consumer - heartbeat . By default, if the consumer does not respond for 60 seconds, then at the next iteration an error occurs that the connection is lost.

I have two questions:

1. What is the risk of disabling - heartbeat in rabbitmq?I read it here but still don't understand.
2. How is it customary to handle the heavy tasks that the costumer receives? maybe they should not be launched immediately in the consumer itself? if you run it as a separate process, then the question immediately arises, what to do if an error occurs in the process of generating a report? the message from the queue is read and sent to ack.

best practices needed))

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
Developer, 2021-08-04
@samodum

The consumer itself does not have to process tasks. Its main purpose is to unload the queue and send tasks further, for example, to a database or to other servers through a balancer. The first way is best.
Next, a worker looks at the unprocessed tasks in the database and slowly processes them. Processed tasks are flagged.

S
Sergey, 2021-08-04
@yarkin

There was a similar problem, the sishny client in one flow was not able normally in heartbeat. When disabling heartbeats at the AMQP protocol level, there were problems with "hanging clients" on the RabbitMQ side, when the application exited unexpectedly. Setting TCP keepalive helped to solve this problem.
I don’t know exactly how modern php works, but if it is possible to keep a separate thread to maintain an AMQP connection with heartbeat enabled, then it will be better than via TCP keepalive. It's better to send ack back after the task is finished processing.

D
Dmitry, 2021-08-05
@dmtrrr

This looks like a problem with the php-amqplib library, php still doesn't know how to multithread?
You can create a process that will generate a report.

P
part_os, 2021-08-12
@part_os

And how do you maintain a connection to the database if there are no requests to it during the report generation? We do a ping to the database, as well as to the rebitt, thereby reporting that the cosumer is alive and working.

A
Anton Minakov, 2021-08-12
@int128

Partial solution https://github.com/php-enqueue/enqueue-dev/blob/ma...

E
Elena Stepanova, 2021-08-12
@Insolita

php-amqplib nerdy, better amqp extension from pecl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question