-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorker.php
115 lines (102 loc) · 3.32 KB
/
Worker.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
111
112
113
114
115
<?php
/*
* This file is part of the Worker package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Component\Worker;
use EXSyst\Component\IO\Channel\ChannelInterface;
use EXSyst\Component\IO\Sink;
use EXSyst\Component\IO\Source;
use EXSyst\Component\Worker\Bootstrap\WorkerBootstrapProfile;
class Worker implements ChannelInterface
{
/**
* @var resource
*/
private $process;
/**
* @var ChannelInterface
*/
private $channel;
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationExpression
*/
protected function __construct(WorkerBootstrapProfile $bootstrapProfile, $implementationExpression)
{
$bootstrapProfile->getOrFindPhpExecutablePathAndArguments($php, $phpArgs);
$bootstrapProfile->compileScriptWithExpression($implementationExpression, null, $scriptPath, $deleteScript);
try {
$line = array_merge([$php], $phpArgs, [$scriptPath]);
$outPath = $bootstrapProfile->getOutputPath();
$this->process = proc_open(implode(' ', array_map('escapeshellarg', $line)), [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ($outPath !== null) ? ['file', $outPath, 'a'] : STDERR,
], $pipes);
$inputSink = Sink::fromStream($pipes[0], true);
$outputSource = Source::fromStream($pipes[1], true);
$this->channel = $bootstrapProfile->getChannelFactory()->createChannel($outputSource, $inputSink);
} catch (\Exception $e) {
if (isset($pipes[1])) {
fclose($pipes[1]);
}
if (isset($pipes[0])) {
fclose($pipes[0]);
}
if (isset($this->process)) {
proc_terminate($this->process);
proc_close($this->process);
}
if ($deleteScript) {
unlink($scriptPath);
}
throw $e;
}
}
public function __destruct()
{
unset($this->channel);
proc_close($this->process);
}
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationClassName
*
* @return static
*/
public static function withClass(WorkerBootstrapProfile $bootstrapProfile, $implementationClassName)
{
return new static($bootstrapProfile, $bootstrapProfile->generateExpression($implementationClassName));
}
/**
* @param WorkerBootstrapProfile $bootstrapProfile
* @param string $implementationExpression
*
* @return static
*/
public static function withExpression(WorkerBootstrapProfile $bootstrapProfile, $implementationExpression)
{
return new static($bootstrapProfile, $implementationExpression);
}
/** {@inheritdoc} */
public function getStream()
{
return $this->channel->getStream();
}
/** {@inheritdoc} */
public function sendMessage($message)
{
$this->channel->sendMessage($message);
return $this;
}
/** {@inheritdoc} */
public function receiveMessage()
{
return $this->channel->receiveMessage();
}
}