1. Go to this page and download the library: Download zoujingli/type-scheduler 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/ */
zoujingli / type-scheduler example snippets
declare(strict_types=1);
use Type\Scheduler\CronSchedule;
use Type\Scheduler\Definition;
use Type\Scheduler\FileStateStore;
use Type\Scheduler\Scheduler;
use Type\Scheduler\SystemClock;
use Type\Scheduler\Task;
use Type\Scheduler\TaskContext;
/** 展示显式任务接口;每次计划执行由调度器建立独立上下文。 */
final class DailyReport implements Task
{
/** @return array{completed: bool} 有界且可记录的执行结果。 */
public function run(TaskContext $context): array
{
$context->assertActive();
return ['completed' => true];
}
}
/**
* 在启动期启用 I/O hook,再于同一协程装配并执行一次有限调度。
* 文件目录的创建与权限属于部署方责任。
*
* @param list<string> $argv 第二项为安全本地状态文件路径。
*/
function main(int $argc, array $argv): void
{
\Type\Runtime\CoroutineRuntime::enableIo();
\Type\Runtime\CoroutineRuntime::run(static function () use ($argv): void {
$path = $argv[1] ?? '';
if ($path === '') {
throw new InvalidArgumentException('请传入已准备目录中的调度状态文件绝对路径');
}
$scheduler = new Scheduler(new SystemClock(), new FileStateStore($path), [
new Definition('reports.daily', new CronSchedule('0 9 * * *', 'Asia/Shanghai'),
static fn (TaskContext $context): Task => new DailyReport()),
]);
try {
echo json_encode($scheduler->tick(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) . "\n";
} finally {
$scheduler->stop();
}
});
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.