-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
TaskWorker.php
222 lines (183 loc) · 6.24 KB
/
TaskWorker.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Amp\Parallel\Worker;
use Amp\Delayed;
use Amp\Failure;
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\StatusError;
use Amp\Parallel\Sync\ChannelException;
use Amp\Promise;
use Amp\Success;
use Amp\TimeoutException;
use function Amp\call;
/**
* Base class for most common types of task workers.
*/
abstract class TaskWorker implements Worker
{
const SHUTDOWN_TIMEOUT = 1000;
const ERROR_TIMEOUT = 250;
/** @var Delayed */
private static $throttle;
/** @var Context */
private $context;
/** @var bool */
private $started = false;
/** @var Promise|null */
private $pending;
/** @var Promise|null */
private $exitStatus;
/**
* @param Context $context A context running an instance of TaskRunner.
*/
public function __construct(Context $context)
{
if (self::$throttle === null) {
self::$throttle = new Delayed(0);
}
if ($context->isRunning()) {
throw new \Error("The context was already running");
}
$this->context = $context;
$context = &$this->context;
$pending = &$this->pending;
\register_shutdown_function(static function () use (&$context, &$pending): void {
if ($context === null || !$context->isRunning()) {
return;
}
try {
Promise\wait(Promise\timeout(call(function () use ($context, $pending): \Generator {
if ($pending) {
yield $pending;
}
yield $context->send(0);
return yield $context->join();
}), self::SHUTDOWN_TIMEOUT));
} catch (\Throwable $exception) {
if ($context !== null) {
$context->kill();
}
}
});
}
/**
* {@inheritdoc}
*/
public function isRunning(): bool
{
// Report as running unless shutdown or crashed.
return !$this->started || ($this->exitStatus === null && $this->context !== null && $this->context->isRunning());
}
/**
* {@inheritdoc}
*/
public function isIdle(): bool
{
return $this->pending === null;
}
/**
* {@inheritdoc}
*/
public function enqueue(Task $task): Promise
{
if ($this->exitStatus) {
throw new StatusError("The worker has been shut down");
}
$promise = $this->pending = call(function () use ($task): \Generator {
if ($this->pending) {
try {
yield $this->pending;
} catch (\Throwable $exception) {
// Ignore error from prior job.
}
}
if ($this->exitStatus !== null || $this->context === null) {
throw new WorkerException("The worker was shutdown");
}
if (!$this->context->isRunning()) {
if ($this->started) {
throw new WorkerException("The worker crashed");
}
$delayed = self::$throttle;
self::$throttle = new Delayed(100);
yield $delayed;
$this->started = true;
yield $this->context->start();
}
$job = new Internal\Job($task);
try {
yield $this->context->send($job);
$result = yield $this->context->receive();
} catch (ChannelException $exception) {
try {
yield Promise\timeout($this->context->join(), self::ERROR_TIMEOUT);
} catch (TimeoutException $timeout) {
$this->kill();
throw new WorkerException("The worker failed unexpectedly", 0, $exception);
}
throw new WorkerException("The worker exited unexpectedly", 0, $exception);
}
if (!$result instanceof Internal\TaskResult) {
$this->kill();
throw new WorkerException("Context did not return a task result");
}
if ($result->getId() !== $job->getId()) {
$this->kill();
throw new WorkerException("Task results returned out of order");
}
return $result->promise();
});
$promise->onResolve(function () use ($promise): void {
if ($this->pending === $promise) {
$this->pending = null;
}
});
return $promise;
}
/**
* {@inheritdoc}
*/
public function shutdown(): Promise
{
if ($this->exitStatus !== null) {
return $this->exitStatus;
}
if ($this->context === null || !$this->context->isRunning()) {
return $this->exitStatus = new Success(-1); // Context crashed?
}
return $this->exitStatus = call(function (): \Generator {
if ($this->pending) {
// If a task is currently running, wait for it to finish.
yield Promise\any([$this->pending]);
}
yield $this->context->send(0);
try {
return yield Promise\timeout($this->context->join(), self::SHUTDOWN_TIMEOUT);
} catch (\Throwable $exception) {
$this->context->kill();
throw new WorkerException("Failed to gracefully shutdown worker", 0, $exception);
} finally {
// Null properties to free memory because the shutdown function has references to these.
$this->context = null;
$this->pending = null;
}
});
}
/**
* {@inheritdoc}
*/
public function kill(): void
{
if ($this->exitStatus !== null || $this->context === null) {
return;
}
if ($this->context->isRunning()) {
$this->context->kill();
$this->exitStatus = new Failure(new WorkerException("The worker was killed"));
return;
}
$this->exitStatus = new Success(0);
// Null properties to free memory because the shutdown function has references to these.
$this->context = null;
$this->pending = null;
}
}