1
1
<?php
2
2
declare (ticks = 1 );
3
3
4
+ use Resque \Reserver \ReserverInterface ;
5
+ use Resque \Reserver \ReserverFactory ;
6
+
4
7
/**
5
8
* Resque worker that handles checking queues for jobs, fetching them
6
9
* off the queues, running them and handling the result.
12
15
class Resque_Worker
13
16
{
14
17
/**
15
- * @var LoggerInterface Logging object that impliments the PSR-3 LoggerInterface
16
- */
18
+ * @var ReserverFactory
19
+ */
20
+ private static $ reserverFactory ;
21
+
22
+ /**
23
+ * @var LoggerInterface Logging object that impliments the PSR-3 LoggerInterface
24
+ */
17
25
public $ logger ;
18
26
27
+ /**
28
+ * @var ReserverInterface The reserver used to reserve jobs from the queues.
29
+ */
30
+ private $ reserver ;
31
+
19
32
/**
20
33
* @var array Array of all associated queues for this worker.
21
34
*/
@@ -60,11 +73,13 @@ class Resque_Worker
60
73
* order. You can easily add new queues dynamically and have them worked on using
61
74
* this method.
62
75
*
76
+ * @param ReserverInterface $reserver The reserver to use to reserve jobs from the queues.
63
77
* @param string|array $queues String with a single queue name, array with multiple.
64
78
*/
65
- public function __construct ($ queues )
79
+ public function __construct (ReserverInterface $ reserver , $ queues )
66
80
{
67
- $ this ->logger = new Resque_Log ();
81
+ $ this ->reserver = $ reserver ;
82
+ $ this ->logger = new Resque_Log ();
68
83
69
84
if (!is_array ($ queues )) {
70
85
$ queues = array ($ queues );
@@ -76,6 +91,16 @@ public function __construct($queues)
76
91
$ this ->id = $ this ->hostname . ': ' .getmypid () . ': ' . implode (', ' , $ this ->queues );
77
92
}
78
93
94
+ /**
95
+ * Sets the reserver factory instance. Used by the find() method to create worker instances.
96
+ *
97
+ * @param ReserverFactory $reserverFactory
98
+ */
99
+ public static function setReserverFactory (ReserverFactory $ reserverFactory )
100
+ {
101
+ self ::$ reserverFactory = $ reserverFactory ;
102
+ }
103
+
79
104
/**
80
105
* Return all workers known to Resque as instantiated instances.
81
106
* @return array
@@ -119,7 +144,9 @@ public static function find($workerId)
119
144
120
145
list ($ hostname , $ pid , $ queues ) = explode (': ' , $ workerId , 3 );
121
146
$ queues = explode (', ' , $ queues );
122
- $ worker = new self ($ queues );
147
+
148
+ $ reserver = self ::$ reserverFactory ->createDefaultReserver ($ queues );
149
+ $ worker = new self ($ reserver , $ queues );
123
150
$ worker ->setId ($ workerId );
124
151
return $ worker ;
125
152
}
@@ -142,7 +169,7 @@ public function setId($workerId)
142
169
*
143
170
* @param int $interval How often to check for new jobs across the queues.
144
171
*/
145
- public function work ($ interval = Resque::DEFAULT_INTERVAL , $ blocking = false )
172
+ public function work ($ interval = Resque::DEFAULT_INTERVAL )
146
173
{
147
174
$ this ->updateProcLine ('Starting ' );
148
175
$ this ->startup ();
@@ -154,36 +181,25 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
154
181
155
182
// Attempt to find and reserve a job
156
183
$ job = false ;
157
- if (!$ this ->paused ) {
158
- if ($ blocking === true ) {
159
- $ this ->logger ->log (Psr \Log \LogLevel::INFO , 'Starting blocking with timeout of {interval} ' , array ('interval ' => $ interval ));
160
- $ this ->updateProcLine ('Waiting for ' . implode (', ' , $ this ->queues ) . ' with blocking timeout ' . $ interval );
161
- } else {
162
- $ this ->updateProcLine ('Waiting for ' . implode (', ' , $ this ->queues ) . ' with interval ' . $ interval );
163
- }
184
+ if (!$ this ->paused ) {
185
+ $ this ->updateProcLine ('Waiting for ' . implode (', ' , $ this ->queues ) . ' with interval ' . $ interval );
164
186
165
- $ job = $ this ->reserve ($ blocking , $ interval );
187
+ $ job = $ this ->reserve ();
188
+ } else {
189
+ $ this ->updateProcLine ('Paused ' );
166
190
}
167
191
168
- if (!$ job ) {
192
+ if (!$ job ) {
169
193
// For an interval of 0, break now - helps with unit testing etc
170
- if ($ interval == 0 ) {
194
+ if ($ interval == 0 ) {
171
195
break ;
172
196
}
173
197
174
- if ($ blocking === false )
175
- {
176
- // If no job was found, we sleep for $interval before continuing and checking again
198
+ // If no job was found, we sleep for $interval before continuing and checking again
199
+ if ($ this ->reserver ->waitAfterReservationAttempt ()) {
177
200
$ this ->logger ->log (Psr \Log \LogLevel::INFO , 'Sleeping for {interval} ' , array ('interval ' => $ interval ));
178
- if ($ this ->paused ) {
179
- $ this ->updateProcLine ('Paused ' );
180
- }
181
- else {
182
- $ this ->updateProcLine ('Waiting for ' . implode (', ' , $ this ->queues ));
183
- }
184
-
185
- usleep ($ interval * 1000000 );
186
- }
201
+ usleep ($ interval * 1000000 );
202
+ }
187
203
188
204
continue ;
189
205
}
@@ -252,33 +268,11 @@ public function perform(Resque_Job $job)
252
268
/**
253
269
* @param bool $blocking
254
270
* @param int $timeout
255
- * @return object|boolean Instance of Resque_Job if a job is found, false if not.
271
+ * @return object|boolean Instance of Resque_Job if a job is found, false if not.
256
272
*/
257
- public function reserve ($ blocking = false , $ timeout = null )
273
+ public function reserve ()
258
274
{
259
- $ queues = $ this ->queues ();
260
- if (!is_array ($ queues )) {
261
- return ;
262
- }
263
-
264
- if ($ blocking === true ) {
265
- $ job = Resque_Job::reserveBlocking ($ queues , $ timeout );
266
- if ($ job ) {
267
- $ this ->logger ->log (Psr \Log \LogLevel::INFO , 'Found job on {queue} ' , array ('queue ' => $ job ->queue ));
268
- return $ job ;
269
- }
270
- } else {
271
- foreach ($ queues as $ queue ) {
272
- $ this ->logger ->log (Psr \Log \LogLevel::INFO , 'Checking {queue} for jobs ' , array ('queue ' => $ queue ));
273
- $ job = Resque_Job::reserve ($ queue );
274
- if ($ job ) {
275
- $ this ->logger ->log (Psr \Log \LogLevel::INFO , 'Found job on {queue} ' , array ('queue ' => $ job ->queue ));
276
- return $ job ;
277
- }
278
- }
279
- }
280
-
281
- return false ;
275
+ return $ this ->reserver ->reserve () ?: false ;
282
276
}
283
277
284
278
/**
0 commit comments