1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dmank\gearman; |
4
|
|
|
|
5
|
|
|
use dmank\gearman\event\ConnectToServerEvent; |
6
|
|
|
use dmank\gearman\event\FunctionEvent; |
7
|
|
|
use dmank\gearman\event\FunctionFailureEvent; |
8
|
|
|
use dmank\gearman\event\RegisterFunctionEvent; |
9
|
|
|
use dmank\gearman\event\WorkerEvent; |
10
|
|
|
use dmank\gearman\event\WorkerExceptionEvent; |
11
|
|
|
use dmank\gearman\exception\NoFunctionGiven; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
class Worker |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \GearmanWorker |
18
|
|
|
*/ |
19
|
|
|
private $realWorker; |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $connectedToServer = false; |
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $registeredFunctions = false; |
28
|
|
|
/** |
29
|
|
|
* @var ServerCollection |
30
|
|
|
*/ |
31
|
|
|
private $serverCollection; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var JobCollection |
35
|
|
|
*/ |
36
|
|
|
private $jobs = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var EventDispatcherInterface |
40
|
|
|
*/ |
41
|
|
|
private $dispatcher; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $killRequested = false; |
47
|
|
|
|
48
|
3 |
|
public function __construct( |
49
|
|
|
ServerCollection $servers, |
50
|
|
|
JobCollection $jobs, |
51
|
|
|
EventDispatcherInterface $dispatcher |
52
|
|
|
) |
53
|
|
|
{ |
54
|
3 |
|
$this->serverCollection = $servers; |
55
|
3 |
|
$this->jobs = $jobs; |
56
|
3 |
|
$this->dispatcher = $dispatcher; |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \GearmanWorker $worker |
61
|
|
|
*/ |
62
|
3 |
|
public function setImplementation(\GearmanWorker $worker) |
63
|
|
|
{ |
64
|
3 |
|
$this->realWorker = $worker; |
65
|
3 |
|
} |
66
|
|
|
|
67
|
3 |
|
public function run() |
68
|
|
|
{ |
69
|
3 |
|
$this->getEventDispatcher()->dispatch( |
70
|
3 |
|
WorkerEvent::EVENT_BEFORE_RUN, |
71
|
3 |
|
new WorkerEvent($this) |
72
|
3 |
|
); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
|
76
|
3 |
|
while ($this->workerIsPending()) { |
77
|
|
|
|
78
|
|
|
$gearmanReturnCode = $this->getWorker()->returnCode(); |
79
|
|
|
|
80
|
|
|
if ($gearmanReturnCode == GEARMAN_IO_WAIT) { |
81
|
|
|
|
82
|
|
|
$this->getEventDispatcher()->dispatch( |
83
|
|
|
WorkerEvent::EVENT_ON_IO_WAIT, |
84
|
|
|
new WorkerEvent($this) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
@$this->getWorker()->wait(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($gearmanReturnCode == GEARMAN_NO_JOBS) { |
91
|
|
|
|
92
|
|
|
$this->getEventDispatcher()->dispatch( |
93
|
|
|
WorkerEvent::EVENT_ON_NO_JOBS, |
94
|
|
|
new WorkerEvent($this) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($gearmanReturnCode != GEARMAN_SUCCESS) { |
100
|
|
|
|
101
|
|
|
$this->getEventDispatcher()->dispatch( |
102
|
|
|
WorkerEvent::EVENT_ON_WORK, |
103
|
|
|
new WorkerEvent($this) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($gearmanReturnCode == GEARMAN_SUCCESS) { |
108
|
|
|
|
109
|
|
|
$this->getEventDispatcher()->dispatch( |
110
|
|
|
WorkerEvent::EVENT_AFTER_RUN, |
111
|
|
|
new WorkerEvent($this) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
3 |
|
} catch (NoFunctionGiven $e) { |
117
|
1 |
|
throw $e; |
118
|
|
|
} catch (\Exception $e) { |
119
|
|
|
$this->getEventDispatcher()->dispatch( |
120
|
|
|
WorkerExceptionEvent::EVENT_ON_FAILURE, |
121
|
|
|
new WorkerExceptionEvent($this, $e) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return EventDispatcherInterface |
131
|
|
|
*/ |
132
|
3 |
|
protected function getEventDispatcher() |
133
|
|
|
{ |
134
|
3 |
|
return $this->dispatcher; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return \GearmanWorker |
139
|
|
|
*/ |
140
|
3 |
|
private function getWorker() |
141
|
|
|
{ |
142
|
3 |
|
if (!$this->realWorker) { |
143
|
|
|
$this->realWorker = new \GearmanWorker(); |
144
|
|
|
$this->realWorker->setOptions(GEARMAN_WORKER_NON_BLOCKING); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
if (!$this->connectedToServer) { |
148
|
|
|
|
149
|
3 |
|
$this->getEventDispatcher()->dispatch( |
150
|
3 |
|
ConnectToServerEvent::CONNECT_TO_SERVER_EVENT, |
151
|
3 |
|
new ConnectToServerEvent($this->serverCollection) |
152
|
3 |
|
); |
153
|
|
|
|
154
|
|
|
/* @var \dmank\gearman\Server $server */ |
155
|
3 |
|
foreach ($this->serverCollection->getServers() as $server) { |
156
|
3 |
|
$this->realWorker->addServer($server->getHost(), $server->getPort()); |
157
|
3 |
|
} |
158
|
|
|
|
159
|
3 |
|
$this->getEventDispatcher()->dispatch( |
160
|
3 |
|
ConnectToServerEvent::CONNECTED_TO_SERVER_EVENT, |
161
|
3 |
|
new ConnectToServerEvent($this->serverCollection) |
162
|
3 |
|
); |
163
|
|
|
|
164
|
3 |
|
$this->connectedToServer = true; |
165
|
3 |
|
} |
166
|
|
|
|
167
|
3 |
|
if (!$this->registeredFunctions) { |
168
|
3 |
|
$this->registerFunctions(); |
169
|
|
|
|
170
|
2 |
|
$this->registeredFunctions = true; |
171
|
2 |
|
} |
172
|
|
|
|
173
|
2 |
|
return $this->realWorker; |
174
|
|
|
} |
175
|
|
|
|
176
|
3 |
|
private function registerFunctions() |
177
|
|
|
{ |
178
|
3 |
|
$this->getEventDispatcher()->dispatch( |
179
|
3 |
|
RegisterFunctionEvent::EVENT_ON_BEFORE_REGISTER_FUNCTIONS, |
180
|
3 |
|
new RegisterFunctionEvent($this->jobs) |
181
|
3 |
|
); |
182
|
|
|
|
183
|
3 |
|
if (count($this->jobs) == 0) { |
184
|
|
|
|
185
|
1 |
|
throw new NoFunctionGiven( |
186
|
1 |
|
sprintf('Didnt have jobs to register. So we need to stop here. My bad!') |
187
|
1 |
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/* @var JobHandlerInterface $jobClass */ |
191
|
2 |
|
foreach ($this->jobs->getJobs() as $jobName => $jobClass) { |
192
|
2 |
|
$this->realWorker->addFunction( |
193
|
2 |
|
$jobName, |
194
|
|
|
function (\GearmanJob $gearmanJob) use ($jobClass, $jobName) { |
195
|
|
|
|
196
|
|
|
$job = new Job($jobName, $gearmanJob->workload()); |
197
|
|
|
|
198
|
|
|
try { |
199
|
|
|
|
200
|
|
|
$this->getEventDispatcher()->dispatch( |
201
|
|
|
FunctionEvent::FUNCTION_BEFORE_EXECUTE, |
202
|
|
|
new FunctionEvent($jobClass, $job) |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$result = $jobClass->execute($job); |
206
|
|
|
|
207
|
|
|
$this->getEventDispatcher()->dispatch( |
208
|
|
|
FunctionEvent::FUNCTION_AFTER_EXECUTE, |
209
|
|
|
new FunctionEvent($jobClass, $job) |
210
|
|
|
); |
211
|
|
|
} catch (\Exception $e) { |
212
|
|
|
|
213
|
|
|
$this->getEventDispatcher()->dispatch( |
214
|
|
|
FunctionFailureEvent::FUNCTION_ON_FAILURE, |
215
|
|
|
new FunctionFailureEvent($jobClass, $e, $job) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$result = $e; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$gearmanJob->sendComplete($result); |
222
|
|
|
} |
223
|
2 |
|
); |
224
|
2 |
|
} |
225
|
|
|
|
226
|
2 |
|
$this->getEventDispatcher()->dispatch( |
227
|
2 |
|
RegisterFunctionEvent::EVENT_ON_AFTER_REGISTER_FUNCTIONS, |
228
|
2 |
|
new RegisterFunctionEvent($this->jobs) |
229
|
2 |
|
); |
230
|
|
|
|
231
|
2 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
3 |
|
private function workerIsPending() |
237
|
|
|
{ |
238
|
|
|
return ( |
239
|
3 |
|
$this->getWorker()->work() || |
240
|
2 |
|
$this->getWorker()->returnCode() == GEARMAN_IO_WAIT || |
241
|
2 |
|
$this->getWorker()->returnCode() == GEARMAN_NO_JOBS |
242
|
2 |
|
) && !$this->killRequested; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function destroyWorker() |
246
|
|
|
{ |
247
|
|
|
$this->getEventDispatcher()->dispatch( |
248
|
|
|
WorkerEvent::EVENT_ON_BEFORE_DESTROY, |
249
|
|
|
new WorkerEvent($this) |
250
|
|
|
); |
251
|
|
|
|
252
|
|
|
$this->killRequested = true; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: