1. Go to this page and download the library: Download tonychen/php-task library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tonychen / php-task example snippets
ony\Task\Job\Job;
use Tony\Task\Runner;
use Tony\Task\Scheduler;
use Tony\Task\Struct\ProcessConfig;
class Demo extends Job
{
public function execute(Scheduler $subject): void
{
/**@var Scheduler $subject */
$expression = $subject->getTimer()->getExpression();
error_log(date('Y-m-d H:i:s') . "\t{$expression}\n", 3, '/tmp/php-task.log');
}
}
$processConfig = new ProcessConfig();
$processConfig->pidFile = '/tmp/php-task.pid';
$processConfig->stdErr = '/dev/null';
$processConfig->stdOut = '/dev/null';
$processConfig->stdIn = '/dev/null';
$schedule = new Scheduler();
$schedule->getTimer()->everyMinute();
$schedule->attach(new Demo());
// 5分钟执行一次
$schedule5 = new Scheduler();
$schedule5->getTimer()->everyFiveMinutes();
$schedule5->attach(new Demo());
// 30分钟执行一次
$schedule30 = new Scheduler();
$schedule30->getTimer()->everyThirtyMinutes();
$schedule30->attach(new Demo());
// 自定义事件表达式
// 2个小时执行一次
$schedule2Hours = new Scheduler();
$schedule2Hours->getTimer()->setExpression("* */2 * * *");
$schedule2Hours->attach(new Demo());
$schedules = new SplObjectStorage();
$schedules->attach($schedule);
$schedules->attach($schedule5);
$schedules->attach($schedule30);
$schedules->attach($schedule2Hours);
$runner = new Runner();
$runner->setProcessConfig($processConfig);
$runner->setSchedulers($schedules);
$runner->run();