diff --git a/blog/2026-02-23-task-manager-scheduler.mdx b/blog/2026-02-23-task-manager-scheduler.mdx new file mode 100644 index 0000000..cc4290d --- /dev/null +++ b/blog/2026-02-23-task-manager-scheduler.mdx @@ -0,0 +1,9 @@ +--- +title: "Task Manager: Scheduler" +authors: [jannik] +tags: [task-manager] +--- + +The task manager now has an [integrated scheduler][1], that you should use instead of Symfony's scheduler. + +[1]: /docs/php/symfony/task-manager/#schedules diff --git a/docs/php/symfony/task-manager/index.mdx b/docs/php/symfony/task-manager/index.mdx index 6c97158..1e10457 100644 --- a/docs/php/symfony/task-manager/index.mdx +++ b/docs/php/symfony/task-manager/index.mdx @@ -1,4 +1,5 @@ import {LinkList} from "../../../../src/components/Link/LinkList"; +import {AddedBadge} from "../../../../src/components/AddedBadge"; # Task Manager @@ -274,6 +275,51 @@ While not explicitly `->finish()`ed tasks will be handled properly, you should a ::: +## Schedules + + + +The task manager also supports scheduling tasks like the Symfony scheduler. + +```php +use Symfony\Component\Scheduler\Attribute\AsSchedule; +use Symfony\Component\Scheduler\Schedule; +use Symfony\Component\Scheduler\ScheduleProviderInterface; +use Torr\TaskManager\Schedule\TaskScheduler; + +#[AsSchedule] +class MySchedule implements ScheduleProviderInterface +{ + public function __construct( + private TaskScheduler $taskScheduler, + ) {} + + public function getSchedule() : Schedule + { + return $this->taskScheduler->createSchedule() + ->cron("* * * * *", new Task1()) + ->every("10 seconds", new Task2()) + ->getSchedule(); + } +} +``` + +This automatically registers these tasks for the next regular messenger work run, so they are not being worked on right away. + + +### `DispatchAfterRunTask` + +If you want to dispatch tasks after the current run, you can use the `DispatchAfterRunTask`. +This is especially useful for tasks that are scheduled via the scheduler, as the scheduler normally starts working on these tasks right away. +This way the priority system is circumvented. + +Normally, this is not desired, so you should use this helper task. + +:::info +The `TaskScheduler` automatically wraps all provided tasks in `DispatchAfterRunTask`. +::: + + ## Task Log The `TaskDirector` and `RunDirector` automatically log the output of your tasks, whether they succeeded and some metadata. This includes the task object itself, so it must be serializable.