From a7235b92ead87840cfb1093ac63e743ee28dc3c3 Mon Sep 17 00:00:00 2001 From: aliozkan Date: Sun, 3 Mar 2024 23:45:04 +0300 Subject: [PATCH] Add orderingKey support (#70) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ali Özkan --- src/PubSubQueue.php | 15 ++++++++++++--- src/Traits/HasOrderingKey.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/Traits/HasOrderingKey.php diff --git a/src/PubSubQueue.php b/src/PubSubQueue.php index 401863e..ff707c2 100644 --- a/src/PubSubQueue.php +++ b/src/PubSubQueue.php @@ -93,7 +93,13 @@ public function size($queue = null) */ public function push($job, $data = '', $queue = null) { - return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue); + $options = []; + + if (isset($job->orderingKey)) { + $options['orderingKey'] = $job->orderingKey; + } + + return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue, $options); } /** @@ -114,8 +120,11 @@ public function pushRaw($payload, $queue = null, array $options = []) if (! empty($options)) { $publish['attributes'] = $this->validateMessageAttributes($options); - } + if (isset($options['orderingKey'])) { + $publish['orderingKey'] = $options['orderingKey']; + } + } $topic->publish($publish); $decoded_payload = json_decode($payload, true); @@ -195,7 +204,7 @@ public function bulk($jobs, $data = '', $queue = null) foreach ((array) $jobs as $job) { $payload = $this->createPayload($job, $this->getQueue($queue), $data); - $payloads[] = ['data' => base64_encode($payload)]; + $payloads[] = ['data' => base64_encode($payload)] + (isset($job->orderingKey) ? ['orderingKey' => $job->orderingKey] : []); } $topic = $this->getTopic($this->getQueue($queue), $this->topicAutoCreation); diff --git a/src/Traits/HasOrderingKey.php b/src/Traits/HasOrderingKey.php new file mode 100644 index 0000000..16f7e30 --- /dev/null +++ b/src/Traits/HasOrderingKey.php @@ -0,0 +1,15 @@ +orderingKey = $orderingKey; + + return $this; + } +}