1 | <?php |
||
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 | 3 | public function __construct( |
|
44 | ServerCollection $servers, |
||
45 | JobCollection $jobs, |
||
46 | EventDispatcherInterface $dispatcher |
||
47 | ) |
||
48 | { |
||
49 | 3 | $this->serverCollection = $servers; |
|
50 | 3 | $this->jobs = $jobs; |
|
51 | 3 | $this->dispatcher = $dispatcher; |
|
52 | 3 | } |
|
53 | |||
54 | /** |
||
55 | * @param \GearmanWorker $worker |
||
56 | */ |
||
57 | 3 | public function setImplementation(\GearmanWorker $worker) |
|
61 | |||
62 | 3 | public function run() |
|
63 | { |
||
64 | 3 | $this->getEventDispatcher()->dispatch( |
|
65 | 3 | WorkerEvent::EVENT_BEFORE_RUN, |
|
66 | 3 | new WorkerEvent($this) |
|
67 | 3 | ); |
|
68 | |||
69 | try { |
||
70 | |||
71 | 3 | while ($this->workerIsPending()) { |
|
72 | |||
73 | $gearmanReturnCode = $this->getWorker()->returnCode(); |
||
74 | |||
75 | if ($gearmanReturnCode == GEARMAN_IO_WAIT) { |
||
76 | |||
77 | $this->getEventDispatcher()->dispatch( |
||
78 | WorkerEvent::EVENT_ON_IO_WAIT, |
||
79 | new WorkerEvent($this) |
||
80 | ); |
||
81 | |||
82 | @$this->getWorker()->wait(); |
||
|
|||
83 | } |
||
84 | |||
85 | if ($gearmanReturnCode == GEARMAN_NO_JOBS) { |
||
86 | |||
87 | $this->getEventDispatcher()->dispatch( |
||
88 | WorkerEvent::EVENT_ON_NO_JOBS, |
||
89 | new WorkerEvent($this) |
||
90 | ); |
||
91 | |||
92 | } |
||
93 | |||
94 | if ($gearmanReturnCode != GEARMAN_SUCCESS) { |
||
95 | |||
96 | $this->getEventDispatcher()->dispatch( |
||
97 | WorkerEvent::EVENT_ON_WORK, |
||
98 | new WorkerEvent($this) |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | if ($gearmanReturnCode == GEARMAN_SUCCESS) { |
||
103 | |||
104 | $this->getEventDispatcher()->dispatch( |
||
105 | WorkerEvent::EVENT_AFTER_RUN, |
||
106 | new WorkerEvent($this) |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | } |
||
111 | 3 | } catch (NoFunctionGiven $e) { |
|
112 | 1 | throw $e; |
|
113 | } catch (\Exception $e) { |
||
114 | $this->getEventDispatcher()->dispatch( |
||
115 | WorkerExceptionEvent::EVENT_ON_FAILURE, |
||
116 | new WorkerExceptionEvent($this, $e) |
||
117 | ); |
||
118 | |||
119 | } |
||
120 | |||
121 | 2 | return; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return EventDispatcherInterface |
||
126 | */ |
||
127 | 3 | protected function getEventDispatcher() |
|
128 | { |
||
129 | 3 | return $this->dispatcher; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return \GearmanWorker |
||
134 | */ |
||
135 | 3 | private function getWorker() |
|
170 | |||
171 | 3 | private function registerFunctions() |
|
172 | { |
||
173 | 3 | $this->getEventDispatcher()->dispatch( |
|
227 | |||
228 | /** |
||
229 | * @return bool |
||
230 | */ |
||
231 | 3 | private function workerIsPending() |
|
239 | |||
240 | public function destroyWorker() |
||
252 | } |
||
253 |
If you suppress an error, we recommend checking for the error condition explicitly: