2017 © Pedro Peláez
 

cakephp-plugin cake-scheduler

Cron Scheduler for CakePHP 3

image

watchowl/cake-scheduler

Cron Scheduler for CakePHP 3

  • Tuesday, July 10, 2018
  • by watchowl
  • Repository
  • 0 Watchers
  • 7 Stars
  • 86 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 2 Versions
  • 39 % Grown

The README.md

CakeScheduler

Cron Scheduler Plugin for CakePHP 3, (*1)

Table of Contents

Introduction

CakeScheduler allows you to write cron jobs right from PHP files. It works for CakePHP shell as well as any other valid PHP code. Basically it is a replacement of the conventional crontab file., (*2)

Why Use It

The conventional way of writing a cron job is to place an entry in the crontab file each time you need to schedule a job. The problem with this approach is that you have to login(SSH) to the server each time., (*3)

By using CakeScheduler, we are able to place the cron jobs in the source control system and deploy them to production just like any other PHP code., (*4)

Installation

  • To install the CakeScheduler plugin, you can use composer. From your application's ROOT directory (where composer.json file is located) run the following:, (*5)

    composer require watchowl/cake-scheduler, (*6)

  • You will need to add the following line to your application's config/bootstrap.php file:, (*7)

    Plugin::load('WatchOwl/CakeScheduler');, (*8)

Starting The Scheduler

We only need to install one ordinary cron job which runs every minute. This cron job will enable CakeScheduler to schedule all the subsequent jobs:, (*9)

* * * * * /path-to-project/bin/cake cake_scheduler run schedule:run, (*10)

Defining Schedules

A schedule is basically a PHP file ending with Tasks.php and it must return the CakeSchedule object. All schedules should be place inside a folder called schedule. This folder must reside at the root directory where composer.json file is located., (*11)

For example:, (*12)

// schedule/BackTasks.php
$schedule = new \WatchOwl\CakeScheduler\Schedule\CakeSchedule();
$schedule
    ->run('/usr/bin/php backup.php')
    ->daily()
    ->description('Test');
return $schedule;

Scheduling CakePHP Shell

To schedule a CakePHP shell, call CakeSchedule::shell:, (*13)

$schedule->shell('MyCake awesome'), (*14)

Scheduling Any Other Commands

To schedule any other commands, call CakeSchedule::run:, (*15)

$cakeSchedule->run('/usr/bin/php backup.php'), (*16)

Frequency Options

There is plenty of ways to define the frequency of the execution:, (*17)

Method Description
->cron() the classic way of defining a schedule
->hourly() beginning of each hour
->daily() daily at midnight
->weekly sunday of each week
->monthly() first day of each month
->quarterly() first day of each quarter
->yearly() first day of each year
->everyFiveMinutes() every five minutes
->everyMinute() every minute
->everyTwelveHours() every twelve hours
->everyMonth() every month
->everySixMonths() every six months
->everyFifteenDays() every fifteen days
->on('13:30 2016-03-01') at a specific date and time
->at('13:30') at a specific time

Under the hood, CakeSchedule is using the great lavary/crunz library. It has a large number of options for us to configure the frequency of the execution. Check out its official documentation if you are looking for more available frequency., (*18)

Hooks

Hooks make it easy to integrate with other services such as www.watchowl.io., (*19)

Before A Job Runs

To do something before a job is executed, we can use the before() hook:, (*20)

For example:, (*21)

$schedule = new \WatchOwl\CakeScheduler\Schedule\CakeSchedule();

$schedule
    ->run('/usr/bin/php backup.php')
    ->before(function() { 
        // Do something before the job runs
     })
    ->daily()
    ->description('Test');

return $schedule;

After A Job Is Finished

To do something after a job is executed, we can use the after() hook:, (*22)

For example:, (*23)

$schedule = new \WatchOwl\CakeScheduler\Schedule\CakeSchedule();

$schedule
    ->run('/usr/bin/php backup.php')
    ->after(function() { 
        // Do something after the job is finished
     })
    ->daily()
    ->description('Test');

return $schedule;

The Versions

10/07 2018

dev-master

9999999-dev

Cron Scheduler for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Li

03/10 2017

1.0.0

1.0.0.0

Cron Scheduler for CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Li