2017 © Pedro Peláez
 

symfony-bundle cron-bundle

Simple Symfony Cron implementation

image

rikudou/cron-bundle

Simple Symfony Cron implementation

  • Thursday, July 26, 2018
  • by RikudouSage
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

How to install

Via composer: composer require rikudou/cron-bundle., (*1)

The bundle should be enabled automatically if you use the Symfony Flex., (*2)

How to use

Every cron job must implement the Rikudou\CronBundle\Cron\CronJobInterface (every class that implements the interface is automatically registered as cron job), which defines two methods:, (*3)

  • string getCronExpression() - the cron expression
  • execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) - the method that will be executed, you can write to console output, use console input and use logger

Example:, (*4)

<?php

namespace App\CronJobs;

use Psr\Log\LoggerInterface;
use Rikudou\CronBundle\Cron\CronJobInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyTestCronJob implements CronJobInterface
{

    /**
     * The cron expression for triggering this cron job
     * @return string
     */
    public function getCronExpression(): string
    {
        return "*/5 * * * *";
    }

    /**
     * This method will be executed when cron job runs
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @param LoggerInterface $logger
     * @return mixed
     */
    public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger)
    {
        $logger->debug("The cron job was executed!");
    }
}

The cron jobs are executed using the console command cron:run, this command should be executed every minute., (*5)

Example for crontab:, (*6)

* * * * * php /path/to/project/bin/console cron:run, (*7)

Listing cron jobs

If you want to see list of all jobs, run cron:list command from Symfony console., (*8)

Example: php bin/console cron:list, (*9)

Example output:, (*10)

+----------------------------+---------+-----------------+--------+---------------------+
| Class                      | Enabled | Cron expression | Is due | Next run            |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | yes     | */5 * * * *     | no     | 2018-08-07 18:30:00 |
+----------------------------+---------+-----------------+--------+---------------------+

The listing will also let you know if the cron expression is invalid:, (*11)

+----------------------------+---------+-----------------+----------------------------+----------------------------+
| Class                      | Enabled | Cron expression | Is due                     | Next run                   |
+----------------------------+---------+-----------------+----------------------------+----------------------------+
| App\CronJobs\MyTestCronJob | yes     | */5 * * * * *   | Cron expression is invalid | Cron expression is invalid |
+----------------------------+---------+-----------------+----------------------------+----------------------------+

Disabling a cron job

If you have a cron job that you don't want to delete but at the same time you don't want to trigger it, you can define method isEnabled() and let it return false., (*12)

You can also use the packaged helper trait Rikudou\CronBundle\Helper\DisabledCronJob., (*13)

Example 1 (wihout trait):, (*14)

<?php

namespace App\CronJobs;

use Psr\Log\LoggerInterface;
use Rikudou\CronBundle\Cron\CronJobInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyTestCronJob implements CronJobInterface
{

    /**
     * The cron expression for triggering this cron job
     * @return string
     */
    public function getCronExpression(): string
    {
        return "*/5 * * * *";
    }

    /**
     * This method will be executed when cron job runs
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @param LoggerInterface $logger
     * @return mixed
     */
    public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger)
    {
        $logger->debug("The cron job was executed!");
    }

    public function isEnabled() {
        return false;
    }
}

Example 2 (using trait):, (*15)

<?php

namespace App\CronJobs;

use Psr\Log\LoggerInterface;
use Rikudou\CronBundle\Cron\CronJobInterface;
use Rikudou\CronBundle\Helper\DisabledCronJob;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyTestCronJob implements CronJobInterface
{

    use DisabledCronJob;

    /**
     * The cron expression for triggering this cron job
     * @return string
     */
    public function getCronExpression(): string
    {
        return "*/5 * * * *";
    }

    /**
     * This method will be executed when cron job runs
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @param LoggerInterface $logger
     * @return mixed
     */
    public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger)
    {
        $logger->debug("The cron job was executed!");
    }

}

In both cases the output will be similar to:, (*16)

+----------------------------+---------+-----------------+--------+---------------------+
| Class                      | Enabled | Cron expression | Is due | Next run            |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | no      | */5 * * * *     | no     | 2018-08-07 18:40:00 |
+----------------------------+---------+-----------------+--------+---------------------+

Running commands as cron jobs

If you want to run commands as cron jobs, you must configure them in yaml file., (*17)

Example:, (*18)

rikudou_cron:
    commands:
      clearCache:
        command: cache:clear
        cron_expression: "*/2 * * * *"

This setting will run cache:clear every two minutes., (*19)

The config file will be created (if it doesn't already exist) when you first run any cron command (cron:list, cron:run) at %projectDir%/config/packages/rikudou_cron.yaml., (*20)

IF you need to pass arguments or options to the command, you can do so:, (*21)

rikudou_cron:
    commands:
      someCommand:
        command: app:my-command
        cron_expression: "*/2 * * * *"
        options:
          some-option: some-value
        arguments:
          some-argument: some-value

Note that the option and argument names need to match their names as defined in the target command., (*22)

Example output from cron:list:, (*23)

Classes
+----------------------------+---------+-----------------+--------+---------------------+
| Class                      | Enabled | Cron expression | Is due | Next run            |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | no      | */5 * * * *     | no     | 2018-09-06 15:25:00 |
+----------------------------+---------+-----------------+--------+---------------------+
Commands
+-------------+-----------------+--------+---------------------+
| Command     | Cron expression | Is due | Next run            |
+-------------+-----------------+--------+---------------------+
| cache:clear | */2 * * * *     | yes    | 2018-09-06 15:24:00 |
+-------------+-----------------+--------+---------------------+

The Versions

26/07 2018

dev-master

9999999-dev

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký

26/07 2018

v0.0.5

0.0.5.0

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký

25/07 2018

v0.0.4

0.0.4.0

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký

25/07 2018

v0.0.3

0.0.3.0

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký

25/07 2018

v0.0.2

0.0.2.0

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký

19/07 2018

v0.0.1

0.0.1.0

Simple Symfony Cron implementation

  Sources   Download

WTFPL

The Requires

 

by Dominik Chrástecký