fgits/cron-bundle

This bundle provides scheduled execution of Symfony commands

Installs: 2 461

Dependents: 0

Suggesters: 0

Security: 0

Type:symfony-bundle

5.1 2024-11-05 21:16 UTC

This package is not auto-updated.

Last update: 2024-12-17 21:50:31 UTC


README

This bundle provides a simple interface for registering repeated scheduled tasks within your application, including support for installs where the host does not allow for command-line access (TODO).

This bundle is tested only against Symfony 6.3. It will likely work with Symfony 6.4

Installation

Installing this bundle can be done through these simple steps:

  1. Add the bundle to your project as a composer dependency:

    compoer require fgits/cron-bundle
    
  2. Add the bundle to your application kernel:

    // config/bundles.php
    return [
     // ...
     Fgits\Bundle\CronBundle\FgitsCronBundle::class => ['all' => true],
     // ...
    ];
    
  3. Update your DB schema

    bin/console doctrine:schema:update
    
  4. Start using the bundle:

    bin/console cron:scan
    bin/console cron:run
    

Running your cron jobs automatically

This bundle is designed around the idea that your tasks will be run with a minimum interval - the tasks will be run no more frequently than you schedule them, but they can only run when you trigger then (by running app/console cron:run, or the forthcoming web endpoint, for use with webcron services).

To facilitate this, you can create a cron job on your system like this:

*/5 * * * * /path/to/symfony/install/app/console cron:run

This will schedule your tasks to run at most every 5 minutes - for instance, tasks which are scheduled to run every 3 minutes will only run every 5 minutes.

Creating your own tasks

Creating your own tasks with CronBundle couldn't be easier - all you have to do is create a normal Symfony Command and tag it with the CronJob attribute, as demonstrated below:

#CronJob("PT1H")
class DemoCommand extends Command
{
    public function configure()
    {
		// Must have a name configured
		// ...
    }
    
    public function execute(InputInterface $input, OutputInterface $output): int
    {
		// Your code here
    }
}

The interval spec ("PT1H" in the above example) is documented on the DateInterval documentation page, and can be modified whenever you choose. For your CronJob to be scanned and included in future runs, you must first run app/console cron:scan - it will be scheduled to run the next time you run app/console cron:run