Skip to content

[Scheduler] Add new component documentation #18136

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Topics
profiler
rate_limiter
routing
scheduler
security
session
setup
Expand Down
5 changes: 5 additions & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ Routing

* :doc:`Route </routing>`

Scheduler
~~~~~~~~~

* :ref:`AsSchedule <register-schedule-provider>`

Security
~~~~~~~~

Expand Down
129 changes: 129 additions & 0 deletions scheduler.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Scheduler
=========

.. versionadded:: 6.3

The Scheduler component was introduced in Symfony 6.3 and is marked
as experimental.

The Scheduler component provides a way to schedule periodical tasks or handling
recurring messages without using an external tool.

The component shares somes keys concepts with :ref:`Messenger <messenger>`.

Installation
------------

You can install the Scheduler component with:

.. code-block:: terminal

$ composer require symfony/scheduler

.. include:: /components/require_autoload.rst.inc

.. _register-schedule-provider:

Register a Schedule provider
----------------------------

A Schedule provider is a class defining your schedule : which messages should
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A Schedule provider is a class defining your schedule : which messages should
A Schedule provider is a class defining your schedule. Which messages should

be processed depending a trigger you choose::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
be processed depending a trigger you choose::
be processed depends on the trigger you choose::

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
be processed depending a trigger you choose::
be processed depending on the trigger you choose:


namespace App\Schedule;

use App\Message\EndofTrialMessage;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;

#[AsSchedule('trial')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[AsSchedule('trial')]
#[AsSchedule('default')]

I'm thinking we should just use default here. Multiple schedules is an advanced use-case imo.

final class EndOfTrialScheduleProvider implements ScheduleProviderInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class EndOfTrialScheduleProvider implements ScheduleProviderInterface
final class AppSchedule implements ScheduleProviderInterface

{
public function getSchedule(string $id): Schedule
{
return (new Schedule())
->add(new RecurringMessage::every('1 week', new EndofTrialMessage()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new keyword should not be there 😉

Suggested change
->add(new RecurringMessage::every('1 week', new EndofTrialMessage()))
->add(RecurringMessage::every('1 week', new EndofTrialMessage()))

;
}
}

Run the Messenger consumer
--------------------------

Scheduler uses the same :ref:`Messenger worker <messenger-worker>` to consume
messages dispatched by your schedule class.

You can do this with the ``messenger:consume`` command:

.. code-block:: terminal

$ php bin/console messenger:consume scheduler_trial
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$ php bin/console messenger:consume scheduler_trial
$ php bin/console messenger:consume scheduler_default


Trigger your schedule
---------------------

Symfony provides by default two triggers for your schedules : `every` and
`cron`. By extending the provided TriggerInterface you can also create
your own trigger for your app.
Comment on lines +67 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Symfony provides by default two triggers for your schedules : `every` and
`cron`. By extending the provided TriggerInterface you can also create
your own trigger for your app.
Symfony provides several triggers, and trigger wrappers out of the box. You can also define your own triggers.


Every
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Every
PeriodicalTrigger

~~~~~

This trigger allows you to define you schedule with frequency as text, using
intervals or relative date format allowed by PHP::

RecurringMessage::every('10 seconds', $msg);
RecurringMessage::every('1 day', $msg);
RecurringMessage::every('2 weeks', $msg);

RecurringMessage::every('first day of month', $msg);
RecurringMessage::every('next tuesday', $msg);

It is possible to specify an exact hour to trigger the schedule with `from`
option::

RecurringMessage::every('first day of month', $msg, from: '13:47');

// using timezone
RecurringMessage::every('first day of month', $msg, from: '13:47+0400');

// using a DateTimeImmutable object
$from = new \DateTimeImmutable('13:47', new \DateTimeZone('Europe/Paris'));
RecurringMessage::every('first day of month', $msg, from: $from);

If your schedule do not need to run indefinitely, you can use `until` option
to let the Component know when the trigger should stop::

RecurringMessage::every('monday', $msg, until: '2023-06-12');

Cron
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Cron
CronExpressionTrigger

~~~~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a note that https://github.com/dragonmantank/cron-expression is required to use this trigger.

This trigger can be configured following same rules as the eponymous Unix
utility::

// trigger every hour
RecurringMessage::cron('0 * * * *', $msg);

// trigger every monday at 12:00
RecurringMessage::cron('0 12 * * 1', $msg);

.. note::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document symfony/symfony#49792


The minimal interval allowed with cron is 1 minute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO:

  • JitterTrigger
  • CallbackTrigger
  • ExcludeTimeTrigger

Define your own trigger
~~~~~~~~~~~~~~~~~~~~~~~

By implementing the TriggerInterface your can create your own trigger using
``getNextRunDate`` method::

final class CustomTrigger implements TriggerInterface
{
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
// your logic
}
}