A simple scheduler bundle for Symfony which provides a way to execute code at specific cron expressions without modifying cron tab every time.
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
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
}
}
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 |
+------------------------------+-------------+---------------------------------------------------------------------------------+
Executes a scheduled job manually
$ bin/console scheduler:execute named_job
Executing Scheduled Job: "named_job"
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
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
Please see CONTRIBUTING and CONDUCT for details.
Please see License File for more information.