-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.php
64 lines (56 loc) · 2.06 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
<?php
namespace DS\Worker;
use DS\Queue\Consumer\Consumer;
use DS\Queue\Job\Job;
use DS\Queue\Queue;
use DS\Worker\Event\JobCompletedEvent;
use DS\Worker\Event\NoPendingJobEvent;
use DS\Worker\Event\PassCompletedEvent;
use DS\Worker\Event\ResetEvent;
use DS\Worker\Event\ShutdownEvent;
use DS\Worker\Exception\UnknownQueueStatus;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Coordinates moving through the queue in a flexible fashion
*
* @author Ross Tuck <[email protected]>
*/
class Worker
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher) {
$this->eventDispatcher = $eventDispatcher;
}
/**
* Begin processing the backlog of jobs in the queue
*
* @param Queue $queue
* @param Consumer $consumer
* @throws Exception\UnknownQueueStatus
*/
public function work(Queue $queue, Consumer $consumer)
{
$this->eventDispatcher->dispatch(ResetEvent::NAME, new ResetEvent());
do {
$result = $queue->processNextJob($consumer);
// Handle the known return values
if ($result === Queue::RESULT_NO_JOB) {
$this->eventDispatcher->dispatch(NoPendingJobEvent::NAME, new NoPendingJobEvent());
} elseif (is_object($result) && $result instanceof Job) {
$this->eventDispatcher->dispatch(JobCompletedEvent::NAME, new JobCompletedEvent($result, $queue));
} else {
throw new UnknownQueueStatus("Expected job or status code, received '{$result}'");
}
// Fire the pass completed event, this gives us a chance to exit
$passCompletedEvent = new PassCompletedEvent();
$this->eventDispatcher->dispatch(PassCompletedEvent::NAME, $passCompletedEvent);
} while(!$passCompletedEvent->isTerminating());
$this->eventDispatcher->dispatch(ShutdownEvent::NAME, new ShutdownEvent());
}
}