Skip to content

Commit 7ed454f

Browse files
committed
Use exec instead of Process
Simplify it all a bit. Use Process for finding the binary. Thanks @GrahamCampbell
1 parent 72819f6 commit 7ed454f

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"php": ">=5.3.0",
1212
"illuminate/support": "~4.0",
1313
"illuminate/console": "~4.0",
14-
"sebastian/environment": "~1.0",
1514
"symfony/process": "~2.3"
1615
},
1716
"autoload": {

src/AsyncQueue.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<?php
2-
32
namespace Barryvdh\Queue;
43

54
use Barryvdh\Queue\Models\Job;
65
use Illuminate\Queue\Queue;
76
use Illuminate\Queue\QueueInterface;
8-
use SebastianBergmann\Environment\Runtime;
9-
use Symfony\Component\Process\Process;
7+
use Symfony\Component\Process\PhpExecutableFinder;
108

119
class AsyncQueue extends Queue implements QueueInterface
1210
{
11+
/** @var PhpExecutableFinder */
12+
protected $phpfinder;
13+
14+
public function __construct(){
15+
$this->phpfinder = new PhpExecutableFinder();
16+
}
17+
1318
/**
1419
* Push a new job onto the queue.
1520
*
@@ -60,11 +65,8 @@ public function storeJob($job, $data, $delay = 0)
6065
*/
6166
public function startProcess($jobId)
6267
{
63-
$command = $this->getCommand($jobId);
64-
$cwd = $this->container['path.base'];
65-
66-
$process = new Process($command, $cwd);
67-
$process->run();
68+
chdir($this->container['path.base']);
69+
exec($this->getCommand($jobId));
6870
}
6971

7072
/**
@@ -76,29 +78,29 @@ public function startProcess($jobId)
7678
*/
7779
protected function getCommand($jobId)
7880
{
79-
$string = $this->getBinary().' artisan queue:async %d --env=%s ';
80-
81-
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
82-
$string = 'start /B '.$string.' > NUL';
83-
} else {
84-
$string = 'nohup '.$string.' > /dev/null 2>&1 &';
85-
}
81+
$cmd = '%s artisan queue:async %d --env=%s';
82+
$cmd = $this->getBackgroundCommand($cmd);
8683

84+
$php = $this->getPhpBinary();
8785
$environment = $this->container->environment();
8886

89-
return sprintf($string, $jobId, $environment);
87+
return sprintf($cmd, $php, $jobId, $environment);
9088
}
9189

92-
/**
93-
* Get the php binary path.
94-
*
95-
* @return string
96-
*/
97-
protected function getBinary()
90+
protected function getPhpBinary()
9891
{
99-
$runtime = new Runtime();
92+
$path = escapeshellarg($this->phpfinder->find(false));
93+
$args = implode(' ', $this->phpfinder->findArguments());
94+
return trim($path.' '.$args);
95+
}
10096

101-
return $runtime->getBinary();
97+
protected function getBackgroundCommand($cmd)
98+
{
99+
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
100+
return 'start /B '.$cmd.' > NUL';
101+
} else {
102+
return $cmd.' > /dev/null 2>&1 &';
103+
}
102104
}
103105

104106
/**

0 commit comments

Comments
 (0)