66use Forumhouse \LaravelAmqp \Jobs \AMQPJob ;
77use Forumhouse \LaravelAmqp \Utility \ArrayUtil ;
88use Illuminate \Contracts \Queue \Queue as QueueContract ;
9+ use Illuminate \Queue \InvalidPayloadException ;
910use Illuminate \Queue \Queue ;
1011use PhpAmqpLib \Channel \AMQPChannel ;
1112use PhpAmqpLib \Connection \AMQPConnection ;
@@ -68,6 +69,11 @@ class AMQPQueue extends Queue implements QueueContract
6869 */
6970 private $ declareQueues ;
7071
72+ /**
73+ * @var int
74+ */
75+ private $ retryAfter ;
76+
7177 /**
7278 * @param AMQPStreamConnection $connection
7379 * @param string $defaultQueueName Default queue name
@@ -82,6 +88,7 @@ class AMQPQueue extends Queue implements QueueContract
8288 * @param string $exchangeName Exchange name
8389 * @param mixed $exchangeType Exchange type
8490 * @param mixed $exchangeFlags Exchange flags
91+ * @param mixed $retryAfter Optional timeout for failed jobs
8592 */
8693 public function __construct (
8794 AMQPStreamConnection $ connection ,
@@ -92,7 +99,8 @@ public function __construct(
9299 $ defaultChannelId = null ,
93100 $ exchangeName = '' ,
94101 $ exchangeType = null ,
95- $ exchangeFlags = []
102+ $ exchangeFlags = [],
103+ $ retryAfter = 0
96104 ) {
97105 $ this ->connection = $ connection ;
98106 $ this ->defaultQueueName = $ defaultQueueName ?: 'default ' ;
@@ -102,6 +110,7 @@ public function __construct(
102110 $ this ->defaultChannelId = $ defaultChannelId ;
103111 $ this ->exchangeName = $ exchangeName ;
104112 $ this ->channel = $ connection ->channel ($ this ->defaultChannelId );
113+ $ this ->retryAfter = $ retryAfter ;
105114
106115 if ($ exchangeName !== null ) {
107116 $ this ->declareExchange ($ exchangeName , $ exchangeType , $ exchangeFlags );
@@ -134,6 +143,13 @@ protected function declareExchange($exchangeName, $exchangeType, array $exchange
134143 call_user_func_array ([$ this ->channel , 'exchange_declare ' ], $ flags );
135144 }
136145
146+ /**
147+ * @return array
148+ */
149+ public function getCustomMessageOptions (){
150+ return ['retryAfter ' => $ this ->retryAfter ];
151+ }
152+
137153 /**
138154 * Push a new job onto the queue.
139155 *
@@ -410,4 +426,47 @@ private function prepareQueue($queue)
410426
411427 return $ queue ;
412428 }
429+
430+ /**
431+ * Create a payload string from the given job and data.
432+ *
433+ * @param string $job
434+ * @param mixed $data
435+ * @return string
436+ *
437+ * @throws \Illuminate\Queue\InvalidPayloadException
438+ */
439+ protected function createPayload ($ job , $ data = '' )
440+ {
441+ $ data = is_array ($ data ) ? array_merge ($ data , $ this ->getCustomMessageOptions ()) : $ this ->getCustomMessageOptions ();
442+ $ payload = json_encode ($ this ->createPayloadArray ($ job , $ data ));
443+ if (JSON_ERROR_NONE !== json_last_error ()) {
444+ throw new InvalidPayloadException (
445+ 'Unable to JSON encode payload. Error code: ' .json_last_error ()
446+ );
447+ }
448+
449+ return $ payload ;
450+ }
451+
452+ /**
453+ * Create a payload for an object-based queue handler.
454+ *
455+ * @param mixed $job
456+ * @return array
457+ */
458+ protected function createObjectPayload ($ job )
459+ {
460+ return [
461+ 'displayName ' => $ this ->getDisplayName ($ job ),
462+ 'job ' => 'Illuminate\Queue\CallQueuedHandler@call ' ,
463+ 'maxTries ' => $ job ->tries ?? null ,
464+ 'timeout ' => $ job ->timeout ?? null ,
465+ 'timeoutAt ' => $ this ->getJobExpiration ($ job ),
466+ 'data ' => array_merge ([
467+ 'commandName ' => get_class ($ job ),
468+ 'command ' => serialize (clone $ job ),
469+ ],$ this ->getCustomMessageOptions ()),
470+ ];
471+ }
413472}
0 commit comments