1111
1212class RabbitMQJob extends Job implements JobContract
1313{
14+ /**
15+ * Same as RabbitMQQueue, used for attempt counts
16+ */
17+ const ATTEMPT_COUNT_HEADERS_KEY = 'attempts_count ' ;
1418
1519 protected $ connection ;
1620 protected $ channel ;
1721 protected $ queue ;
1822 protected $ message ;
1923
24+ /**
25+ * Creates a new instance of RabbitMQJob.
26+ *
27+ * @param Illuminate\Container\Container $container
28+ * @param VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue $connection
29+ * @param PhpAmqpLib\Channel\AMQPChannel $channel
30+ * @param string $queue
31+ * @param PhpAmqpLib\Message\AMQPMessage $message
32+ *
33+ */
2034 public function __construct (
2135 Container $ container ,
2236 RabbitMQQueue $ connection ,
@@ -38,7 +52,7 @@ public function __construct(
3852 */
3953 public function fire ()
4054 {
41- $ this ->resolveAndFire (json_decode ( $ this ->message -> body , true ));
55+ $ this ->resolveAndFire ($ this ->getParsedBody ( ));
4256 }
4357
4458 /**
@@ -51,6 +65,16 @@ public function getRawBody()
5165 return $ this ->message ->body ;
5266 }
5367
68+ /**
69+ * Retrieves the parsed body for the job.
70+ *
71+ * @return array|false
72+ */
73+ public function getParsedBody ()
74+ {
75+ return json_decode ($ this ->getRawBody (), true );
76+ }
77+
5478 /**
5579 * Delete the job from the queue.
5680 *
@@ -59,12 +83,11 @@ public function getRawBody()
5983 public function delete ()
6084 {
6185 parent ::delete ();
62-
63- $ this ->channel ->basic_ack ($ this ->message ->delivery_info ['delivery_tag ' ]);
86+ $ this ->channel ->basic_ack ($ this ->message ->get ('delivery_tag ' ));
6487 }
6588
6689 /**
67- * Get queue name
90+ * Get the queue name.
6891 *
6992 * @return string
7093 */
@@ -83,16 +106,18 @@ public function getQueue()
83106 public function release ($ delay = 0 )
84107 {
85108 $ this ->delete ();
109+ $ this ->setAttempts ($ this ->attempts () + 1 );
86110
87- $ body = $ this ->message ->body ;
88- $ body = json_decode ($ body , true );
89-
90- $ attempts = $ this ->attempts ();
111+ $ body = $ this ->getParsedBody ();
91112
92- $ job = unserialize ($ body ['data ' ]['command ' ]);
93-
94- // write attempts to job
95- $ job ->attempts = $ attempts + 1 ;
113+ /**
114+ * Some jobs don't have the command set, so fall back to just sending it the job name string
115+ */
116+ if (isset ($ body ['data ' ]['command ' ]) === true ) {
117+ $ job = unserialize ($ body ['data ' ]['command ' ]);
118+ } else {
119+ $ job = $ this ->getName ();
120+ }
96121
97122 $ data = $ body ['data ' ];
98123
@@ -110,15 +135,28 @@ public function release($delay = 0)
110135 */
111136 public function attempts ()
112137 {
113- $ body = json_decode ($ this ->message ->body , true );
114- $ job = unserialize ($ body ['data ' ]['command ' ]);
115- if (is_object ($ job ) && property_exists ($ job , 'attempts ' ))
116- {
117- return (int ) $ job ->attempts ;
138+ if ($ this ->message ->has ('application_headers ' ) === true ) {
139+ $ headers = $ this ->message ->get ('application_headers ' )->getNativeData ();
140+
141+ if (isset ($ headers [self ::ATTEMPT_COUNT_HEADERS_KEY ]) === true ) {
142+ return $ headers [self ::ATTEMPT_COUNT_HEADERS_KEY ];
143+ }
118144 }
119145 return 0 ;
120146 }
121147
148+ /**
149+ * Sets the count of attempts at processing this job.
150+ *
151+ * @param int $count
152+ *
153+ * @return void
154+ */
155+ private function setAttempts ($ count )
156+ {
157+ $ this ->connection ->setAttempts ($ count );
158+ }
159+
122160 /**
123161 * Get the job identifier.
124162 *
@@ -128,5 +166,4 @@ public function getJobId()
128166 {
129167 return $ this ->message ->get ('correlation_id ' );
130168 }
131-
132169}
0 commit comments