-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
AbstractWorker.php
177 lines (144 loc) · 4.12 KB
/
AbstractWorker.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
<?php
namespace Amp\Parallel\Worker;
use Amp\Coroutine;
use Amp\Deferred;
use Amp\Parallel\StatusError;
use Amp\Parallel\Strand;
use Amp\Parallel\Worker\Internal\Job;
use Amp\Parallel\Worker\Internal\TaskResult;
use Amp\Promise;
/**
* Base class for most common types of task workers.
*/
abstract class AbstractWorker implements Worker {
/** @var \Amp\Parallel\Strand */
private $context;
/** @var bool */
private $shutdown = false;
/** @var \Amp\Deferred[] */
private $jobQueue = [];
/** @var callable */
private $when;
/**
* @param \Amp\Parallel\Strand $strand
*/
public function __construct(Strand $strand) {
$this->context = $strand;
$this->when = function ($exception, $data) {
if ($exception) {
$this->kill();
return;
}
if (!$data instanceof TaskResult) {
$this->kill();
return;
}
$id = $data->getId();
if (!isset($this->jobQueue[$id])) {
$this->kill();
return;
}
$deferred = $this->jobQueue[$id];
unset($this->jobQueue[$id]);
if (!empty($this->jobQueue)) {
$this->context->receive()->onResolve($this->when);
}
$deferred->resolve($data->promise());
};
}
/**
* {@inheritdoc}
*/
public function isRunning(): bool {
return $this->context->isRunning();
}
/**
* {@inheritdoc}
*/
public function isIdle(): bool {
return empty($this->jobQueue);
}
/**
* {@inheritdoc}
*/
public function start() {
$this->context->start();
}
/**
* {@inheritdoc}
*/
public function enqueue(Task $task): Promise {
if (!$this->context->isRunning()) {
throw new StatusError('The worker has not been started.');
}
if ($this->shutdown) {
throw new StatusError('The worker has been shut down.');
}
return new Coroutine($this->doEnqueue($task));
}
/**
* @coroutine
*
* @param \Amp\Parallel\Worker\Task $task
*
* @return \Generator
* @throws \Amp\Parallel\StatusError
* @throws \Amp\Parallel\Worker\TaskException
* @throws \Amp\Parallel\Worker\TaskError
* @throws \Amp\Parallel\Worker\WorkerException
*/
private function doEnqueue(Task $task): \Generator {
if (empty($this->jobQueue)) {
$this->context->receive()->onResolve($this->when);
}
try {
$job = new Job($task);
$this->jobQueue[$job->getId()] = $deferred = new Deferred;
yield $this->context->send($job);
} catch (\Throwable $exception) {
$this->kill();
throw new WorkerException('Sending the task to the worker failed.', $exception);
}
return yield $deferred->promise();
}
/**
* {@inheritdoc}
*/
public function shutdown(): Promise {
if (!$this->context->isRunning() || $this->shutdown) {
throw new StatusError('The worker is not running.');
}
return new Coroutine($this->doShutdown());
}
/**
* {@inheritdoc}
*/
private function doShutdown(): \Generator {
$this->shutdown = true;
// If a task is currently running, wait for it to finish.
yield Promise\any(\array_map(function (Deferred $deferred): Promise {
return $deferred->promise();
}, $this->jobQueue));
yield $this->context->send(0);
return yield $this->context->join();
}
/**
* {@inheritdoc}
*/
public function kill() {
$this->cancelPending();
$this->context->kill();
}
/**
* Cancels all pending tasks.
*/
private function cancelPending() {
if (!empty($this->jobQueue)) {
$exception = new WorkerException('Worker was shut down.');
foreach ($this->jobQueue as $job) {
$job->fail($exception);
}
$this->jobQueue = [];
}
}
}