8000 feature #38484 [Messenger] Add DelayStamp::delayFor() and DelayStamp:… · symfony/symfony@0000dfe · GitHub
[go: up one dir, main page]

Skip to content

Commit 0000dfe

Browse files
committed
feature #38484 [Messenger] Add DelayStamp::delayFor() and DelayStamp::delayUntil() (Nyholm)
This PR was squashed before being merged into the 5.x branch. Discussion ---------- [Messenger] Add DelayStamp::delayFor() and DelayStamp::delayUntil() | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #38141 | License | MIT | Doc PR | This PR will revert the work by @Toflar in #38141. He added some helper methods and I want to add a generic factory method that adds a DateInterval. This PR will also close #38480 and fix #38459. It is also related to a comment in #36512 (comment) that was liked by a few people. Maybe adding both `DelayStamp::delayFor(\DateInterval)` and `DelayStamp::delayUntil(\DateTimeInterface)` is overkill. But this covers all the bases and I hope that it will be the last change to this small class. Commits ------- c5de1eb [Messenger] Add DelayStamp::delayFor() and DelayStamp::delayUntil()
2 parents 0137609 + c5de1eb commit 0000dfe

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* The `RedeliveryStamp` will no longer be populated with error data. This information is now stored in the `ErrorDetailsStamp` instead.
88
* Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set.
9-
* Added factory methods to `DelayStamp`.
9+
* Added factory methods `DelayStamp::delayFor(\DateInterval)` and `DelayStamp::delayUntil(\DateTimeInterface)`.
1010
* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call
1111
* Added `WorkerMessageRetriedEvent`
1212
* Added `WorkerMessageReceivedEvent::setEnvelope()` and made event mutable

src/Symfony/Component/Messenger/Stamp/DelayStamp.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ public function getDelay(): int
3131
return $this->delay;
3232
}
3333

34-
public static function delayForSeconds(int $seconds): self
34+
public static function delayFor(\DateInterval $interval): self
3535
{
36-
return new self($seconds * 1000);
37-
}
36+
$now = new \DateTimeImmutable();
37+
$end = $now->add($interval);
3838

39-
public static function delayForMinutes(int $minutes): self
40-
{
41-
return self::delayForSeconds($minutes * 60);
39+
return new self(($end->getTimestamp() - $now->getTimestamp()) * 1000);
4240
}
4341

44-
public static function delayForHours(int $hours): self
42+
public static function delayUntil(\DateTimeInterface $dateTime): self
4543
{
46-
return self::delayForMinutes($hours * 60);
44+
return new self(($dateTime->getTimestamp() - time()) * 1000);
4745
}
4846
}

src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@
1919
*/
2020
class DelayStampTest extends TestCase
2121
{
22-
public function testSeconds()
22+
public function testDelayFor()
2323
{
24-
$stamp = DelayStamp::delayForSeconds(30);
24+
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 seconds'));
2525
$this->assertSame(30000, $stamp->getDelay());
26-
}
27-
28-
public function testMinutes()
29-
{
30-
$stamp = DelayStamp::delayForMinutes(30);
26+
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 minutes'));
3127
$this->assertSame(1800000, $stamp->getDelay());
28+
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 hours'));
29+
$this->assertSame(108000000, $stamp->getDelay());
30+
31+
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('-5 seconds'));
32+
$this->assertSame(-5000, $stamp->getDelay());
3233
}
3334

34-
public function testHours()
35+
public function testDelayUntil()
3536
{
36-
$stamp = DelayStamp::delayForHours(30);
37-
$this->assertSame(108000000, $stamp->getDelay());
37+
$stamp = DelayStamp::delayUntil(new \DateTimeImmutable('+30 seconds'));
38+
$this->assertSame(30000, $stamp->getDelay());
39+
40+
$stamp = DelayStamp::delayUntil(new \DateTimeImmutable('-5 seconds'));
41+
$this->assertSame(-5000, $stamp->getDelay());
3842
}
3943
}

0 commit comments

Comments
 (0)
0