1212use Throwable ;
1313use iamfarhad \LaravelRabbitMQ \RabbitQueue ;
1414
15- class Consumer extends Worker
15+ final class Consumer extends Worker
1616{
17- protected Container $ container ;
17+ private Container $ container ;
1818
19- protected string $ consumerTag ;
19+ private string $ consumerTag ;
2020
21- protected int $ prefetchSize ;
21+ private int $ prefetchSize ;
2222
23- protected int $ maxPriority ;
23+ private int $ maxPriority ;
2424
25- protected int $ prefetchCount ;
25+ private int $ prefetchCount ;
2626
27- protected AMQPChannel $ channel ;
27+ private AMQPChannel $ amqpChannel ;
2828
29- private $ currentJob ;
29+ private ? object $ currentJob = null ;
3030
31- public function setContainer (Container $ value ): void
31+ public function setContainer (Container $ container ): void
3232 {
33- $ this ->container = $ value ;
33+ $ this ->container = $ container ;
3434 }
3535
3636 public function setConsumerTag (string $ value ): void
@@ -58,62 +58,60 @@ public function setPrefetchCount(int $value): void
5858 *
5959 * @param string $connectionName
6060 * @param string $queue
61- * @param WorkerOptions $options
6261 * @return int
63- *
6462 * @throws Throwable
6563 */
66- public function daemon ($ connectionName , $ queue , WorkerOptions $ options )
64+ public function daemon ($ connectionName , $ queue , WorkerOptions $ workerOptions )
6765 {
6866 if ($ this ->supportsAsyncSignals ()) {
6967 $ this ->listenForSignals ();
7068 }
7169
72- $ lastRestart = $ this ->getTimestampOfLastQueueRestart ();
73-
74- [ $ startTime , $ jobsProcessed] = [ hrtime ( true ) / 1e9 , 0 ] ;
70+ $ timestampOfLastQueueRestart = $ this ->getTimestampOfLastQueueRestart ();
71+ $ startTime = hrtime ( true ) / 1e9 ;
72+ $ jobsProcessed = 0 ;
7573
7674 $ connection = $ this ->manager ->connection ($ connectionName );
7775
78- $ this ->channel = $ connection ->getChannel ();
76+ $ this ->amqpChannel = $ connection ->getChannel ();
7977
80- $ this ->channel ->basic_qos (
78+ $ this ->amqpChannel ->basic_qos (
8179 $ this ->prefetchSize ,
8280 $ this ->prefetchCount ,
8381 null
8482 );
8583
8684 $ jobClass = $ connection ->getJobClass ();
8785 $ arguments = [];
88- if ($ this ->maxPriority ) {
86+ if ($ this ->maxPriority !== 0 ) {
8987 $ arguments ['priority ' ] = ['I ' , $ this ->maxPriority ];
9088 }
9189
92- $ this ->channel ->basic_consume (
90+ $ this ->amqpChannel ->basic_consume (
9391 $ queue ,
9492 $ this ->consumerTag ,
9593 false ,
9694 false ,
9795 false ,
9896 false ,
99- function (AMQPMessage $ message ) use ($ connection , $ options , $ connectionName , $ queue , $ jobClass , &$ jobsProcessed ): void {
97+ function (AMQPMessage $ amqpMessage ) use ($ connection , $ workerOptions , $ connectionName , $ queue , $ jobClass , &$ jobsProcessed ): void {
10098 $ job = new $ jobClass (
10199 $ this ->container ,
102100 $ connection ,
103- $ message ,
101+ $ amqpMessage ,
104102 $ connectionName ,
105103 $ queue
106104 );
107105
108106 $ this ->currentJob = $ job ;
109107
110108 if ($ this ->supportsAsyncSignals ()) {
111- $ this ->registerTimeoutHandler ($ job , $ options );
109+ $ this ->registerTimeoutHandler ($ job , $ workerOptions );
112110 }
113111
114- $ jobsProcessed ++ ;
112+ ++ $ jobsProcessed ;
115113
116- $ this ->runJob ($ job , $ connectionName , $ options );
114+ $ this ->runJob ($ job , $ connectionName , $ workerOptions );
117115
118116 if ($ this ->supportsAsyncSignals ()) {
119117 $ this ->resetTimeoutHandler ();
@@ -123,21 +121,21 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
123121 $ arguments
124122 );
125123
126- while ($ this ->channel ->is_consuming ()) {
124+ while ($ this ->amqpChannel ->is_consuming ()) {
127125 // Before reserving any jobs, we will make sure this queue is not paused and
128126 // if it is we will just pause this worker for a given amount of time and
129127 // make sure we do not need to kill this worker process off completely.
130- if (! $ this ->daemonShouldRun ($ options , $ connectionName , $ queue )) {
131- $ this ->pauseWorker ($ options , $ lastRestart );
128+ if (! $ this ->daemonShouldRun ($ workerOptions , $ connectionName , $ queue )) {
129+ $ this ->pauseWorker ($ workerOptions , $ timestampOfLastQueueRestart );
132130
133131 continue ;
134132 }
135133
136134 // If the daemon should run (not in maintenance mode, etc.), then we can wait for a job.
137135 try {
138- $ this ->channel ->wait (null , true , (int ) $ options ->timeout );
139- } catch (AMQPRuntimeException $ exception ) {
140- $ this ->exceptions ->report ($ exception );
136+ $ this ->amqpChannel ->wait (null , true , (int ) $ workerOptions ->timeout );
137+ } catch (AMQPRuntimeException $ amqpRuntimeException ) {
138+ $ this ->exceptions ->report ($ amqpRuntimeException );
141139
142140 $ this ->kill (1 );
143141 } catch (Exception | Throwable $ exception ) {
@@ -148,15 +146,15 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
148146
149147 // If no job is got off the queue, we will need to sleep the worker.
150148 if ($ this ->currentJob === null ) {
151- $ this ->sleep ($ options ->sleep );
149+ $ this ->sleep ($ workerOptions ->sleep );
152150 }
153151
154152 // Finally, we will check to see if we have exceeded our memory limits or if
155153 // the queue should restart based on other indications. If so, we'll stop
156154 // this worker and let whatever is "monitoring" it restart the process.
157155 $ status = $ this ->stopIfNecessary (
158- $ options ,
159- $ lastRestart ,
156+ $ workerOptions ,
157+ $ timestampOfLastQueueRestart ,
160158 $ startTime ,
161159 $ jobsProcessed ,
162160 $ this ->currentJob
@@ -173,21 +171,19 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
173171 /**
174172 * Determine if the daemon should process on this iteration.
175173 *
176- * @param WorkerOptions $options
177174 * @param string $connectionName
178175 * @param string $queue
179- * @return bool
180176 */
181- protected function daemonShouldRun (WorkerOptions $ options , $ connectionName , $ queue ): bool
177+ protected function daemonShouldRun (WorkerOptions $ workerOptions , $ connectionName , $ queue ): bool
182178 {
183- return ! ((( $ this ->isDownForMaintenance )() && ! $ options ->force ) || $ this ->paused ) ;
179+ return !(( $ this ->isDownForMaintenance )() && ! $ workerOptions ->force ) && ! $ this ->paused ;
184180 }
185181
186182 public function stop ($ status = 0 , $ options = []): int
187183 {
188184 // Tell the server you are going to stop consuming.
189185 // It will finish up the last message and not send you anymore.
190- $ this ->channel ->basic_cancel ($ this ->consumerTag , false , true );
186+ $ this ->amqpChannel ->basic_cancel ($ this ->consumerTag , false , true );
191187
192188 return parent ::stop ($ status );
193189 }
0 commit comments