laravel-async-queue icon indicating copy to clipboard operation
laravel-async-queue copied to clipboard

Job executed synchronously when app running in PHP executable

Open jvelo opened this issue 10 years ago • 15 comments

I created an async job for a long processing tasks, it gets correctly executed asynchronously when the app is running via apache mod_php, but it gets executed synchronously when the app is running via the PHP CLI server mode.

jvelo avatar Jan 20 '15 19:01 jvelo

same problem here, any solution? if we Queue::push its awaiting..

nmkr avatar Jan 29 '15 17:01 nmkr

im on windows btw.. solution:

public function startProcess($jobId, $delay = 0)
{
    chdir($this->container['path.base']);
    pclose(popen($this->getCommand($jobId, $delay), "r"));
    //exec($this->getCommand($jobId, $delay));
}

nmkr avatar Jan 29 '15 18:01 nmkr

Tks @nmkr, now is running smoothly

grzmartins avatar Feb 14 '15 22:02 grzmartins

PHP cli is blocking, can only have 1 request. Not sure if that is the problem. But cli mode is only for testing right?

barryvdh avatar Feb 14 '15 23:02 barryvdh

And is this Linux? Or Windows? I'll try to reproduce and try the popen way.

barryvdh avatar Feb 15 '15 08:02 barryvdh

@barryvdh For me it's on linux. PHP cli is blocking but that should not be the issue, since the async queue spawns a new process, no ? And, yes the cli is mostly used in dev mode I assume as well.

jvelo avatar Feb 15 '15 17:02 jvelo

I have the same problem, but also when the app is running via apache mod_php... By replacing exec with popen and pclose, it works (I'm on windows).

bioteck avatar Feb 17 '15 08:02 bioteck

Okay will check it out. Perhaps bring the Symfony process component back.

barryvdh avatar Feb 17 '15 08:02 barryvdh

In the user contributed notes of exec in php.net (http://php.net/manual/fr/function.exec.php), there is this comment, if it can help:

This will execute $cmd in the background (no cmd window) without PHP waiting for it to finish, on both Windows and Unix.

<?php
function execInBackground($cmd) {
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r")); 
    }
    else {
        exec($cmd . " > /dev/null &");  
    }
}
?>

So it seems that popen and pclose must be used on windows for execution in background.

bioteck avatar Feb 17 '15 09:02 bioteck

Can you all try this, using the Symfony Process command?

public function startProcess($jobId, $delay = 0)
{
    $command = $this->getCommand($jobId, $delay);
    $cwd = $this->container['path.base'];
    $process = new \Symfony\Component\Process\Process($command, $cwd);
    $process->run();
}

barryvdh avatar Feb 18 '15 13:02 barryvdh

@barryvdh Works fine for me :)

bioteck avatar Feb 18 '15 13:02 bioteck

Okay cool, if the rest can confirm (can also try 0.3.x@dev) I'll push a new tag.

barryvdh avatar Feb 18 '15 13:02 barryvdh

For me it's still synchronous (PHP 5.6.5 CLI, linux).

I've tried with $process->start(); instead of $process->run(); (as per http://symfony.com/doc/current/components/process.html#running-processes-asynchronously) but still no success.

jvelo avatar Feb 18 '15 15:02 jvelo

What's odd is that it seems to be sync only for PHP commands. If I replace the command with $process = new \Symfony\Component\Process\Process("sleep(10)", $cwd); it's async as expected.

jvelo avatar Feb 18 '15 15:02 jvelo

Strange. How I did it before is also how Laravel does it: https://github.com/laravel/framework/blob/388aa6a1c3aa16a4e7f6fa850fa6f08bd3143ccd/src/Illuminate/Console/Scheduling/Event.php#L126

barryvdh avatar Feb 18 '15 15:02 barryvdh