1616use Bdf \Queue \Message \QueuedMessage ;
1717use Exception ;
1818use Pheanstalk \Exception \ClientException ;
19+ use Pheanstalk \Exception \ConnectionException as PheanstalkConnectionException ;
1920use Pheanstalk \Exception \ServerException as BaseServerException ;
2021use Pheanstalk \Exception \SocketException ;
2122use Pheanstalk \Job as PheanstalkJob ;
2223use Pheanstalk \Pheanstalk ;
2324
25+ use Pheanstalk \Values \Job as Pheanstalk5Job ;
26+ use Pheanstalk \Values \TubeName ;
27+
28+ use Pheanstalk \Values \TubeStats ;
29+
30+ use function class_exists ;
2431use function method_exists ;
2532
2633/**
@@ -48,15 +55,23 @@ public function push(Message $message): void
4855 {
4956 $ message ->setQueuedAt (new \DateTimeImmutable ());
5057 $ pheanstalk = $ this ->connection ->pheanstalk ();
58+ $ queue = $ message ->queue ();
59+
60+ if (class_exists (TubeName::class)) {
61+ // Support for Pheanstalk 5
62+ $ queue = new TubeName ($ queue );
63+ }
5164
5265 try {
53- $ pheanstalk ->useTube ($ message ->queue ())->put (
66+ $ pheanstalk ->useTube ($ queue );
67+
68+ $ pheanstalk ->put (
5469 $ this ->connection ->serializer ()->serialize ($ message ),
5570 $ message ->header ('priority ' , Pheanstalk::DEFAULT_PRIORITY ),
5671 $ message ->delay (),
5772 $ message ->header ('ttr ' , $ this ->connection ->timeToRun ())
5873 );
59- } catch (SocketException $ e ) {
74+ } catch (SocketException | PheanstalkConnectionException $ e ) {
6075 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
6176 } catch (BaseServerException $ e ) {
6277 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -72,14 +87,21 @@ public function pushRaw($raw, string $queue, int $delay = 0): void
7287 {
7388 $ pheanstalk = $ this ->connection ->pheanstalk ();
7489
90+ if (class_exists (TubeName::class)) {
91+ // Support for Pheanstalk 5
92+ $ queue = new TubeName ($ queue );
93+ }
94+
7595 try {
76- $ pheanstalk ->useTube ($ queue )->put (
96+ $ pheanstalk ->useTube ($ queue );
97+
98+ $ pheanstalk ->put (
7799 $ raw ,
78100 Pheanstalk::DEFAULT_PRIORITY ,
79101 $ delay ,
80102 $ this ->connection ->timeToRun ()
81103 );
82- } catch (SocketException $ e ) {
104+ } catch (SocketException | PheanstalkConnectionException $ e ) {
83105 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
84106 } catch (BaseServerException $ e ) {
85107 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -95,24 +117,41 @@ public function pop(string $queue, int $duration = ConnectionDriverInterface::DU
95117 {
96118 $ pheanstalk = $ this ->connection ->pheanstalk ();
97119
120+ if (class_exists (TubeName::class)) {
121+ // Support for Pheanstalk 5
122+ $ queue = new TubeName ($ queue );
123+ }
124+
98125 try {
99- $ pheanstalk = $ pheanstalk ->watchOnly ($ queue );
126+ if (method_exists ($ pheanstalk , 'watchOnly ' )) {
127+ // Pheanstalk < 5
128+ $ pheanstalk ->watchOnly ($ queue );
129+ } else {
130+ // Pheanstalk 5
131+ $ pheanstalk ->watch ($ queue );
132+
133+ foreach ($ pheanstalk ->listTubesWatched () as $ tube ) {
134+ if ($ tube != $ queue ) {
135+ $ pheanstalk ->ignore ($ tube );
136+ }
137+ }
138+ }
100139
101140 if (method_exists ($ pheanstalk , 'reserveWithTimeout ' )) {
102141 $ job = $ pheanstalk ->reserveWithTimeout ($ duration );
103142 } else {
104143 // Support for Pheanstalk 3
105144 $ job = $ pheanstalk ->reserve ($ duration );
106145 }
107- } catch (SocketException $ e ) {
146+ } catch (SocketException | PheanstalkConnectionException $ e ) {
108147 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
109148 } catch (BaseServerException $ e ) {
110149 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
111150 } catch (ClientException $ e ) {
112151 throw new ConnectionException ($ e ->getMessage (), $ e ->getCode (), $ e );
113152 }
114153
115- if (!$ job instanceof PheanstalkJob) {
154+ if (!$ job instanceof PheanstalkJob && ! $ job instanceof Pheanstalk5Job ) {
116155 return null ;
117156 }
118157
@@ -128,7 +167,7 @@ public function acknowledge(QueuedMessage $message): void
128167 {
129168 try {
130169 $ this ->connection ->pheanstalk ()->delete ($ message ->internalJob ());
131- } catch (SocketException $ e ) {
170+ } catch (SocketException | PheanstalkConnectionException $ e ) {
132171 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
133172 } catch (BaseServerException $ e ) {
134173 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -148,7 +187,7 @@ public function release(QueuedMessage $message): void
148187 $ message ->header ('priority ' , Pheanstalk::DEFAULT_PRIORITY ),
149188 $ message ->delay ()
150189 );
151- } catch (SocketException $ e ) {
190+ } catch (SocketException | PheanstalkConnectionException $ e ) {
152191 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
153192 } catch (BaseServerException $ e ) {
154193 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -162,8 +201,19 @@ public function release(QueuedMessage $message): void
162201 */
163202 public function count (string $ name ): int
164203 {
204+ if (class_exists (TubeName::class)) {
205+ // Support for Pheanstalk 5
206+ $ name = new TubeName ($ name );
207+ }
208+
165209 try {
166- return $ this ->connection ->pheanstalk ()->statsTube ($ name )['current-jobs-ready ' ];
210+ $ stats = $ this ->connection ->pheanstalk ()->statsTube ($ name );
211+
212+ if ($ stats instanceof TubeStats) {
213+ return $ stats ->currentJobsReady ;
214+ } else {
215+ return $ stats ['current-jobs-ready ' ];
216+ }
167217 } catch (Exception $ e ) {
168218 return 0 ;
169219 }
@@ -186,7 +236,7 @@ public function stats(): array
186236 try {
187237 $ queuesInfo = array_merge ($ queuesInfo , $ this ->queuesInfo ($ pheanstalk , $ host , $ port ));
188238 $ workersInfo = array_merge ($ workersInfo , $ this ->workersInfo ($ pheanstalk , $ host , $ port ));
189- } catch (SocketException $ e ) {
239+ } catch (SocketException | PheanstalkConnectionException $ e ) {
190240 throw new ConnectionLostException ($ e ->getMessage (), $ e ->getCode (), $ e );
191241 } catch (BaseServerException $ e ) {
192242 throw new ServerException ($ e ->getMessage (), $ e ->getCode (), $ e );
@@ -214,21 +264,37 @@ private function queuesInfo($pheanstalk, string $host, int $port): array
214264
215265 foreach ($ pheanstalk ->listTubes () as $ tube ) {
216266 try {
217- /** @var \Pheanstalk\Response\ArrayResponse $stats */
267+ /** @var \Pheanstalk\Response\ArrayResponse|TubeStats $stats */
218268 $ stats = $ pheanstalk ->statsTube ($ tube );
219269
220- $ status [] = [
221- 'host ' => $ host .': ' .$ port ,
222- 'queue ' => $ stats ['name ' ],
223- 'jobs in queue ' => $ stats ['current-jobs-ready ' ],
224- 'jobs running ' => $ stats ['current-jobs-reserved ' ],
225- 'jobs delayed ' => $ stats ['current-jobs-delayed ' ],
226- // 'jobs buried' => $stats['current-jobs-buried'],
227- 'total jobs ' => $ stats ['total-jobs ' ],
228- // 'workers using' => $stats['current-using'],
229- 'workers waiting ' => $ stats ['current-waiting ' ],
230- 'workers watching ' => --$ stats ['current-watching ' ], // remove the monitoring
231- ];
270+ if ($ stats instanceof TubeStats) {
271+ // Pheanstalk 5
272+ $ status [] = [
273+ 'host ' => $ host .': ' .$ port ,
274+ 'queue ' => $ stats ->name ->value ,
275+ 'jobs in queue ' => $ stats ->currentJobsReady ,
276+ 'jobs running ' => $ stats ->currentJobsReserved ,
277+ 'jobs delayed ' => $ stats ->currentJobsDelayed ,
278+ // 'jobs buried' => $stats['current-jobs-buried'],
279+ 'total jobs ' => $ stats ->totalJobs ,
280+ // 'workers using' => $stats['current-using'],
281+ 'workers waiting ' => $ stats ->currentWaiting ,
282+ 'workers watching ' => $ stats ->currentWatching - 1 , // remove the monitoring
283+ ];
284+ } else {
285+ $ status [] = [
286+ 'host ' => $ host .': ' .$ port ,
287+ 'queue ' => $ stats ['name ' ],
288+ 'jobs in queue ' => $ stats ['current-jobs-ready ' ],
289+ 'jobs running ' => $ stats ['current-jobs-reserved ' ],
290+ 'jobs delayed ' => $ stats ['current-jobs-delayed ' ],
291+ // 'jobs buried' => $stats['current-jobs-buried'],
292+ 'total jobs ' => $ stats ['total-jobs ' ],
293+ // 'workers using' => $stats['current-using'],
294+ 'workers waiting ' => $ stats ['current-waiting ' ],
295+ 'workers watching ' => --$ stats ['current-watching ' ], // remove the monitoring
296+ ];
297+ }
232298 } catch (Exception $ e ) {
233299 // tube not found
234300 }
0 commit comments