8000 [Messenger] Add `BeanstalkdPriorityStamp` to Beanstalkd bridge by HypeMC · Pull Request #59273 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add BeanstalkdPriorityStamp to Beanstalkd bridge #59273

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

Merged
merged 1 commit into from
Jan 5, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `BeanstalkdPriorityStamp` option to allow setting the message priority

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdPriorityStamp;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\BeanstalkdSender;
use Symfony\Component\Messenger\Bridge\Beanstalkd\Transport\Connection;
use Symfony\Component\Messenger\Envelope;
Expand All @@ -27,7 +28,7 @@ public function testSend()
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 0);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 0, null);

$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturn($encoded);
Expand All @@ -42,7 +43,22 @@ public function testSendWithDelay()
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 500);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 500, null);

$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturn($encoded);

$sender = new BeanstalkdSender($connection, $serializer);
$sender->send($envelope);
}

public function testSendWithPriority()
{
$envelope = (new Envelope(new DummyMessage('Oy')))->with(new BeanstalkdPriorityStamp(2));
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];

$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers'], 0, 2);

$serializer = $this->createMock(SerializerInterface::class);
$serializer->method('encode')->with($envelope)->willReturn($encoded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,41 @@ public function testSend()
$this->assertSame($id, (int) $returnedId);
}

public function testSendWithPriority()
{
$tube = 'xyz';

$body = 'foo';
$headers = ['test' => 'bar'];
$delay = 1000;
$priority = 2;
$expectedDelay = $delay / 1000;

$id = 110;

$client = $this->createMock(PheanstalkInterface::class);
$client->expects($this->once())->method('useTube')->with($tube)->willReturn($client);
$client->expects($this->once())->method('put')->with(
$this->callback(function (string $data) use ($body, $headers): bool {
$expectedMessage = json_encode([
'body' => $body,
'headers' => $headers,
]);

return $expectedMessage === $data;
}),
$priority,
$expectedDelay,
90
)->willReturn(new Job($id, 'foobar'));

$connection = new Connection(['tube_name' => $tube], $client);

$returnedId = $connection->send($body, $headers, $delay, $priority);

$this->assertSame($id, (int) $returnedId);
}

public function testSendWhenABeanstalkdExceptionOccurs()
{
$tube = 'xyz';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Bridge\Beanstalkd\Transport;

use Symfony\Component\Messenger\Stamp\StampInterface;

final readonly class BeanstalkdPriorityStamp implements StampInterface
{
public function __construct(
public int $priority,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe?

Suggested change
public int $priority,
public readonly int $priority,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fabpot No need, the class itself is readonly.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed :)

) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public function send(Envelope $envelope): Envelope
{
$encodedMessage = $this->serializer->encode($envelope);

/** @var DelayStamp|null $delayStamp */
$delayStamp = $envelope->last(DelayStamp::class);
$delayInMs = null !== $delayStamp ? $delayStamp->getDelay() : 0;

$this->connection->send($encodedMessage['body'], $encodedMessage['headers'] ?? [], $delayInMs);
$this->connection->send(
$encodedMessage['body'],
$encodedMessage['headers'] ?? [],
$envelope->last(DelayStamp::class)?->getDelay() ?? 0,
$envelope->last(BeanstalkdPriorityStamp::class)?->priority,
);

return $envelope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public function getTube(): string
}

/**
* @param int $delay The delay in milliseconds
* @param int $delay The delay in milliseconds
* @param ?int $priority The priority at which the message will be reserved
*
* @return string The inserted id
*/
public function send(string $body, array $headers, int $delay = 0): string
public function send(string $body, array $headers, int $delay = 0, ?int $priority = null): string
{
$message = json_encode([
'body' => $body,
Expand All @@ -123,7 +124,7 @@ public function send(string $body, array $headers, int $delay = 0): string
try {
$job = $this->client->useTube($this->tube)->put(
$message,
PheanstalkInterface::DEFAULT_PRIORITY,
$priority ?? PheanstalkInterface::DEFAULT_PRIORITY,
(int) ($delay / 1000),
$this->ttr
);
Expand Down
0