How to notify user when async task completes in PHP / Windows

Consider the diagram below:

the workflow

thoughts

  • the user can perform a single file / package upload (I know about the HTTP specification), batch I mean that someone can visually send multiple files at a time.
  • The image download service is designed to create a child process (image handler) for the sent file.

the questions

  1. then which communication approach between the client and server is best for sending notifications from these child processes back to the client?
  2. What is the best approach to create a child process? I read about proc_open , curl approaches. I did not understand how to use message queues (if applicable at all).

notes

just said that I am using Windows ( Windows Server 2008 ) and XAMPP

+6
source share
5 answers

User Uploads Image. After this step you can create a task. One Taks - one row of the task table in your database.

 class Task { const STATUS_PENDING = 'pending' const STATUS_ERROR = 'error' const STATUS_FINISHED = 'finished' private $userid; private $taskData = array(); public function run() { // create process } public function update() { // update the status an other changes you need to the database // or any other storage you use. } public function getStatus(); } class TaskManager { const MAX_TASKS = 5; private $tasks = array(); public function addTaks(); public function start() { foreach ($tasks as $task) { $task->run(); } } } 

On the server side, you should think about how to work / organize your tasks. It depends on your needs. With the power of TaskManager, you can control your process. It is very important that not 100 processes run in parallel.

Now the client side can easily request a task table with a user ID to verify the pending task. If the status is completed or an error, you can give the user excellent feedback. You can poll this information every five seconds. The interval depends on your needs. Polling for five seconds may be odd for your browser. It’s a good idea that the client receives the status instead of the server, sends the status to the client. If it is important that the server sends status, you should use web sockets. The backend may be the same.

Here is a simple survey example.

 setInterval(function(){ $.ajax({ url: "server", success: function(data){ //Update your dashboard gauge salesGauge.setValue(data.value); }, dataType: "json"}); }, 30000); 

proc_open is only good if you have some additional options. With the power of proc_open (pay attention to the pipes) and in combination with stream_set_blocking. You can write asynchronous task processing. If you do not need a special exec , that will be enough.

Here is an example without locking.

 class Task { private $process = null; private $pipes = array(); private $status = 'pending' public function run() { $descriptor = array ( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w") ); $cmd = "/path/to/progam --with --some-arguments" $this->process = proc_open($command, $descriptor, $this->pipes); stream_set_blocking($this->pipes[1], 0); } public function close() { foreach($this->pipes as $pipe) { fclose($pipe); } $exitStatus = proc_close($this->process); $this->process = NULL; return $exitStatus; } } 

Hope this gives you some inspiration to solve your problem. For real message queues, you can use what you want. I am a fan of http://kr.imtqy.com/beanstalkd/ Now I will focus on solutions and ideas, because this question is more complicated, and I can write another 100 pages. If you have questions about konkrete, you can delete the line.

+4
source

in ASP.NET this is easy to do. Here is an example of its implementation in ASP.NET. http://brockallen.com/2013/07/27/implementing-async-http-modules-in-asp-net-using-tpls-task-api/

But since you want this to be done in PHP / windows, you can do this in several ways.

Why can't you do image processing when you are finished, update a field in the database table or session, and save a javascript timer from your HTML page that will poll the database / session variable of possible status? Once it gets the completed status from db / session (which has been updated by the image processing logic), can your javascript timer take further action?

Hope this helps.

+5
source

For proper client-server communication during the file upload process, we usually use AJAX (Async JS) in jQuery. Send all the necessary data, which in your case will be files. Perform operations on the server (PHP) and after that send the json response back to the client for notification purposes. Trick - you need to get the answer from your child process in the success function of AJAX. Only after successful completion will notifications be sent.

+1
source

You can watch WAMP .

He already has PHP and JS libs

Sinse not all browsers support sockets, you should use them carefully or provide some kind of reserve for older browsers

+1
source

The best way for you is to use message queues like ActiveMQ, RabbitMQ, etc. Then the following steps will be performed:

  • Upload the file to the server, ideally nginx (PHP is slow and cannot handle multiple downloads efficiently).
  • Call the PHP code, which will prepare all the information and save it as a task in MQ. It also returns a unique path for subscribing to events (push nginx module), which will be used by the browser. It can be a WebSocket or a long polling channel, choose the one that you are well aware of.
  • PHP Daemon connects to MQ and performs multi-threaded image processing, sends signals back to the push nginx module to notify the browser of the completion of the task.
  • Display a notification window for the user and unsubscribe from the channel.
+1
source

Source: https://habr.com/ru/post/958142/


All Articles