Schedule Symfony Messenger messages in the future
This package is auto-updated.
Last update: 2021-04-21 08:49:30 UTC
Schedule Symfony Messenger messages in the future.
The need for this bundle came on a project where we needed to check the expiry of an event on a per minute basis using a cron job. So an event could end in 10 days at 10:05:00, but the cron job wouldn't know this. Instead, the cron job runs every minute to check it. Checking it was a very memory intensive task, so this wasn't feasible in the long run.
Therefore, instead of checking every minute, we schedule a command to be run in the future on the time that we know the event ends.
Problem solved 🎉
$ composer require setono/message-scheduler-bundle
If you use Symfony Flex it will be enabled automatically.
Else you need to add it to the
<?php // config/bundles.php return [ // ... Setono\MessageSchedulerBundle\SetonoMessageSchedulerBundle::class => ['all' => true], // ... ];
This bundle introduces a new entity,
ScheduledMessage, therefore you need to add a migration:
$ php bin/console doctrine:migrations:diff $ php bin/console doctrine:migrations:migrate
This bundle introduces a command,
setono:message-scheduler:dispatch, that you should run rather often. How often
depends on the needs of your application. Every minute is recommended. Here's a crontab snippet for you:
* * * * * php /absolute/path/to/bin/console setono:message-scheduler:dispatch
Because this bundle dispatches your messages using a command itself, it is rather important to send that command asynchronously. This will make sure the cron job you set up in the previous step will run fast.
framework: messenger: routing: # Route all command messages to the async transport # This presumes that you have already set up an 'async' transport 'Setono\MessageSchedulerBundle\Message\Command\CommandInterface': async