77use Illuminate \Contracts \Queue \Queue as QueueContract ;
88use Illuminate \Queue \Queue ;
99use Log ;
10+ use Rapide \LaravelQueueKafka \Exceptions \QueueKafkaException ;
1011use Rapide \LaravelQueueKafka \Queue \Jobs \KafkaJob ;
1112
1213class KafkaQueue extends Queue implements QueueContract
@@ -90,17 +91,18 @@ public function push($job, $data = '', $queue = null)
9091 * @param array $options
9192 *
9293 * @return mixed
94+ * @throws QueueKafkaException
9395 */
9496 public function pushRaw ($ payload , $ queue = null , array $ options = [])
9597 {
9698 try {
9799 $ topic = $ this ->getTopic ($ queue );
98100
99- $ correlationId = $ this ->getCorrelationId ();
101+ $ pushRawCorrelationId = $ this ->getCorrelationId ();
100102
101- $ topic ->produce (RD_KAFKA_PARTITION_UA , 0 , $ payload , $ correlationId );
103+ $ topic ->produce (RD_KAFKA_PARTITION_UA , 0 , $ payload , $ pushRawCorrelationId );
102104
103- return $ correlationId ;
105+ return $ pushRawCorrelationId ;
104106 } catch (ErrorException $ exception ) {
105107 $ this ->reportConnectionError ('pushRaw ' , $ exception );
106108 }
@@ -119,7 +121,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
119121 public function later ($ delay , $ job , $ data = '' , $ queue = null )
120122 {
121123 //Later is not sup
122- throw new Exception ('Later not yet implemented ' );
124+ throw new QueueKafkaException ('Later not yet implemented ' );
123125 }
124126
125127 /**
@@ -128,33 +130,37 @@ public function later($delay, $job, $data = '', $queue = null)
128130 * @param string|null $queue
129131 *
130132 * @return \Illuminate\Queue\Jobs\Job|null
133+ * @throws QueueKafkaException
131134 */
132135 public function pop ($ queue = null )
133136 {
134- $ queue = $ this ->getQueueName ($ queue );
135- if (!in_array ($ queue , $ this ->subscribedQueueNames )) {
136- $ this ->subscribedQueueNames [] = $ queue ;
137- $ this ->consumer ->subscribe ($ this ->subscribedQueueNames );
138- }
137+ try {
138+ $ queue = $ this ->getQueueName ($ queue );
139+ if (!in_array ($ queue , $ this ->subscribedQueueNames )) {
140+ $ this ->subscribedQueueNames [] = $ queue ;
141+ $ this ->consumer ->subscribe ($ this ->subscribedQueueNames );
142+ }
139143
140- $ message = $ this ->consumer ->consume (1000 );
144+ $ message = $ this ->consumer ->consume (1000 );
141145
142- if ($ message === null ) {
143- return ;
144- }
146+ if ($ message === null ) {
147+ return null ;
148+ }
145149
146- switch ($ message ->err ) {
147- case RD_KAFKA_RESP_ERR_NO_ERROR :
148- return new KafkaJob (
149- $ this ->container , $ this , $ message ,
150- $ this ->connectionName , $ queue ?: $ this ->defaultQueue
151- );
152- break ;
153- case RD_KAFKA_RESP_ERR__PARTITION_EOF :
154- case RD_KAFKA_RESP_ERR__TIMED_OUT :
155- break ;
156- default :
157- throw new \Exception ($ message ->errstr (), $ message ->err );
150+ switch ($ message ->err ) {
151+ case RD_KAFKA_RESP_ERR_NO_ERROR :
152+ return new KafkaJob (
153+ $ this ->container , $ this , $ message ,
154+ $ this ->connectionName , $ queue ?: $ this ->defaultQueue
155+ );
156+ case RD_KAFKA_RESP_ERR__PARTITION_EOF :
157+ case RD_KAFKA_RESP_ERR__TIMED_OUT :
158+ break ;
159+ default :
160+ throw new QueueKafkaException ($ message ->errstr (), $ message ->err );
161+ }
162+ } catch (\RdKafka \Exception $ exception ) {
163+ throw new QueueKafkaException ('Could not pop from the queue ' , 0 , $ exception );
158164 }
159165 }
160166
@@ -229,15 +235,15 @@ protected function createPayloadArray($job, $data = '', $queue = null)
229235 * @param string $action
230236 * @param Exception $e
231237 *
232- * @throws Exception
238+ * @throws QueueKafkaException
233239 */
234240 protected function reportConnectionError ($ action , Exception $ e )
235241 {
236242 Log::error ('Kafka error while attempting ' . $ action . ': ' . $ e ->getMessage ());
237243
238244 // If it's set to false, throw an error rather than waiting
239245 if ($ this ->sleepOnError === false ) {
240- throw new \ RuntimeException ('Error writing data to the connection with Kafka ' );
246+ throw new QueueKafkaException ('Error writing data to the connection with Kafka ' );
241247 }
242248
243249 // Sleep so that we don't flood the log file
0 commit comments