4
4
5
5
use Exception ;
6
6
use Illuminate \Container \Container ;
7
- use Illuminate \Contracts \Queue \Job ;
8
7
use Illuminate \Queue \Worker ;
9
8
use Illuminate \Queue \WorkerOptions ;
10
9
use PhpAmqpLib \Channel \AMQPChannel ;
11
10
use PhpAmqpLib \Exception \AMQPRuntimeException ;
12
11
use PhpAmqpLib \Message \AMQPMessage ;
13
- use Symfony \Component \Debug \Exception \FatalThrowableError ;
14
12
use Throwable ;
15
13
use VladimirYuldashev \LaravelQueueRabbitMQ \Queue \RabbitMQQueue ;
16
14
@@ -31,8 +29,8 @@ class Consumer extends Worker
31
29
/** @var AMQPChannel */
32
30
protected $ channel ;
33
31
34
- /** @var bool */
35
- protected $ gotJob = false ;
32
+ /** @var object|null */
33
+ protected $ currentJob ;
36
34
37
35
public function setContainer (Container $ value ): void
38
36
{
@@ -54,14 +52,25 @@ public function setPrefetchCount(int $value): void
54
52
$ this ->prefetchCount = $ value ;
55
53
}
56
54
57
- public function daemon ($ connectionName , $ queue , WorkerOptions $ options ): void
55
+ /**
56
+ * Listen to the given queue in a loop.
57
+ *
58
+ * @param string $connectionName
59
+ * @param string $queue
60
+ * @param WorkerOptions $options
61
+ * @return int
62
+ * @throws Throwable
63
+ */
64
+ public function daemon ($ connectionName , $ queue , WorkerOptions $ options )
58
65
{
59
66
if ($ this ->supportsAsyncSignals ()) {
60
67
$ this ->listenForSignals ();
61
68
}
62
69
63
70
$ lastRestart = $ this ->getTimestampOfLastQueueRestart ();
64
71
72
+ [$ startTime , $ jobsProcessed ] = [hrtime (true ) / 1e9 , 0 ];
73
+
65
74
/** @var RabbitMQQueue $connection */
66
75
$ connection = $ this ->manager ->connection ($ connectionName );
67
76
@@ -82,9 +91,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options): void
82
91
false ,
83
92
false ,
84
93
false ,
85
- function (AMQPMessage $ message ) use ($ connection , $ options , $ connectionName , $ queue , $ jobClass ): void {
86
- $ this ->gotJob = true ;
87
-
94
+ function (AMQPMessage $ message ) use ($ connection , $ options , $ connectionName , $ queue , $ jobClass , &$ jobsProcessed ): void {
88
95
$ job = new $ jobClass (
89
96
$ this ->container ,
90
97
$ connection ,
@@ -93,10 +100,14 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
93
100
$ queue
94
101
);
95
102
103
+ $ this ->currentJob = $ job ;
104
+
96
105
if ($ this ->supportsAsyncSignals ()) {
97
106
$ this ->registerTimeoutHandler ($ job , $ options );
98
107
}
99
108
109
+ $ jobsProcessed ++;
110
+
100
111
$ this ->runJob ($ job , $ connectionName , $ options );
101
112
102
113
if ($ this ->supportsAsyncSignals ()) {
@@ -121,27 +132,33 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
121
132
$ this ->exceptions ->report ($ exception );
122
133
123
134
$ this ->kill (1 );
124
- } catch (Exception $ exception ) {
135
+ } catch (Exception | Throwable $ exception ) {
125
136
$ this ->exceptions ->report ($ exception );
126
137
127
- $ this ->stopWorkerIfLostConnection ($ exception );
128
- } catch (Throwable $ exception ) {
129
- $ this ->exceptions ->report ($ exception = new FatalThrowableError ($ exception ));
130
-
131
138
$ this ->stopWorkerIfLostConnection ($ exception );
132
139
}
133
140
134
141
// If no job is got off the queue, we will need to sleep the worker.
135
- if (! $ this ->gotJob ) {
142
+ if ($ this ->currentJob === null ) {
136
143
$ this ->sleep ($ options ->sleep );
137
144
}
138
145
139
146
// Finally, we will check to see if we have exceeded our memory limits or if
140
147
// the queue should restart based on other indications. If so, we'll stop
141
148
// this worker and let whatever is "monitoring" it restart the process.
142
- $ this ->stopIfNecessary ($ options , $ lastRestart , $ this ->gotJob ? true : null );
149
+ $ status = $ this ->stopIfNecessary (
150
+ $ options ,
151
+ $ lastRestart ,
152
+ $ startTime ,
153
+ $ jobsProcessed ,
154
+ $ this ->currentJob
155
+ );
156
+
157
+ if (! is_null ($ status )) {
158
+ return $ this ->stop ($ status );
159
+ }
143
160
144
- $ this ->gotJob = false ;
161
+ $ this ->currentJob = null ;
145
162
}
146
163
}
147
164
@@ -162,14 +179,14 @@ protected function daemonShouldRun(WorkerOptions $options, $connectionName, $que
162
179
* Stop listening and bail out of the script.
163
180
*
164
181
* @param int $status
165
- * @return void
182
+ * @return int
166
183
*/
167
- public function stop ($ status = 0 ): void
184
+ public function stop ($ status = 0 ): int
168
185
{
169
186
// Tell the server you are going to stop consuming.
170
187
// It will finish up the last message and not send you any more.
171
188
$ this ->channel ->basic_cancel ($ this ->consumerTag , false , true );
172
189
173
- parent ::stop ($ status );
190
+ return parent ::stop ($ status );
174
191
}
175
192
}
0 commit comments