Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c57c971

Browse files
committed
[Messenger] Add support for the DelayStamp in InMemoryTransport
1 parent 5fbf9be commit c57c971

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1617
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
1718
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
1819
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
@@ -84,6 +85,15 @@ public function testQueue()
8485
$this->assertSame([], $this->transport->get());
8586
}
8687

88+
public function testQueueWithDelay()
89+
{
90+
$envelope1 = new Envelope(new \stdClass());
91+
$envelope1 = $this->transport->send($envelope1);
92+
$envelope2 = (new Envelope(new \stdClass()))->with(new DelayStamp(10_000));
93+
$envelope2 = $this->transport->send($envelope2);
94+
$this->assertSame([$envelope1], $this->transport->get());
95+
}
96+
8797
public function testQueueWithSerialization()
8898
{
8999
$envelope = new Envelope(new \stdClass());

src/Symfony/Component/Messenger/Transport/InMemory/InMemoryTransport.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Component\Messenger\Transport\InMemory;
1313

14+
use Psr\Clock\ClockInterface;
1415
use Symfony\Component\Messenger\Envelope;
1516
use Symfony\Component\Messenger\Exception\LogicException;
17+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1618
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
1719
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
1820
use Symfony\Component\Messenger\Transport\TransportInterface;
@@ -46,16 +48,25 @@ class InMemoryTransport implements TransportInterface, ResetInterface
4648
private array $queue = [];
4749

4850
private int $nextId = 1;
49-
private ?SerializerInterface $serializer;
51+
private array $availableAt = [];
5052

51-
public function __construct(SerializerInterface $serializer = null)
52-
{
53-
$this->serializer = $serializer;
53+
public function __construct(
54+
private ?SerializerInterface $serializer = null,
55+
private ?ClockInterface $clock = null,
56+
) {
5457
}
5558

5659
public function get(): iterable
5760
{
58-
return array_values($this->decode($this->queue));
61+
$envelopes = [];
62+
$now = $this->clock?->now() ?: new \DateTimeImmutable();
63+
foreach ($this->decode($this->queue) as $id => $envelope) {
64+
if (!isset($this->availableAt[$id]) || $now > $this->availableAt[$id]) {
65+
$envelopes[] = $envelope;
66+
}
67+
}
68+
69+
return $envelopes;
5970
}
6071

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

69-
unset($this->queue[$transportMessageIdStamp->getId()]);
80+
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
7081
}
7182

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

80-
unset($this->queue[$transportMessageIdStamp->getId()]);
91+
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
8192
}
8293

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

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

0 commit comments

Comments
 (0)