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