There is an application that sends to the back of a different type of task, these tasks are packed into a queue and every 15 minutes cron executes them. At the moment there is a simple notification that cron started processing the task queue, everything is working. There was a desire to listen to the execution status, of course, you can read the log, but I want real-time from the browser.

The way of thinking :

  1. We send a puzzle to back.
  2. We write it to the type log: php run.php AmoParser mode=base_state > AmoParser_thread_$thread
  3. On the client, every 5 seconds we send AJAX and read the problem log

Here, as if everything is clear, we read the log - we are broadcasting to the browser, upon completion, to send a flag to the log that the broadcast is over (or how?). But how to be if at the same time 10 tasks are performed, I would like to look at the state of everyone as it is.

What if the log is very large or the task is slow? if I translate everything to the browser, it will somehow fall off over time, it’s necessary to control the issue and how to organize the streaming of the log file.

what do you advise?

  • As an option in the browser show only a piece of the log. And about how to stop the broadcast, you can make a condition on the lack of new data timeout. - Evgeniy A
  • No new data for what period? and if there is no data but the task is performed? You can certainly check the PID . A piece of log in the sense of the last 200 lines? type cat <log_file> | tail -200 cat <log_file> | tail -200 - Redr01d

1 answer 1

In general, I did not wait for an answer and decided to solve the problem as I knew, I will not describe the code, I will describe the logic.

  1. When the user presses the start button, an ajax request occurs that accesses the start controller, the start timestamp is passed in the parameters, the model runs the puzzle into the back and starts writing an action log with the name of the timestamp parameter. The answer goes back to the user that the job is started and the pid of the process of which writes a log

  2. This response initializes a timer that every 2 seconds sends a request with a log name parameter and an offset from the position from which to read the log. back receives the answer with the log text and the log size, the log size at the next request is the offset parameters, so I always get a fresh batch of log slice and not the entire log.

  3. With each request, I save the size of the log and compare it with the previous one, if the size is different, I add the answer to the output, if it does not, I check the status of the PID, if it is not there, I display the message that the process worked.

Thus, I get a working monitor for the state of the job without the need to read from the mail or read the log on the server. It turned out quite nice and convenient monitor: enter image description here