Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trentrichardson/cakephp-scheduler
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: CakeDC/cakephp-scheduler
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4.next-cake4
Choose a head ref
Able to merge. These branches can be automatically merged.

Commits on Jan 18, 2016

  1. Copy the full SHA
    30e4d3c View commit details

Commits on Jan 19, 2016

  1. Adds some tests

    trentrichardson committed Jan 19, 2016
    Copy the full SHA
    12d9647 View commit details
  2. Copy the full SHA
    98c6abf View commit details
  3. Copy the full SHA
    104307c View commit details
  4. Updates README

    trentrichardson committed Jan 19, 2016
    Copy the full SHA
    3961c37 View commit details
  5. Updates README

    trentrichardson committed Jan 19, 2016
    Copy the full SHA
    77fe405 View commit details

Commits on Jan 20, 2016

  1. Copy the full SHA
    675c3bf View commit details
  2. Copy the full SHA
    d9a1f78 View commit details
  3. Copy the full SHA
    a6c30b4 View commit details
  4. Copy the full SHA
    0708fe6 View commit details
  5. Copy the full SHA
    dd63453 View commit details
  6. Copy the full SHA
    a8dca4e View commit details
  7. Adds tmp directory

    trentrichardson committed Jan 20, 2016
    Copy the full SHA
    8c9f0d8 View commit details

Commits on Jan 22, 2016

  1. Copy the full SHA
    91712ac View commit details

Commits on Jan 25, 2016

  1. Copy the full SHA
    4cc3754 View commit details
  2. Copy the full SHA
    94724ae View commit details

Commits on Dec 4, 2024

  1. Update composer.json

    arodu authored Dec 4, 2024
    Copy the full SHA
    3113bd7 View commit details

Commits on Dec 7, 2024

  1. fix(39176): Add initial implementation of Scheduler plugin with datab…

    …ase strategy and schema
    arodu committed Dec 7, 2024
    Copy the full SHA
    8025ae9 View commit details
  2. fix(39176): Validate strategy class existence and ensure options are …

    …set for SchedulerCommand
    arodu committed Dec 7, 2024
    Copy the full SHA
    357461d View commit details

Commits on Dec 12, 2024

  1. Copy the full SHA
    f0a3b2f View commit details

Commits on Dec 18, 2024

  1. feat: implement SchedulerStoreData for strategy management and enhanc…

    …e error handling in SchedulerCommand
    arodu committed Dec 18, 2024
    Copy the full SHA
    b51a834 View commit details

Commits on Dec 23, 2024

  1. Copy the full SHA
    877423e View commit details
  2. Copy the full SHA
    a330dae View commit details

Commits on Dec 24, 2024

  1. Copy the full SHA
    f54f3a4 View commit details

Commits on Feb 7, 2025

  1. Copy the full SHA
    90bff53 View commit details
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/composer.lock
/composer.phar
/phpunit.xml
/.phpunit.result.cache
/phpunit.phar
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
117 changes: 5 additions & 112 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,118 +1,11 @@
CakePHP Scheduler Plugin
========================
# Scheduler plugin for CakePHP

Makes scheduling tasks in CakePHP much simpler.
## Installation

Author
------
Trent Richardson [http://trentrichardson.com]
You can install this plugin into your CakePHP application using [composer](https://getcomposer.org).

License
-------
Copyright 2015 Trent Richardson
The recommended way to install composer packages is:

You may use this project under MIT license.
http://trentrichardson.com/Impromptu/MIT-LICENSE.txt

How It Works
------------
SchedulerShell works by scheduling one cron (SchedulerShell) for your project. Then in bootstrap.php you can create intervals for all your tasks. Deploying new scheduled tasks are now much easier; one crontab entry executes all your tasks.

Install
-------

This Shell was developed for CakePHP 3.

### Composer Installation (recommended)

In your project's composer.json file add this to your require:

````
"trentrichardson/cakephp-scheduler": "~3.0"
````

### Manual Installation

Copy the Scheduler plugin into your App/Plugin folder and rename the folder to Scheduler.

### Load the Plugin

In your bootstrap.php file add either:

```php
Plugin::loadAll();
```

or

```php
Plugin::load('Scheduler',['autoload'=>true]);
composer require cakedc/cakephp-scheduler
```

Schedule a single system cron by the shortest interval you need for SchedulerShell.php. For example, if you have 5 tasks and the most often run is every 5 minutes, then schedule this cron to run at least every 5 minutes. For more help see [Shells as Cron Jobs](http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html).

Example cron job:

````
*/5 * * * * cd /path/to/app && bin/cake Scheduler.Scheduler
````

This would run the SchedulerShell every 5 minutes.

Now once this shell is scheduled we are able to add our entries to bootstrap.php. Lets say we want to schedule a CleanUp task daily at 5am and a NewsletterTask for every 15 minutes.

```php
Configure::write('SchedulerShell.jobs', array(
'CleanUp' => array('interval' => 'next day 5:00', 'task' => 'CleanUp'),// tomorrow at 5am
'Newsletters' => array('interval' => 'PT15M', 'task' => 'Newsletter') //every 15 minutes
));
```

The key to each entry will be used to store the previous run. *These must be unique*!

**interval** is set one of two ways.
1) For set times of day we use PHP's [relative time formats](http://www.php.net/manual/en/datetime.formats.relative.php): "next day 5:00".

2) To use an interval to achieve "every 15 minutes" we use [DateInterval](http://www.php.net/manual/en/class.dateinterval.php) string format: "PT15M".

**task** is simply the name of the Task.

There are a couple optional arguments you may pass: "action" and "pass".

**action** defaults to "execute", which is the method name to call in the task you specify.

**pass** defaults to array(), which is the array of arguments to pass to your "action".

```php
Configure::write('SchedulerShell.jobs', array(
'CleanUp' => array('interval' => 'next day 5:00', 'task' => 'CleanUp', 'action' => 'execute', 'pass' => array()),
'Newsletters' => array('interval' => 'PT15M', 'task' => 'Newsletter', 'action' => 'execute', 'pass' => array())
));
```

Storage of Results
------------------
SchedulerShell keeps track of each run in a json file. By default this is stored in TMP and is named "cron_scheduler.json".

If you need to change either of these you may use:

```php
// change the file name
Configure::write('SchedulerShell.storeFile', "scheduler_results.json");
// change the path (note the ending /)
Configure::write('SchedulerShell.storePath', "/path/to/save/");
```

Preventing Simultaneous SchedulerShells Running Same Tasks
----------------------------------------------------------
By default, the SchedulerShell will exit if it is already running and has been for less than 10 minutes. You can adjust this by setting:

```php
// change the number of seconds to wait before running a parallel SchedulerShell; 0 = do not exit
Configure::write('SchedulerShell.processTimeout', 5*60);
```

Other Notes/Known Issues
------------------------
- The optional pass arguments have not been thoroughly tested
- PHP prior to version 5.3.6 only used relative datetime for the DateTime::modify() function. This could result in an interval of "next day 5:00" not running if the previous `lastRun` time was 05:02. Therefore this plugin should only be run on PHP >= 5.3.6.
42 changes: 22 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
"name": "trentrichardson/cakephp-scheduler",
"type": "cakephp-plugin",
"description": "Makes scheduling tasks in CakePHP much simpler.",
"keywords": ["cakephp","scheduler","cron job","task"],
"homepage": "http://github.com/trentrichardson/cakephp-scheduler",
"license": "MIT",
"autoload": {
"psr-4": {
"Scheduler\\": "src"
}
},
"require": {
"php": ">=5.3.6"
},
"support": {
"source": "https://github.com/trentrichardson/cakephp-scheduler"
},
"extra": {
"installer-name": "Scheduler"
}
"name": "cakedc/cakephp-scheduler",
"description": "Scheduler plugin for CakePHP",
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"php": ">=7.2",
"cakephp/cakephp": "^4.5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3"
},
"autoload": {
"psr-4": {
"Scheduler\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Scheduler\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
}
}
61 changes: 61 additions & 0 deletions config/Migrations/20241206202437_CreateSchedulerStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateSchedulerStore extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* @return void
*/
public function change(): void
{
$table = $this->table('scheduler_store');
$table->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
'comment' => 'Name of the job',
]);
$table->addColumn('interval_job', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
'comment' => 'Interval of the job',
]);
$table->addColumn('task', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
'comment' => 'Task to run, src/Job/Service/*',
]);
$table->addColumn('pass', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
'comment' => 'Parameters to pass to the task',
]);
$table->addColumn('lastRun', 'datetime', [
'default' => null,
'null' => false,
'comment' => 'Last time the job was run',
]);
$table->addColumn('lastResult', 'integer', [
'default' => 0,
'limit' => 1,
'null' => false,
'comment' => 'Last result of the job, 0 = success, 1 = failure',
]);
$table->AddColumn('paused', 'boolean', [
'default' => false,
'null' => false,
'comment' => 'Is the job paused',
]);
$table->create();
}
}
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Scheduler">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup fixture extension -->
<extensions>
<extension class="Cake\TestSuite\Fixture\PHPUnitExtension" />
</extensions>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
Loading