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

Skip to content

Commit 29511ca

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

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
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: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Envelope;
1515
use Symfony\Component\Messenger\Exception\LogicException;
16+
use Symfony\Component\Messenger\Stamp\DelayStamp;
1617
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
1718
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
1819
use Symfony\Component\Messenger\Transport\TransportInterface;
@@ -47,6 +48,7 @@ class InMemoryTransport implements TransportInterface, ResetInterface
4748

4849
private int $nextId = 1;
4950
private ?SerializerInterface $serializer;
51+
private array $availableAt = [];
5052

5153
public function __construct(SerializerInterface $serializer = null)
5254
{
@@ -55,7 +57,15 @@ public function __construct(SerializerInterface $serializer = null)
5557

5658
public function get(): iterable
5759
{
58-
return array_values($this->decode($this->queue));
60+
$envelopes = [];
61+
$now = new \DateTimeImmutable();
62+
foreach ($this->decode($this->queue) as $id => $envelope) {
63+
if (!isset($this->availableAt[$id]) || $now > $this->availableAt[$id]) {
64+
$envelopes[] = $envelope;
65+
}
66+
}
67+
68+
return $envelopes;
5969
}
6070

6171
public function ack(Envelope $envelope): void
@@ -66,7 +76,7 @@ public function ack(Envelope $envelope): void
6676
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
6777
}
6878

69-
unset($this->queue[$transportMessageIdStamp->getId()]);
79+
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
7080
}
7181

7282
public function reject(Envelope $envelope): void
@@ -77,7 +87,7 @@ public function reject(Envelope $envelope): void
7787
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
7888
}
7989

80-
unset($this->queue[$transportMessageIdStamp->getId()]);
90+
unset($this->queue[$id = $transportMessageIdStamp->getId()], $this->availableAt[$id]);
8191
}
8292

8393
public function send(Envelope $envelope): Envelope
@@ -88,6 +98,11 @@ public function send(Envelope $envelope): Envelope
8898
$this->sent[] = $encodedEnvelope;
8999
$this->queue[$id] = $encodedEnvelope;
90100

101+
/** @var DelayStamp|null $delayStamp */
102+
if ($delayStamp = $envelope->last(DelayStamp::class)) {
103+
$this->availableAt[$id] = (new \DateTimeImmutable())->modify(sprintf('+%d seconds', $delayStamp->getDelay() / 1000));
104+
}
105+
91106
return $envelope;
92107
}
93108

0 commit comments

Comments
 (0)