Skip to content

Commit

Permalink
Add orderingKey support (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Ali Özkan <[email protected]>
  • Loading branch information
aliozkan and aliozkan authored Mar 3, 2024
1 parent 470ac83 commit a7235b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/PubSubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/Traits/HasOrderingKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Kainxspirits\PubSubQueue\Traits;

trait HasOrderingKey
{
public $orderingKey;

public function setOrderingKey(string $orderingKey): self
{
$this->orderingKey = $orderingKey;

return $this;
}
}

0 comments on commit a7235b9

Please sign in to comment.