aurimasniekis/scheduler-bundle

A simple Symfony Job Scheduling Bundle

1.0.0 2020-03-22 06:14 UTC

This package is auto-updated.

Last update: 2024-11-22 16:49:26 UTC


README

Latest Version on Packagist Software License Build Status Code Quality Code Coverage Mutation testing badge Total Downloads

Email

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.