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

Skip to content

[Messenger] Add DelayStamp::delayFor() and DelayStamp::delayUntil() #38484

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
Oct 8, 2020
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGELOG

* The `RedeliveryStamp` will no longer be populated with error data. This information is now stored in the `ErrorDetailsStamp` instead.
* 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.
* Added factory methods to `DelayStamp`.
* Added factory methods `DelayStamp::delayFor(\DateInterval)` and `DelayStamp::delayUntil(\DateTimeInterface)`.
* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call
* Added `WorkerMessageRetriedEvent`
* Added `WorkerMessageReceivedEvent::setEnvelope()` and made event mutable
Expand Down
14 changes: 6 additions & 8 deletions src/Symfony/Component/Messenger/Stamp/DelayStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ public function getDelay(): int
return $this->delay;
}

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

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

public static function delayForHours(int $hours): self
public static function delayUntil(\DateTimeInterface $dateTime): self
{
return self::delayForMinutes($hours * 60);
return new self(($dateTime->getTimestamp() - time()) * 1000);
}
}
24 changes: 14 additions & 10 deletions src/Symfony/Component/Messenger/Tests/Stamp/DelayStampTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
*/
class DelayStampTest extends TestCase
{
public function testSeconds()
public function testDelayFor()
{
$stamp = DelayStamp::delayForSeconds(30);
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 seconds'));
$this->assertSame(30000, $stamp->getDelay());
}

public function testMinutes()
{
$stamp = DelayStamp::delayForMinutes(30);
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 minutes'));
$this->assertSame(1800000, $stamp->getDelay());
$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('30 hours'));
$this->assertSame(108000000, $stamp->getDelay());

$stamp = DelayStamp::delayFor(\DateInterval::createFromDateString('-5 seconds'));
$this->assertSame(-5000, $stamp->getDelay());
}

public function testHours()
public function testDelayUntil()
{
$stamp = DelayStamp::delayForHours(30);
$this->assertSame(108000000, $stamp->getDelay());
$stamp = DelayStamp::delayUntil(new \DateTimeImmutable('+30 seconds'));
$this->assertSame(30000, $stamp->getDelay());

$stamp = DelayStamp::delayUntil(new \DateTimeImmutable('-5 seconds'));
$this->assertSame(-5000, $stamp->getDelay());
}
}