phpsymfonysymfony-scheduler

Scheduler not sending any messages


symfony/scheduler is a new component since 6.3, it's not yet documented so I'm hoping someone knows how to get this component to work. All I've read is their blog post which can be found here.

Javier describes the component to be almost equal in it's implementation to the symfony/messenger component, and that the only difference is that a scheduled message needs to have a ScheduleProvider that defines the schedule, and it's own queue name.

I've added my schedule as such:

#[AsSchedule("default")]
class DefaultScheduleProvider implements ScheduleProviderInterface
{
    public function getSchedule(): Schedule
    {
        $schedule = new Schedule();

        $schedule->add(RecurringMessage::every("5 seconds", new TestScheduledMessage()));

        return $schedule;
    }
}

Then I defined my message queue in the messenger configuration:

framework:
    messenger:
        failure_transport: failed
        transports:
            scheduler_default: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            App\Scheduler\TestScheduledMessage: scheduler_default

Then I run symfony console debug:scheduler to verify that the schedule is detected:

default
-------
 ------------------------- ------------------------------------ --------------------------- 
  Message                   Trigger                              Next Run                   
 ------------------------- ------------------------------------ --------------------------- 
  TestScheduledMessage      PeriodicalTrigger: every 5 seconds   2023-08-10T15:28:30+00:00  
 ------------------------- ------------------------------------ ---------------------------

And the messenger recognizes my transport:

 ------------------- ------- 
  Transport           Count  
 ------------------- ------- 
  scheduler_default   0      
 ------------------- ------- 

I then setup my worker via symfony console messenger:consume -vv scheduler_default, but I never receive any messages. It just sits there forever and does nothing.

Is there something else I need to do to get things up and running?

Edit

Also, if it may be useful information, this is my handler:

#[AsMessageHandler]
final class TestMessageHandler
{
    public function __invoke(TestScheduledMessage $message)
    {
        // do something with your message
    }
}

Solution

  • So, it's not entirely like the messenger component, as the transport must not be defined in the messenger.yaml configuration. The SchedulerProvider set up the queue automagically, and if the queue already exists, it does nothing.

    Removing the scheduler_default transport in my code above, and then routing the messages to another transport, such as the default async will make it work.

    Example:

    framework:
        messenger:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
            failure_transport: failed
            routing:
                App\Scheduler\TestScheduledMessage: async
    

    As there was no documentation, and the blog post said it's configured just the same as the messenger component, I was falsely assuming you had to configure the transport too.

    The scheduler transport still needs it's specific worker though, just like any regular messenger transport.