Skip to content

Commit

Permalink
bug #49817 [Scheduler] Improve triggers performance when possible (fa…
Browse files Browse the repository at this point in the history
…bpot)

This PR was merged into the 6.3 branch.

Discussion
----------

[Scheduler] Improve triggers performance when possible

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes-ish
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Using `\DatePeriod` for the default trigger is great as it takes care of all date/time idiosyncrasies.
But for high frequencies, that does not work well and performance becomes an issue. So, this PR solves this issue by always using the fast algorithm when the frequency is expressed in seconds (int) or an ISO period (like `PT2S`) or when created from a string that uses a "simple" expression (like `2 seconds`).

/cc `@upyx`

Commits
-------

ebcced6 [Scheduler] Improve triggers performance when possible
  • Loading branch information
nicolas-grekas committed May 22, 2023
2 parents 3f9fe23 + ebcced6 commit 9b0d811
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 359 deletions.
20 changes: 12 additions & 8 deletions src/Symfony/Component/Scheduler/RecurringMessage.php
Expand Up @@ -13,8 +13,8 @@

use Symfony\Component\Scheduler\Exception\InvalidArgumentException;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
use Symfony\Component\Scheduler\Trigger\DateIntervalTrigger;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\PeriodicalTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

/**
Expand All @@ -31,17 +31,21 @@ private function __construct(
}

/**
* Uses a relative date format to define the frequency.
* Sets the trigger frequency.
*
* Supported frequency formats:
*
* * An integer to define the frequency as a number of seconds;
* * An ISO 8601 duration format;
* * A relative date format as supported by \DateInterval;
* * A \DateInterval instance.
*
* @see https://en.wikipedia.org/wiki/ISO_8601#Durations
* @see https://php.net/datetime.formats.relative
*/
public static function every(string $frequency, object $message, string|\DateTimeImmutable $from = new \DateTimeImmutable(), string|\DateTimeImmutable $until = new \DateTimeImmutable('3000-01-01')): self
public static function every(string|int|\DateInterval $frequency, object $message, string|\DateTimeImmutable $from = new \DateTimeImmutable(), string|\DateTimeImmutable $until = new \DateTimeImmutable('3000-01-01')): self
{
if (false === $interval = \DateInterval::createFromDateString($frequency)) {
throw new InvalidArgumentException(sprintf('Frequency "%s" cannot be parsed.', $frequency));
}

return new self(new DateIntervalTrigger($interval, $from, $until), $message);
return new self(new PeriodicalTrigger($frequency, $from, $until), $message);
}

public static function cron(string $expression, object $message): self
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9b0d811

Please sign in to comment.