Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key and partition selector #123

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Tests export-ignore
24 changes: 24 additions & 0 deletions EnvelopeItem/KeyStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Enqueue\MessengerAdapter\EnvelopeItem;

use Interop\Queue\Message;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Stamp\StampInterface;

class KeyStamp implements StampInterface
{
private string $key;

public function getKey(): ?string
{
return $this->key;
}

public function setKey(string $key = null): self
{
$this->key = $key;

return $this;
}
}
24 changes: 24 additions & 0 deletions EnvelopeItem/PartitionStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Enqueue\MessengerAdapter\EnvelopeItem;

use Interop\Queue\Message;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Stamp\StampInterface;

class PartitionStamp implements StampInterface
{
private int $partition;

public function getPartition(): int
{
return $this->partition;
}

public function setPartition(int $partition): self
{
$this->partition = $partition;

return $this;
}
}
12 changes: 12 additions & 0 deletions QueueInteropTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Enqueue\AmqpTools\RabbitMqDelayPluginDelayStrategy;
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
use Enqueue\MessengerAdapter\EnvelopeItem\InteropMessageStamp;
use Enqueue\MessengerAdapter\EnvelopeItem\KeyStamp;
use Enqueue\MessengerAdapter\EnvelopeItem\PartitionStamp;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
use Enqueue\MessengerAdapter\Exception\MissingMessageMetadataSetterException;
use Enqueue\MessengerAdapter\Exception\SendingMessageFailedException;
Expand Down Expand Up @@ -168,6 +170,16 @@ public function send(Envelope $envelope): Envelope
$producer->setTimeToLive($this->options['timeToLive']);
}

$partitionStamp = $envelope->last(PartitionStamp::class);
if (method_exists($interopMessage, 'setPartition') && !is_null($partitionStamp)) {
$interopMessage->setPartition($partitionStamp->getPartition());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What class do you expect here?
Ie, it is not Interop\Queue\Message, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Nyholm

I expect to have a Enqueue\RdKafka\RdKafkaMessage

I don't remember why I choose to check the method instead of checking the class...

Copy link
Author

@cluster28 cluster28 May 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nyholm do you prefer that?

$partitionStamp = $envelope->last(PartitionStamp::class);
if ($interopMessage instanceof RdKafkaMessage && !is_null($partitionStamp)) {
    $interopMessage->setPartition($partitionStamp->getPartition());
}

Copy link
Author

@cluster28 cluster28 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

$keyStamp = $envelope->last(KeyStamp::class);
if (method_exists($interopMessage, 'setKey') && !is_null($keyStamp)) {
$interopMessage->setKey($keyStamp->getKey());
}

try {
$producer->send($topic, $interopMessage);
} catch (InteropQueueException $e) {
Expand Down
21 changes: 21 additions & 0 deletions Tests/EnvelopeItem/KeyStampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Enqueue\MessengerAdapter\Tests\EnvelopeItem;

use Enqueue\MessengerAdapter\EnvelopeItem\KeyStamp;
use PHPUnit\Framework\TestCase;

class KeyStampTest extends TestCase
{
public function testPartitionSetter()
{
$keyStamp = (new KeyStamp())->setKey('key');
$this->assertInstanceOf(KeyStamp::class, $keyStamp);
}

public function testPartitionGetter()
{
$keyStamp = (new KeyStamp())->setKey('key');
$this->assertEquals('key', $keyStamp->getKey());
}
}
21 changes: 21 additions & 0 deletions Tests/EnvelopeItem/PartitionStampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Enqueue\MessengerAdapter\Tests\EnvelopeItem;

use Enqueue\MessengerAdapter\EnvelopeItem\PartitionStamp;
use PHPUnit\Framework\TestCase;

class PartitionStampTest extends TestCase
{
public function testPartitionSetter()
{
$partitionStamp = (new PartitionStamp())->setPartition(1);
$this->assertInstanceOf(PartitionStamp::class, $partitionStamp);
}

public function testPartitionGetter()
{
$partitionStamp = (new PartitionStamp())->setPartition(1);
$this->assertEquals(1, $partitionStamp->getPartition());
}
}