-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobProcess.php
110 lines (96 loc) · 2.43 KB
/
JobProcess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace yiicod\jobqueue;
use Illuminate\Queue\Jobs\Job;
use SfCod\QueueBundle\Job\MongoJob;
use Symfony\Component\Process\Process;
use Yii;
/**
* Class JobProcess
*
* @author Orlov Alexey <aaorlov88@gmail.com>
*
* @package SfCod\QueueBundle
*/
class JobProcess
{
/**
* @var string
*/
public $binPath;
/**
* @var string
*/
public $scriptName;
/**
* @var string
*/
public $binary;
/**
* @var string
*/
public $binaryArgs;
public function __construct(
string $scriptName = 'yii',
string $binPath = null,
string $binary = 'php',
string $binaryArgs = '')
{
$this->scriptName = $scriptName;
$this->binPath = $binPath;
$this->binary = $binary;
$this->binaryArgs = $binaryArgs;
}
/**
* Get the Artisan process for the job id.
*
* @param Job|MongoJob $job
* @param string $connectionName
*
* @return Process
*/
public function getProcess(Job $job, string $connectionName): Process
{
$cmd = '%s %s job-queue/process %s --connection=%s --queue=%s';
$cmd = $this->getBackgroundCommand($cmd);
$cmd = sprintf($cmd, $this->getPhpBinary(), $this->scriptName, (string)$job->getJobId(), $connectionName, $job->getQueue());
$alias = '';
if (is_null($this->binPath)) {
if (file_exists(Yii::getAlias('@app/yii'))) {
$alias = '@app';
} elseif (file_exists(Yii::getAlias('@app/../yii'))) {
$alias = '@app/../';
}
}
return new Process($cmd, Yii::getAlias($this->binPath ?? $alias));
}
/**
* @param $cmd
*
* @return string
*/
protected function getBackgroundCommand(string $cmd): string
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
return 'start /B ' . $cmd . ' > NUL';
} else {
return $cmd . ' > /dev/null 2>&1 &';
}
}
/**
* Get the escaped PHP Binary from the configuration
*
* @return string
*/
protected function getPhpBinary(): string
{
$path = $this->binary;
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
$path = escapeshellarg($path);
}
$args = $this->binaryArgs;
if (is_array($args)) {
$args = implode(' ', $args);
}
return trim($path . ' ' . $args);
}
}