trovit / cron-manager-bundle
A simple cron manager that allow to schedule commands without relying on system operators
Installs: 455
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 5
Forks: 6
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.3.9
- doctrine/doctrine-bundle: ~1.3
- doctrine/orm: ~2.5
- mtdowling/cron-expression: ^1.0
- symfony/console: ~2.3|~3.0
- symfony/form: ~2.3|~3.0
- symfony/framework-bundle: ~2.3|~3.0
- symfony/process: ~2.3|~3.0
- symfony/twig-bundle: ~2.3|~3.0
- symfony/yaml: ~2.3|~3.0
Requires (Dev)
- phpunit/phpunit: 4.7.*
- symfony/phpunit-bridge: ~2.7|~3.0
This package is not auto-updated.
Last update: 2022-04-02 08:52:43 UTC
README
Manage cron tasks commands without relying on system operators
Installation
Step 1: Require bundle using composer
$ composer require trovit/cron-manager-bundle "~0.1"
Step 2: Enable the bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Trovit\CronManagerBundle\TrovitCronManagerBundle(), // ... ); }
Step 3: Update your database schema
$ php bin/console doctrine:schema:update --force
Usage
Create a new cron task
<?php //... $this->get('trovit.cron_manager.create_cron_task')->create( $name = 'CronTask Example', $description = 'Example cron task', $command = 'cache:clear', $interval = 'PT1H' // DateInterval format: http://php.net/manual/en/dateinterval.construct.php ); //...
Run
There are two ways to execute the cron manager: via a cronjob or via system daemon. Important: the cron must be executed every minute to work properly.
Via Cronjob:
Add this line into your crontab file:
*/1 * * * * php /path/to/your/project/app/console cron:execute --no-debug >/dev/null 2>&1
Via Daemon:
You could use Upstart. If you do so, create a file named cron-manager.conf and place it in /etc/init.d:
description "Cron tasks manager runner" author "Trovit" # Tell Upstart to respawn our command on a crash # stop restarting if it crashes more then 5 times within 10 seconds respawn respawn limit 5 10 # Tell Upstart to start us after MySQL started and shut us down on system shutdown start on started mysql stop on runlevel S # Run as the www-data user and group setuid www-data setgid www-data exec php /path/to/your/project/app/console cron:execute --daemon --no-debug >/dev/null 2>&1
To start the daemon, execute start cron-manager
.
Modify an existing cron task
<?php //... $cronTask = $this->getDoctrine()->getRepository('TrovitCronManagerBundle:TblCronTask')->find($id); $this->get('trovit.cron_manager.update_cron_task')->update( $cronTask, $name = 'CronTask Example updated', $description = 'Example cron task updated', $command = 'cache:clear --no-warmup', $interval = 'PT30M' // DateInterval format: http://php.net/manual/en/dateinterval.construct.php ); $this->get('trovit.cron_manager.update_cron_task')->deactivate($cronTask); $this->get('trovit.cron_manager.update_cron_task')->activate($cronTask); //...
Delete a cron task
<?php //... $cronTask = $this->getDoctrine()->getRepository('TrovitCronManagerBundle:TblCronTask')->find($id); $this->get('trovit.cron_manager.delete_cron_task')->delete($cronTask); // It can also be deleted by id // $this->get('trovit.cron_manager.delete_cron_task')->deleteById($id); //...
List cron tasks
<?php //... $allCronTasks = $this->get('trovit.cron_manager.read_cron_task')->getAllCronTasks(); $activeCronTasks = $this->get('trovit.cron_manager.read_cron_task')->getActiveCronTasks(); //...