Completed
Pull Request — master (#6)
by
unknown
08:52
created

JobProcess::getBackgroundCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
     * JobProcess constructor.
40
     *
41
     * @param string $scriptName
42
     * @param string $binPath
43
     * @param string $binary
44
     * @param string $binaryArgs
45
     */
46
    public function __construct(
47
        string $scriptName,
48
        string $binPath,
49
        string $binary = 'php',
50
        string $binaryArgs = '')
51
    {
52
        $this->scriptName = $scriptName;
53
        $this->binPath = $binPath;
54
        $this->binary = $binary;
55
        $this->binaryArgs = $binaryArgs;
56
    }
57
58
    /**
59
     * Get the Artisan process for the job id.
60
     *
61
     * @param JobContractInterface $job
62
     * @param Options $options
63
     *
64
     * @return Process
65
     */
66
    public function getProcess(JobContractInterface $job, Options $options): Process
67
    {
68
//        $cmd = '%s %s job-queue:run-job %s --connection=%s --queue=%s --env=%s --delay=%s --memory=%s --timeout=%s --sleep=%s --maxTries=%s';
69
//        $cmd = $this->getBackgroundCommand($cmd);
70
//        $cmd = sprintf(
71
//            $cmd,
72
//            $this->getPhpBinary(),
73
//            $this->scriptName,
74
//            (string)$job->getJobId(),
75
//            $job->getConnectionName(),
76
//            $job->getQueue(),
77
//            getenv('APP_ENV'),
78
//            $options->delay,
79
//            $options->memory,
80
//            $options->timeout,
81
//            $options->sleep,
82
//            $options->maxTries
83
//        );
84
85
        return new Process(array_filter([
86
            defined('PHP_WINDOWS_VERSION_BUILD') ? 'start /B ' : null,
87
            $this->getPhpBinary(),
88
            $this->scriptName,
89
            'job-queue:run-job',
90
            $job->getJobId(),
91
            '--connection=' . $job->getConnectionName(),
92
            '--queue=' . $job->getQueue(),
93
            '--env=' . getenv('APP_ENV'),
94
            '--delay=' . $options->delay,
95
            '--memory=' . $options->memory,
96
            '--timeout=' . $options->timeout,
97
            '--sleep=' . $options->sleep,
98
            '--maxTries=' . $options->maxTries,
99
            defined('PHP_WINDOWS_VERSION_BUILD') ? ' > NUL' : ' > /dev/null 2>&1 &',
100
        ]), $this->binPath);
101
    }
102
//
103
//    /**
104
//     * @param $cmd
105
//     *
106
//     * @return string
107
//     */
108
//    protected function getBackgroundCommand(string $cmd): string
109
//    {
110
//        if (defined('PHP_WINDOWS_VERSION_BUILD')) {
111
//            return 'start /B ' . $cmd . ' > NUL';
112
//        } else {
113
//            return $cmd . ' > /dev/null 2>&1 &';
114
//        }
115
//    }
116
117
    /**
118
     * Get the escaped PHP Binary from the configuration
119
     *
120
     * @return string
121
     */
122
    protected function getPhpBinary(): string
123
    {
124
        $path = $this->binary;
125
        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
126
            $path = escapeshellarg($path);
127
        }
128
129
        $args = $this->binaryArgs;
130
        if (is_array($args)) {
131
            $args = implode(' ', $args);
132
        }
133
134
        return trim(trim($path . ' ' . $args), '\'');
135
    }
136
}
137