Skip to content

Commit f6be791

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: [Scheduler] Document events
2 parents b3618fd + 8bdc50e commit f6be791

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

scheduler.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,103 @@ before being further redispatched to its corresponding handler::
478478
}
479479
}
480480

481+
Scheduler Events
482+
----------------
483+
484+
PreRunEvent
485+
~~~~~~~~~~~
486+
487+
**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\PreRunEvent`
488+
489+
``PreRunEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
490+
or cancel a message before it's consumed::
491+
492+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
493+
use Symfony\Component\Scheduler\Event\PreRunEvent;
494+
495+
public function onMessage(PreRunEvent $event): void
496+
{
497+
$schedule = $event->getSchedule();
498+
$context = $event->getMessageContext();
499+
$message = $event->getMessage();
500+
501+
// do something with the schedule, context or message
502+
503+
// and/or cancel message
504+
$event->shouldCancel(true);
505+
}
506+
507+
Execute this command to find out which listeners are registered for this event
508+
and their priorities:
509+
510+
.. code-block:: terminal
511+
512+
$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\PreRunEvent"
513+
514+
PostRunEvent
515+
~~~~~~~~~~~~
516+
517+
**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\PostRunEvent`
518+
519+
``PostRunEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
520+
after a message is consumed::
521+
522+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
523+
use Symfony\Component\Scheduler\Event\PostRunEvent;
524+
525+
public function onMessage(PostRunEvent $event): void
526+
{
527+
$schedule = $event->getSchedule();
528+
$context = $event->getMessageContext();
529+
$message = $event->getMessage();
530+
531+
// do something with the schedule, context or message
532+
}
533+
534+
Execute this command to find out which listeners are registered for this event
535+
and their priorities:
536+
537+
.. code-block:: terminal
538+
539+
$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\PostRunEvent"
540+
541+
FailureEvent
542+
~~~~~~~~~~~~
543+
544+
**Event Class**: :class:`Symfony\\Component\\Scheduler\\Event\\FailureEvent`
545+
546+
``FailureEvent`` allows to modify the :class:`Symfony\\Component\\Scheduler\\Schedule`
547+
when a message consumption throws an exception::
548+
549+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
550+
use Symfony\Component\Scheduler\Event\FailureEvent;
551+
552+
public function onMessage(FailureEvent $event): void
553+
{
554+
$schedule = $event->getSchedule();
555+
$context = $event->getMessageContext();
556+
$message = $event->getMessage();
557+
558+
$error = $event->getError();
559+
560+
// do something with the schedule, context, message or error (logging, ...)
561+
562+
// and/or ignore failure event
563+
$event->shouldIgnore(true);
564+
}
565+
566+
Execute this command to find out which listeners are registered for this event
567+
and their priorities:
568+
569+
.. code-block:: terminal
570+
571+
$ php bin/console debug:event-dispatcher "Symfony\Component\Scheduler\Event\FailureEvent"
572+
573+
.. versionadded:: 6.4
574+
575+
The ``PreRunEvent``, ``PostRunEvent`` and ``FailureEvent`` events were
576+
introduced in Symfony 6.4.
577+
481578
.. _`Memoizing`: https://en.wikipedia.org/wiki/Memoization
482579
.. _`cron command-line utility`: https://en.wikipedia.org/wiki/Cron
483580
.. _`crontab.guru website`: https://crontab.guru/

0 commit comments

Comments
 (0)