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

Skip to content

[Scheduler] Document events #19450

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
Jan 23, 2024
Merged
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
97 changes: 97 additions & 0 deletions scheduler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,103 @@ before being further redispatched to its corresponding handler::
}
}

Scheduler Events
----------------

PreRunEvent
~~~~~~~~~~~

**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\PreRunEvent`

``PreRunEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
or cancel message before it's consumed::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Scheduler\Event\PreRunEvent;

public function onMessage(PreRunEvent $event): void
{
$schedule = $event->getSchedule();
$context = $event->getMessageContext();
$message = $event->getMessage();
Comment on lines +520 to +522
Copy link
Contributor Author

@alamirault alamirault Jan 22, 2024

Choose a reason for hiding this comment

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

Right know, we don't explain in the scheduler documentation:

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we're missing things ... but let's do that in other PRs. The idea with the Scheduler docs is to move very fast (even if that breaks some things) to compensate a bit the big delay in merging these docs. Thanks.


// do something with the schedule, context or message

// and/or cancel message
$event->shouldCancel(true);
}

Execute this command to find out which listeners are registered for this event
and their priorities:

.. code-block:: terminal

$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\PreRunEvent"

PostRunEvent
~~~~~~~~~~~~

**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\PostRunEvent`

``PostRunEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
after message is consumed::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Scheduler\Event\PostRunEvent;

public function onMessage(PostRunEvent $event): void
{
$schedule = $event->getSchedule();
$context = $event->getMessageContext();
$message = $event->getMessage();

// do something with the schedule, context or message
}

Execute this command to find out which listeners are registered for this event
and their priorities:

.. code-block:: terminal

$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\PostRunEvent"

FailureEvent
~~~~~~~~~~~~

**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\FailureEvent`

``FailureEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
when message consumption throw an exception::

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Scheduler\Event\FailureEvent;

public function onMessage(FailureEvent $event): void
{
$schedule = $event->getSchedule();
$context = $event->getMessageContext();
$message = $event->getMessage();

$error = $event->getError();

// do something with the schedule, context, message or error (logging, ...)

// and/or ignore failure event
$event->shouldIgnore(true);
}

Execute this command to find out which listeners are registered for this event
and their priorities:

.. code-block:: terminal

$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\FailureEvent"

.. versionadded:: 6.4

Methods ``PreRunEvent``, ``PostRunEvent`` and ``FailureEvent`` were introduced
in Symfony 6.4.

.. _`Memoizing`: https://en.wikipedia.org/wiki/Memoization
.. _`cron command-line utility`: https://en.wikipedia.org/wiki/Cron
.. _`crontab.guru website`: https://crontab.guru/
Expand Down