lordsimal/cakephp-scheduler

Scheduler plugin for CakePHP

Installs: 84

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 1

Forks: 2

Open Issues: 1

Type:cakephp-plugin

1.1.0 2024-09-15 07:09 UTC

This package is auto-updated.

Last update: 2024-11-01 22:04:03 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require codecov

What can this plugin do?

This tool allows you to move all your cron jobs from being server configured via crontab to being app controlled in your CakePHP application (and plugins).

Requirements

  • PHP 8.1+
  • CakePHP 5.0+

Installation

composer require lordsimal/cakephp-scheduler

Loading plugin

In Application.php

public function bootstrap()
{
    parent::bootstrap();

    $this->addPlugin(\CakeScheduler\CakeSchedulerPlugin::class);
}

Or use the cake CLI.

bin/cake plugin load CakeScheduler

Usage

Defining a schedule

Either your app or your plugin need to implement the CakeSchedulerInterface which will add the schedule(Scheduler &$scheduler) method.

<?php
 
namespace App;

use App\Command\MyAppCommand;
use App\Command\OtherAppCommand;
use Cake\Http\BaseApplication;
use CakeScheduler\CakeSchedulerInterface;
use CakeScheduler\Scheduler\Scheduler;

class Application extends BaseApplication implements CakeSchedulerInterface
{
    public function schedule(Scheduler &$scheduler): void
    {
        $scheduler->execute(MyAppCommand::class)->daily();
        $scheduler->execute(OtherAppCommand::class, ['somearg', '--myoption=someoption'])->daily();
    }
}

with the ->execute() method you define which Command should be executed.

Each ->execute() method will return a \CakeScheduler\Scheduler\Event instance which is used to tell the scheduler when the command should be executed.

Available frequencies

Also see the EventTest for all the available options

List all scheduled events

bin/cake schedule:view

Running the Scheduler

You still need the following entry in your crontab

* * * * * cd /path-to-your-project && bin/cake schedule:run >> /dev/null 2>&1

Credit where credit is due

This plugin is heavily inspired by the Laravel Task Scheduling Feature

License

The plugin is available as open source under the terms of the MIT License.