Skip to content

Commit a7235b9

Browse files
authored
Add orderingKey support (#70)
Co-authored-by: Ali Özkan <[email protected]>
1 parent 470ac83 commit a7235b9

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/PubSubQueue.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ public function size($queue = null)
9393
*/
9494
public function push($job, $data = '', $queue = null)
9595
{
96-
return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue);
96+
$options = [];
97+
98+
if (isset($job->orderingKey)) {
99+
$options['orderingKey'] = $job->orderingKey;
100+
}
101+
102+
return $this->pushRaw($this->createPayload($job, $this->getQueue($queue), $data), $queue, $options);
97103
}
98104

99105
/**
@@ -114,8 +120,11 @@ public function pushRaw($payload, $queue = null, array $options = [])
114120

115121
if (! empty($options)) {
116122
$publish['attributes'] = $this->validateMessageAttributes($options);
117-
}
118123

124+
if (isset($options['orderingKey'])) {
125+
$publish['orderingKey'] = $options['orderingKey'];
126+
}
127+
}
119128
$topic->publish($publish);
120129

121130
$decoded_payload = json_decode($payload, true);
@@ -195,7 +204,7 @@ public function bulk($jobs, $data = '', $queue = null)
195204

196205
foreach ((array) $jobs as $job) {
197206
$payload = $this->createPayload($job, $this->getQueue($queue), $data);
198-
$payloads[] = ['data' => base64_encode($payload)];
207+
$payloads[] = ['data' => base64_encode($payload)] + (isset($job->orderingKey) ? ['orderingKey' => $job->orderingKey] : []);
199208
}
200209

201210
$topic = $this->getTopic($this->getQueue($queue), $this->topicAutoCreation);

src/Traits/HasOrderingKey.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Kainxspirits\PubSubQueue\Traits;
4+
5+
trait HasOrderingKey
6+
{
7+
public $orderingKey;
8+
9+
public function setOrderingKey(string $orderingKey): self
10+
{
11+
$this->orderingKey = $orderingKey;
12+
13+
return $this;
14+
}
15+
}

0 commit comments

Comments
 (0)