aurimasniekis / scheduler-bundle
A simple Symfony Job Scheduling Bundle
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.4||^8.0
- dragonmantank/cron-expression: ^2.3
- psr/log: ^1.1
- symfony/console: ~4.0||~5.0
- symfony/framework-bundle: ~4.0||~5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-12-22 17:02:30 UTC
README
A simple scheduler bundle for Symfony which provides a way to execute code at specific cron expressions without modifying cron tab every time.
Install
Via Composer
$ composer require aurimasniekis/scheduler-bundle
Add line to bundle.php
:
<?php return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], // ... AurimasNiekis\SchedulerBundle\AurimasNiekisSchedulerBundle::class => ['all' => true], ];
Add the scheduler to cron tab to run every minute:
* * * * * /path/to/symfony/install/bin/console scheduler:run 1>> /dev/null 2>&1
Usage
Scheduler Bundle uses Symfony Container autoconfigure
feature which automatically registers all services
which implement ScheduledJobInterface
or NamedScheduledJobInterface
interface into Scheduler.
To create a scheduled job you have two options either simple Scheduled Job or Named Scheduled Job. First one uses class name as job name, second provides method to define a job name.
<?php use AurimasNiekis\SchedulerBundle\ScheduledJobInterface; class NamelessJob implements ScheduledJobInterface { public function __invoke(): void { // Execute some logic } public function getSchedulerExpresion(): string { return '*/5 * * * *'; // Run every 5 minutes } }
<?php use AurimasNiekis\SchedulerBundle\NamedScheduledJobInterface; class NamedJob implements NamedScheduledJobInterface { public function __invoke(): void { // Execute some logic } public function getName(): string { return 'named_job'; } public function getSchedulerExpresion(): string { return '*/5 * * * *'; // Run every 5 minutes } }
Console Commands
scheduler:list
List all registered scheduled jobs, and their next scheduled run times
$ bin/console scheduler:list +------------------------------+-------------+---------- Scheduled Jobs -------------------------------------------------------+ | Name | Expression | Next Scheduled Run Times | +------------------------------+-------------+---------------------------------------------------------------------------------+ | named_job | */5 * * * * | 2020-03-22T04:55:00+00:00, 2020-03-22T05:00:00+00:00, 2020-03-22T05:05:00+00:00 | | App\ScheduledJob\NamelessJob | */5 * * * * | 2020-03-22T04:55:00+00:00, 2020-03-22T05:00:00+00:00, 2020-03-22T05:05:00+00:00 | +------------------------------+-------------+---------------------------------------------------------------------------------+
scheduler:execute
Executes a scheduled job manually
$ bin/console scheduler:execute named_job
Executing Scheduled Job: "named_job"
scheduler:run
Command to run all scheduled jobs at this moment. (Used for cron job definition)
* * * * * /path/to/symfony/install/bin/console scheduler:run 1>> /dev/null 2>&1
Testing
Run test cases
$ composer test
Run test cases with coverage (HTML format)
$ composer test-coverage
Run PHP style checker
$ composer cs-check
Run PHP style fixer
$ composer cs-fix
Contributing
Please see CONTRIBUTING and CONDUCT for details.
License
Please see License File for more information.