setono / message-scheduler-bundle
Schedule Symfony Messenger messages in the future
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^7.4
- doctrine/doctrine-bundle: ^2.1
- doctrine/orm: ^2.7
- doctrine/persistence: ^1.0 || ^2.0
- psr/log: ^1.1
- ramsey/uuid: ^3.0 || ^4.0
- symfony/config: ^4.4 || ^5.0
- symfony/console: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/framework-bundle: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
- symfony/messenger: ^4.4 || ^5.0
- symfony/workflow: ^4.4 || ^5.0
- thecodingmachine/safe: ^1.1
- webmozart/assert: ^1.0
Requires (Dev)
- nyholm/symfony-bundle-test: ^1.6
- phpunit/phpunit: ^8.5
- roave/security-advisories: dev-master
- setono/code-quality-pack: ^1.1
- vimeo/psalm: ^3.12
This package is auto-updated.
Last update: 2024-12-15 22:29:29 UTC
README
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 🎉
Installation
Step 1: Download
$ composer require setono/message-scheduler-bundle
Step 2: Enable the bundle
If you use Symfony Flex it will be enabled automatically.
Else you need to add it to the config/bundles.php
:
<?php // config/bundles.php return [ // ... Setono\MessageSchedulerBundle\SetonoMessageSchedulerBundle::class => ['all' => true], // ... ];
Step 3: Update your database schema
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
Step 4: Setup a cron job for dispatching messages
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
Step 5: Configure Symfony Messenger
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
Usage
TODO