1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pyrowman\PheanstalkBundle\Proxy; |
4
|
|
|
|
5
|
|
|
use Pheanstalk\Command\CreateScheduleCommand; |
6
|
|
|
use Pheanstalk\Command\GetWorkflowInstancesCommand; |
7
|
|
|
use Pheanstalk\Pheanstalk; |
8
|
|
|
use Pheanstalk\Structure\Schedule; |
9
|
|
|
use Pheanstalk\Structure\TaskInstance; |
10
|
|
|
use Pheanstalk\Structure\TimeSchedule; |
11
|
|
|
use Pheanstalk\Structure\Tube; |
12
|
|
|
use Pheanstalk\Structure\Workflow; |
13
|
|
|
use Pheanstalk\Structure\WorkflowInstance; |
14
|
|
|
use Pyrowman\PheanstalkBundle\Event\CommandEvent; |
15
|
|
|
use Pheanstalk\Connection; |
16
|
|
|
use Pheanstalk\PheanstalkInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
class PheanstalkProxy implements PheanstalkProxyInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EventDispatcherInterface |
23
|
|
|
*/ |
24
|
|
|
protected $dispatcher; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $name; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Pheanstalk |
33
|
|
|
*/ |
34
|
|
|
protected $pheanstalk; |
35
|
|
|
|
36
|
|
|
/** @var $currentClass PheanstalkInterface */ |
|
|
|
|
37
|
|
|
private $currentClass; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
*/ |
42
|
|
|
public function setConnection(Connection $connection) |
43
|
|
|
{ |
44
|
|
|
$this->pheanstalk->setConnection($connection); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function getConnection() |
53
|
|
|
{ |
54
|
|
|
return $this->pheanstalk->getConnection(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return PheanstalkInterface |
59
|
|
|
*/ |
60
|
|
|
public function getCurrentClass(): PheanstalkInterface |
61
|
|
|
{ |
62
|
|
|
return $this->currentClass ?? $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param PheanstalkInterface $currentClass |
67
|
|
|
* |
68
|
|
|
* @return Pheanstalk |
69
|
|
|
*/ |
70
|
|
|
public function setCurrentClass(PheanstalkInterface $currentClass): PheanstalkInterface |
71
|
|
|
{ |
72
|
|
|
$this->currentClass = $currentClass; |
73
|
|
|
return $this; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function delete(Workflow $workflow) |
80
|
|
|
{ |
81
|
1 |
|
if ($this->dispatcher) { |
82
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflow' => $workflow]), CommandEvent::DELETE); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$this->pheanstalk->delete($workflow); |
86
|
|
|
|
87
|
1 |
|
return $this; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
1 |
|
public function workflowExists($name) |
94
|
|
|
{ |
95
|
1 |
|
if ($this->dispatcher) { |
96
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['name' => $name]), CommandEvent::WORKFLOW_EXISTS); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $this->pheanstalk->workflowExists($name); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function getWorkflow(Workflow $workflow) |
106
|
|
|
{ |
107
|
1 |
|
if ($this->dispatcher) { |
108
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflow' => $workflow]), CommandEvent::TASK_EXISTS); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $this->pheanstalk->getWorkflow($workflow); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
1 |
|
public function getWorkflowInstances(?Workflow $workflow = null, ?string $status = null) |
118
|
|
|
{ |
119
|
1 |
|
if ($this->dispatcher) { |
120
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
121
|
1 |
|
'workflow' => $workflow, |
122
|
1 |
|
'status' => $status |
123
|
1 |
|
]), CommandEvent::WORKFLOW_INSTANCES); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $this->pheanstalk->getWorkflowInstances($workflow, $status); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function getWorkflowInstancesDetails(WorkflowInstance $workflowInstance) |
133
|
|
|
{ |
134
|
1 |
|
if ($this->dispatcher) { |
135
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflowInstance' => $workflowInstance]), |
136
|
1 |
|
CommandEvent::WORKFLOW_INSTANCES_DETAILS); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
return $this->pheanstalk->getWorkflowInstancesDetails($workflowInstance); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function tubeExists($name) |
146
|
|
|
{ |
147
|
1 |
|
if ($this->dispatcher) { |
148
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['name' => $name]), CommandEvent::TUBE_EXISTS); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
return $this->pheanstalk->tubeExists($name); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
1 |
|
public function listTubes() |
158
|
|
|
{ |
159
|
1 |
|
if ($this->dispatcher) { |
160
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this), CommandEvent::LIST_TUBES); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
return $this->pheanstalk->listTubes(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritDoc} |
168
|
|
|
*/ |
169
|
1 |
|
public function peek() |
170
|
|
|
{ |
171
|
1 |
|
if ($this->dispatcher) { |
172
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this), CommandEvent::PEEK); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
return $this->pheanstalk->peek(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritDoc} |
180
|
|
|
*/ |
181
|
1 |
|
public function put(Workflow $workflow) |
182
|
|
|
{ |
183
|
1 |
|
if ($this->dispatcher) |
|
|
|
|
184
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflow' => $workflow]), CommandEvent::PUT); |
|
|
|
|
185
|
|
|
|
186
|
1 |
|
return $this->pheanstalk->put($workflow); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
1 |
|
public function statsTube(Tube $tube) |
193
|
|
|
{ |
194
|
1 |
|
if ($this->dispatcher) { |
195
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['tube' => $tube]), CommandEvent::STATS_TUBE); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
return $this->pheanstalk->statsTube($tube); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
*/ |
204
|
2 |
|
public function stats() |
205
|
|
|
{ |
206
|
2 |
|
if ($this->dispatcher) { |
207
|
2 |
|
$this->dispatcher->dispatch(new CommandEvent($this), CommandEvent::STATS); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
2 |
|
return $this->pheanstalk->stats(); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return EventDispatcherInterface |
215
|
|
|
*/ |
216
|
|
|
public function getDispatcher() |
217
|
|
|
{ |
218
|
|
|
return $this->dispatcher; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param EventDispatcherInterface $dispatch |
223
|
|
|
*/ |
224
|
25 |
|
public function setDispatcher(EventDispatcherInterface $dispatch) |
225
|
|
|
{ |
226
|
25 |
|
$this->dispatcher = $dispatch; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritDoc} |
231
|
|
|
*/ |
232
|
1 |
|
public function getPheanstalk() |
233
|
|
|
{ |
234
|
1 |
|
return $this->pheanstalk; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritDoc} |
239
|
|
|
*/ |
240
|
27 |
|
public function setPheanstalk(PheanstalkInterface $pheanstalk) |
241
|
|
|
{ |
242
|
27 |
|
$this->pheanstalk = $pheanstalk; |
|
|
|
|
243
|
27 |
|
$this->pheanstalk->setCurrentClass($this); |
244
|
|
|
|
245
|
27 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritDoc} |
250
|
|
|
*/ |
251
|
|
|
public function getName() |
252
|
|
|
{ |
253
|
|
|
return $this->name; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritDoc} |
258
|
|
|
*/ |
259
|
1 |
|
public function setName($name) |
260
|
|
|
{ |
261
|
1 |
|
$this->name = $name; |
262
|
|
|
|
263
|
1 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritdoc} |
268
|
|
|
*/ |
269
|
2 |
|
public function create(Workflow $workflow, $force = false): Workflow |
270
|
|
|
{ |
271
|
2 |
|
if ($this->dispatcher) { |
272
|
2 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflow' => $workflow]), CommandEvent::CREATE_WORKFLOW); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
2 |
|
$workflow = $this->pheanstalk->create($workflow); |
276
|
2 |
|
return $workflow; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
*/ |
282
|
1 |
|
public function update(Workflow $workflow): Workflow |
283
|
|
|
{ |
284
|
1 |
|
if ($this->dispatcher) { |
285
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflow' => $workflow]), CommandEvent::UPDATE_WORKFLOW); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
return $this->pheanstalk->update($workflow); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
1 |
|
public function createSchedule(Schedule $schedule) |
295
|
|
|
{ |
296
|
1 |
|
if ($this->dispatcher) { |
297
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
298
|
1 |
|
'schedule' => $schedule, |
299
|
1 |
|
]), CommandEvent::CREATE_SCHEDULE); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
$workflowSchedule = $this->pheanstalk->createSchedule($schedule); |
303
|
1 |
|
return $workflowSchedule; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* {@inheritdoc} |
308
|
|
|
*/ |
309
|
1 |
|
public function deleteSchedule(Schedule $schedule) |
310
|
|
|
{ |
311
|
1 |
|
if ($this->dispatcher) { |
312
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
313
|
1 |
|
'schedule' => $schedule, |
314
|
1 |
|
]), CommandEvent::DELETE_SCHEDULE); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
1 |
|
return $this->pheanstalk->deleteSchedule($schedule); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* {@inheritdoc} |
322
|
|
|
*/ |
323
|
1 |
|
public function getSchedule(int $schedule) |
324
|
|
|
{ |
325
|
1 |
|
if ($this->dispatcher) { |
326
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
327
|
1 |
|
'schedule' => $schedule, |
328
|
1 |
|
]), CommandEvent::GET_SCHEDULE); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
1 |
|
return $this->pheanstalk->getSchedule($schedule); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* {@inheritdoc} |
336
|
|
|
*/ |
337
|
1 |
|
public function updateSchedule(Schedule $schedule): Schedule |
338
|
|
|
{ |
339
|
1 |
|
if ($this->dispatcher) { |
340
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
341
|
1 |
|
'schedule' => $schedule, |
342
|
1 |
|
]), CommandEvent::UPDATE_SCHEDULE); |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
345
|
1 |
|
return $this->pheanstalk->updateSchedule($schedule); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* {@inheritdoc} |
350
|
|
|
*/ |
351
|
1 |
|
public function listSchedules() |
352
|
|
|
{ |
353
|
1 |
|
if ($this->dispatcher) { |
354
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, []), CommandEvent::LIST_SCHEDULE); |
|
|
|
|
355
|
|
|
} |
356
|
|
|
|
357
|
1 |
|
return $this->pheanstalk->listSchedules(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* {@inheritdoc} |
362
|
|
|
*/ |
363
|
1 |
|
public function createTask(string $name, string $group, string $path, $queue = 'default', $useAgent = false, $user = null, $host = null, $comment = null): Workflow |
364
|
|
|
{ |
365
|
1 |
|
if ($this->dispatcher) { |
366
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, [ |
367
|
1 |
|
'name' => $name, |
368
|
1 |
|
'group' => $group, |
369
|
1 |
|
'path' => $path, |
370
|
1 |
|
'queue' => $queue, |
371
|
1 |
|
'useAgent' => $useAgent, |
372
|
1 |
|
'user' => $user, |
373
|
1 |
|
'host' => $host, |
374
|
1 |
|
'comment' => $comment |
375
|
1 |
|
]), CommandEvent::CREATE_TASK); |
|
|
|
|
376
|
|
|
} |
377
|
|
|
|
378
|
1 |
|
return $this->pheanstalk->createTask($name, $group, $path, $queue, $useAgent, $user, $host, $comment); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* {@inheritdoc} |
383
|
|
|
*/ |
384
|
1 |
|
public function createTube(Tube $tube): Tube |
385
|
|
|
{ |
386
|
1 |
|
if ($this->dispatcher) { |
387
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['tube' => $tube]), COmmandEvent::CREATE_TUBE); |
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
1 |
|
return $this->pheanstalk->createTube($tube); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* {@inheritdoc} |
395
|
|
|
*/ |
396
|
1 |
|
public function updateTube(Tube $tube): Tube |
397
|
|
|
{ |
398
|
1 |
|
if ($this->dispatcher) { |
399
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['tube' => $tube]), COmmandEvent::CREATE_TUBE); |
|
|
|
|
400
|
|
|
} |
401
|
|
|
|
402
|
1 |
|
return $this->pheanstalk->updateTube($tube); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* {@inheritdoc} |
407
|
|
|
*/ |
408
|
1 |
|
public function cancel(WorkflowInstance $workflowInstance) |
409
|
|
|
{ |
410
|
1 |
|
if ($this->dispatcher) { |
411
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflowInstance' => $workflowInstance]), COmmandEvent::CANCEL); |
|
|
|
|
412
|
|
|
} |
413
|
|
|
|
414
|
1 |
|
return $this->pheanstalk->cancel($workflowInstance); |
415
|
|
|
} |
416
|
|
|
|
417
|
1 |
|
public function kill(WorkflowInstance $workflowInstance, TaskInstance $taskInstance) |
418
|
|
|
{ |
419
|
1 |
|
if ($this->dispatcher) { |
420
|
1 |
|
$this->dispatcher->dispatch(new CommandEvent($this, ['workflowInstance' => $workflowInstance, 'taskInstance' => $taskInstance]), COmmandEvent::CANCEL); |
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
423
|
1 |
|
return $this->pheanstalk->kill($workflowInstance, $taskInstance); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|