elstc/cakephp-cron-jobs

A cron job runner for CakePHP

Installs: 1 584

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 2

Forks: 0

Open Issues: 0

Type:cakephp-plugin

v0.3.0 2019-11-13 10:46 UTC

This package is auto-updated.

Last update: 2021-03-23 14:00:55 UTC


README

Software License Build Status Codecov Latest Stable Version

This plugin is simple wrapper lavary/crunz.

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

(CakePHP >= 3.6.0) Load the plugin by adding the following statement in your project's src/Application.php:

$this->addPlugin('Elastic/CronJobs');

(CakePHP <= 3.5.x) Load the plugin by adding the following statement in your project's config/bootstrap.php file:

Plugin::load('Elastic/CronJobs');

Generate config file

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.

Register to cron

add your cron schedule, use crontab -e

* * * * * cd {YOUR-APP-DIR}; bin/cake CronJobs schedule:run vendor/elstc/cakephp-cron-jobs/tasks/

Usage

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) {
    $schedule = $event->getSubject();
    /* @var $schedule \Elastic\CronJobs\Schedule\CakeSchedule */
    
    // Add scheduled shell command
    $schedule->run('touch tmp/crunz-time-from-event')
        ->description('your job description')
        ->everyDay()
        ->at('09:00');

    // Add scheduled cake's shell 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

Show scheduled jobs

bin/cake CronJobs schedule:list vendor/elstc/cakephp-cron-jobs/tasks/