elstc / cakephp-cron-jobs
A cron job runner for CakePHP
Installs: 5 452
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 0
Open Issues: 0
Type:cakephp-plugin
Requires
- php: >=8.1
- cakephp/cakephp: ^5.0
- crunzphp/crunz: ^3.6
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^10.5.5 || ^11.1.3 || ^12.0.9
README
This plugin is a simple wrapper for crunzphp/crunz.
Version Map
CakePHP Version | Plugin Version | Branch |
---|---|---|
5.x | 3.x | cake5 |
4.x | 2.x | cake4 |
3.x | 0.3.x | cake3 |
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require elstc/cakephp-cron-jobs
Load plugin
Load the plugin by adding the following statement in your project's src/Application.php
:
$this->addPlugin('Elastic/CronJobs');
Generate config file
Run bin/cake CronJobs publish:config
command.
The command generates crunz.yml
in the project ROOT
directory.
You can configure it with crunz.yml
, see also https://github.com/crunzphp/crunz#configuration
I recommend changing source:
to:
source: vendor/elstc/cakephp-cron-jobs/tasks
This makes it unnecessary to specify a directory when using schedule:run
and schedule:list
command.
Register to cron
Add your cron schedule using crontab -e
:
* * * * * cd {YOUR-APP-DIR}; bin/cake CronJobs schedule:run vendor/elstc/cakephp-cron-jobs/tasks/
Usage
You can register a scheduled job from the CakePHP event system.
Register to job scheduler in bootstrap_cli.php using the CakePHP event system:
use Cake\Event\Event; use Cake\Event\EventManager; EventManager::instance()->on('CronJobs.buildSchedule', static function (Event $event) { /** @type \Elastic\CronJobs\Schedule\CakeSchedule $schedule */ $schedule = $event->getSubject(); // Add a scheduled command $schedule->run('touch tmp/crunz-time-from-event') ->description('your job description') ->everyDay() ->at('09:00'); // Add a scheduled cake's command // such as `bin/cake your_command command_arg1 --command-option --some-opt=value` $schedule->runCommand('your_command', [ 'command_arg1', '--command-option', '--some-opt' => 'value', ]) ->description('your job description') ->cron('0 3 * * *'); });
\Elastic\CronJobs\Schedule\CakeSchedule
is a \Crunz\Schedule
wrapper class.
See also: crunzphp/crunz README
Show scheduled jobs
bin/cake CronJobs schedule:list vendor/elstc/cakephp-cron-jobs/tasks/
Upgrade from CakePHP 3
crunzphp/crunz updated from 1.12 to 2.x (<= PHP 7.3), 3.x (>= PHP 7.4). See also crunz's Upgrade Guide.