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
|
|
|
$command = '%s %s job-queue:run-job %s --connection=%s --queue=%s --env=%s --delay=%s --memory=%s --timeout=%s --sleep=%s --maxTries=%s'; |
77
|
|
|
$command = $this->getBackgroundCommand($command); |
78
|
|
|
$command = sprintf( |
79
|
|
|
$command, |
80
|
|
|
$this->getPhpBinary(), |
81
|
|
|
$this->binPath . DIRECTORY_SEPARATOR . $this->scriptName, |
82
|
|
|
(string)$job->getJobId(), |
83
|
|
|
$job->getConnectionName(), |
84
|
|
|
$job->getQueue(), |
85
|
|
|
$this->environment, |
86
|
|
|
$options->delay, |
87
|
|
|
$options->memory, |
88
|
|
|
$options->timeout, |
89
|
|
|
$options->sleep, |
90
|
|
|
$options->maxTries |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return Process::fromShellCommandline($command); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Prepare command for background run |
98
|
|
|
* |
99
|
|
|
* @param $cmd |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function getBackgroundCommand(string $cmd): string |
104
|
|
|
{ |
105
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
106
|
|
|
return 'start /B ' . $cmd . ' > NUL'; |
107
|
|
|
} else { |
108
|
|
|
return $cmd . ' > /dev/null 2>&1 &'; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the escaped PHP Binary from the configuration |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getPhpBinary(): string |
118
|
|
|
{ |
119
|
|
|
$path = $this->binary; |
120
|
|
|
if (!defined('PHP_WINDOWS_VERSION_BUILD')) { |
121
|
|
|
$path = escapeshellarg($path); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$args = $this->binaryArgs; |
125
|
|
|
if (is_array($args)) { |
126
|
|
|
$args = implode(' ', $args); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return trim(trim($path . ' ' . $args), '\''); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|