Scheduler
, (*1)
PHP job scheduler. (Cronjob tasker), (*2)
Installing
Via composer:, (*3)
composer require 10quality/scheduler
Usage
Scheduler
Create a php file that will be called by cronjob, Sample, and include the following code lines:, (*4)
// 1) LOAD COMPOSER AUTOLOAD OR BOOTSTRAP FIRST
// 2)
use Scheduler\Scheduler;
Init scheduler with the required configuration:, (*5)
Scheduler::ready([
'jobs' => ['path' => __DIR__.'/jobs'],
'session' => ['driver' => 'file', 'path'=>__DIR__.'/.tmp'],
]);
NOTE: Scheduler requires a defined jobs path (where jobs are located) and the session driver/settings to use to handle intervals (the package only supports file atm)., (*6)
Register your jobs and execution interval., (*7)
Scheduler::ready([...])
->job('MyJob', function($task) {
return $task->daily();
});
Start scheduler:, (*8)
Scheduler::ready([...])
->job(...)
->start();
Then setup a cronjob task to run scheduler, as follows:, (*9)
* * * * * php /path/to/scheduler-file.php >> /dev/null 2>&1
Jobs
Scheduler runs jobs written in PHP. A job is a PHP class extended from Scheduler\Base\Job
class. Once a job is registered in the scheduler, it will call to the execute()
function in runtime, Sample., (*10)
Create a job mimicking the following example:, (*11)
use Scheduler\Base\Job;
class MyJob extends Job
{
public function execute()
{
// My code here...
}
}
For the example above, the job file must be named MyJob.php
and must be stored in the jobs path
., (*12)
Intervals
Execution intervals are defined when registering a job:, (*13)
Scheduler::ready([...])
->job('MyJob', function($task) {
// Here we define the task interval
return $task->daily();
});
Available intervals:, (*14)
// On every execution
$task->now();
// Daily
$task->daily();
// Weekly
$task->weekly();
// Monthly
$task->monthly();
// Every minute
$task->everyMinute();
// Every 5 minutes
$task->everyFiveMinutes();
// Every 10 minutes
$task->everyTenMinutes();
// Every 30 minutes
$task->everyHalfHour();
// Every hour
$task->everyHour();
// Every 12 hours
$task->everyTwelveHours();
Requirements
Coding guidelines
PSR-4., (*15)
LICENSE
The MIT License (MIT), (*16)
Copyright (c) 2016 10Quality., (*17)