Scheduling

Scheduling PHP

You have the Ecotone async worker running already and you want a once-a-minute job (clean expired carts, refresh stale projections, poll a third-party API) without registering yet another supervisor or wiring up Symfony's Scheduler / Laravel's task scheduler. The #[Scheduled] attribute runs that job in the same worker process — same retries, same error channel, same metrics as the rest of your async handlers.

Scheduled Method

class NotificationService
{
    #[Scheduled(endpointId: "notificationSender")]
    #[Poller(fixedRateInMilliseconds: 1000)]
    public function sendNotifications(): void
    {
        echo "Sending notifications...\n";
    }
}

endpointId - it's name which identifies process to run poller - Configuration how to execute this method read more in next section. Above configuration tells Ecotone to execute this method every second.

console ecotone:list
+--------------------+
| Endpoint Names     |
+--------------------+
| notificationSender |
+--------------------+

After setting up Scheduled endpoint we can run the endpoint:

Scheduled Handler

You can run Scheduled for given Handler. Right now method return Message which is send to given routing.

requestChannelName - The channel name to which Message should be send.

When the Message will arrive on the Command Handler it will be automatically converted to ExchangeCommand. If you want to understand how the conversion works, you may read about it in Conversion section.

Expression Language

We can also set up cron and fixed rate using expression language. This gives us ability to set it up differently based on the environment we are currently in.

Materials

Demo implementation

You may find demo implementation here.

Last updated

Was this helpful?