This plugin is simple wrapper lavary/crunz.
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 the plugin by adding the following statement in your project's src/Application.php
:
$this->addPlugin('Elastic/CronJobs');
Run bin/cake CronJobs publish:config
command.
The command generate crunz.yml
in the project ROOT
directory.
You can configure with crunz.yml
, see also https://github.com/lavary/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.
add your cron schedule, use crontab -e
* * * * * cd {YOUR-APP-DIR}; bin/cake CronJobs schedule:run vendor/elstc/cakephp-cron-jobs/tasks/
You can register a schedule job from the CakePHP event system.
Register to job schduler in bootstrap_cli.php, using 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 scheduled command
$schedule->run('touch tmp/crunz-time-from-event')
->description('your job description')
->everyDay()
->at('09:00');
// Add scheduled cake's command
// such as `bin/cake your_command comannd_arg1 --command-option --some-opt=value`
$schedule->runCommand('your_command', [
'comannd_arg1',
'--command-option',
'--some-opt' => 'value',
])
->description('your job description')
->cron('0 3 * * *');
});
\Elastic\CronJobs\Schedule\CakeSchedule
is \Crunz\Schedule
wrapper class.
See also: lavary/crunz README
bin/cake CronJobs schedule:list vendor/elstc/cakephp-cron-jobs/tasks/
larvery/crunz updated from 1.12 to 2.x(<= PHP 7.3), 3.x(>= PHP 7.4), See also crunz's Upgrade Guide.