11<?php
22declare (ticks = 1 );
33
4+ use Resque \Reserver \ReserverInterface ;
5+ use Resque \Reserver \ReserverFactory ;
6+
47/**
58 * Resque worker that handles checking queues for jobs, fetching them
69 * off the queues, running them and handling the result.
1215class Resque_Worker
1316{
1417 /**
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+ */
1725 public $ logger ;
1826
27+ /**
28+ * @var ReserverInterface The reserver used to reserve jobs from the queues.
29+ */
30+ private $ reserver ;
31+
1932 /**
2033 * @var array Array of all associated queues for this worker.
2134 */
@@ -60,11 +73,13 @@ class Resque_Worker
6073 * order. You can easily add new queues dynamically and have them worked on using
6174 * this method.
6275 *
76+ * @param ReserverInterface $reserver The reserver to use to reserve jobs from the queues.
6377 * @param string|array $queues String with a single queue name, array with multiple.
6478 */
65- public function __construct ($ queues )
79+ public function __construct (ReserverInterface $ reserver , $ queues )
6680 {
67- $ this ->logger = new Resque_Log ();
81+ $ this ->reserver = $ reserver ;
82+ $ this ->logger = new Resque_Log ();
6883
6984 if (!is_array ($ queues )) {
7085 $ queues = array ($ queues );
@@ -76,6 +91,16 @@ public function __construct($queues)
7691 $ this ->id = $ this ->hostname . ': ' .getmypid () . ': ' . implode (', ' , $ this ->queues );
7792 }
7893
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+
79104 /**
80105 * Return all workers known to Resque as instantiated instances.
81106 * @return array
@@ -119,7 +144,9 @@ public static function find($workerId)
119144
120145 list ($ hostname , $ pid , $ queues ) = explode (': ' , $ workerId , 3 );
121146 $ queues = explode (', ' , $ queues );
122- $ worker = new self ($ queues );
147+
148+ $ reserver = self ::$ reserverFactory ->createDefaultReserver ($ queues );
149+ $ worker = new self ($ reserver , $ queues );
123150 $ worker ->setId ($ workerId );
124151 return $ worker ;
125152 }
@@ -142,7 +169,7 @@ public function setId($workerId)
142169 *
143170 * @param int $interval How often to check for new jobs across the queues.
144171 */
145- public function work ($ interval = Resque::DEFAULT_INTERVAL , $ blocking = false )
172+ public function work ($ interval = Resque::DEFAULT_INTERVAL )
146173 {
147174 $ this ->updateProcLine ('Starting ' );
148175 $ this ->startup ();
@@ -154,36 +181,25 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
154181
155182 // Attempt to find and reserve a job
156183 $ 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 );
164186
165- $ job = $ this ->reserve ($ blocking , $ interval );
187+ $ job = $ this ->reserve ();
188+ } else {
189+ $ this ->updateProcLine ('Paused ' );
166190 }
167191
168- if (!$ job ) {
192+ if (!$ job ) {
169193 // For an interval of 0, break now - helps with unit testing etc
170- if ($ interval == 0 ) {
194+ if ($ interval == 0 ) {
171195 break ;
172196 }
173197
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 ()) {
177200 $ 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+ }
187203
188204 continue ;
189205 }
@@ -252,33 +268,11 @@ public function perform(Resque_Job $job)
252268 /**
253269 * @param bool $blocking
254270 * @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.
256272 */
257- public function reserve ($ blocking = false , $ timeout = null )
273+ public function reserve ()
258274 {
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 ;
282276 }
283277
284278 /**
0 commit comments