8000 [Messenger] Add support for the DelayStamp in InMemoryTransport by fabpot · Pull Request #49725 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add support for the DelayStamp in InMemoryTransport #49725

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
Mar 19, 2023
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
Split Diff View
Split
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
Expand Down Expand Up @@ -84,6 +85,15 @@ public function testQueue()
$this->assertSame([], $this->transport->get());
}

public function testQueueWithDelay()
{
$envelope1 = new Envelope(new \stdClass());
$envelope1 = $this->transport->send($envelope1);
$envelope2 = (new Envelope(new \stdClass()))->with(new DelayStamp(10_000));
$envelope2 = $this->transport->send($envelope2);
$this->assertSame([$envelope1], $this->transport->get());
}

public function testQueueWithSerialization()
{
$envelope = new Envelope(new \stdClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Component\Messenger\Transport\InMemory;

use Psr\Clock\ClockInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
Expand Down Expand Up @@ -46,16 +48,25 @@ class InMemoryTransport implements TransportInterface, ResetInterface
private array $queue = [];

private int $nextId = 1;
private ?SerializerInterface $serializer;
private array $availableAt = [];

public function __construct(SerializerInterface $serializer = null)
{
$this->serializer = $serializer;
public function __construct(
private ?SerializerInterface $serializer = null,
private ?ClockInterface $clock = null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this as require dev dep? Or is it already available?

) {
}

public function get(): iterable
{
return array_values($this->decode($this->queue));
$envelopes = [];
$now = $this->clock?->now() ?? new \DateTimeImmutable();
foreach ($this->decode($this->queue) as $id => $envelope) {
if (!isset($this->availableAt[$id]) || $now > $this->availableAt[$id]) {
$envelopes[] = $envelope;
}
}

return $envelopes;
}

public function ack(Envelope $envelope): void
Expand All @@ -66,7 +77,7 @@ public function ack(Envelope $envelope): void
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}

unset($this->queue[$transportMessageIdStamp->getId()]);
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
}

public function reject(Envelope $envelope): void
Expand All @@ -77,7 +88,7 @@ public function reject(Envelope $envelope): void
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}

unset($this->queue[$transportMessageIdStamp->getId()]);
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
}

public function send(Envelope $envelope): Envelope
Expand All @@ -88,6 +99,12 @@ public function send(Envelope $envelope): Envelope
$this->sent[] = $encodedEnvelope;
$this->queue[$id] = $encodedEnvelope;

/** @var DelayStamp|null $delayStamp */
if ($delayStamp = $envelope->last(DelayStamp::class)) {
$now = $this->clock?->now() ?? new \DateTimeImmutable();
$this->availableAt[$id] = $now->modify(sprintf('+%d seconds', $delayStamp->getDelay() / 1000));
}

return $envelope;
}

Expand Down
0