1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Service; |
4
|
|
|
|
5
|
|
|
use SfCod\QueueBundle\Job\JobContractInterface; |
6
|
|
|
use SfCod\QueueBundle\Worker\Options; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class JobProcess |
11
|
|
|
* |
12
|
|
|
* @author Virchenko Maksim <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @package SfCod\QueueBundle |
15
|
|
|
*/ |
16
|
|
|
class JobProcess |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $binPath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $scriptName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $binary; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $binaryArgs; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $environment; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* JobProcess constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $scriptName |
47
|
|
|
* @param string $binPath |
48
|
|
|
* @param string $environment |
49
|
|
|
* @param string $binary |
50
|
|
|
* @param string $binaryArgs |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
string $scriptName, |
54
|
|
|
string $binPath, |
55
|
|
|
string $environment = 'prod', |
56
|
|
|
string $binary = 'php', |
57
|
|
|
string $binaryArgs = '') |
58
|
|
|
{ |
59
|
|
|
$this->scriptName = $scriptName; |
60
|
|
|
$this->binPath = $binPath; |
61
|
|
|
$this->environment = $environment; |
62
|
|
|
$this->binary = $binary; |
63
|
|
|
$this->binaryArgs = $binaryArgs; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the Artisan process for the job id. |
68
|
|
|
* |
69
|
|
|
* @param JobContractInterface $job |
70
|
|
|
* @param Options $options |
71
|
|
|
* |
72
|
|
|
* @return Process |
73
|
|
|
*/ |
74
|
|
|
public function getProcess(JobContractInterface $job, Options $options): Process |
75
|
|
|
{ |
76
|
|
|
return new Process(array_filter([ |
77
|
|
|
defined('PHP_WINDOWS_VERSION_BUILD') ? 'start /B ' : null, |
78
|
|
|
$this->getPhpBinary(), |
79
|
|
|
$this->scriptName, |
80
|
|
|
'job-queue:run-job', |
81
|
|
|
$job->getJobId(), |
82
|
|
|
'--connection=' . $job->getConnectionName(), |
83
|
|
|
'--queue=' . $job->getQueue(), |
84
|
|
|
'--env=' . $this->environment, |
85
|
|
|
'--delay=' . $options->delay, |
86
|
|
|
'--memory=' . $options->memory, |
87
|
|
|
'--timeout=' . $options->timeout, |
88
|
|
|
'--sleep=' . $options->sleep, |
89
|
|
|
'--maxTries=' . $options->maxTries, |
90
|
|
|
defined('PHP_WINDOWS_VERSION_BUILD') ? ' > NUL' : ' > /dev/null 2>&1 &', |
91
|
|
|
]), $this->binPath); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the escaped PHP Binary from the configuration |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getPhpBinary(): string |
100
|
|
|
{ |
101
|
|
|
$path = $this->binary; |
102
|
|
|
if (!defined('PHP_WINDOWS_VERSION_BUILD')) { |
103
|
|
|
$path = escapeshellarg($path); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$args = $this->binaryArgs; |
107
|
|
|
if (is_array($args)) { |
108
|
|
|
$args = implode(' ', $args); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return trim(trim($path . ' ' . $args), '\''); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|