ipl/scheduler
Composer 安装命令:
composer require ipl/scheduler
包简介
Icinga PHP Library - Tasks scheduler
关键字:
README 文档
README
ipl-scheduler provides an event-loop-driven task scheduler for PHP. It runs
tasks at regular intervals or once at a specific time, using an event-driven
and promise-based model built on top of ReactPHP.
Tasks are plain PHP classes. Frequency is configured independently via cron expressions, one-off datetimes, or iCalendar recurrence rules. The scheduler emits lifecycle events so you can observe scheduling, execution, completion, failures, and expiration without coupling your tasks to the scheduler itself.
Installation
The recommended way to install this library is via Composer:
composer require ipl/scheduler
ipl/scheduler requires PHP 8.2 or later.
For production use, install the ext-ev
PHP extension to improve event loop efficiency.
Key Abstractions
| Class | Description |
|---|---|
Scheduler |
Manages tasks in the ReactPHP event loop |
Cron |
Repeating frequency driven by a cron expression |
RRule |
Repeating frequency driven by an iCalendar RRULE |
OneOff |
Single-run frequency at an exact point in time |
Usage
Implementing a Task
Implement the Task interface. Use the TaskProperties trait to satisfy the
getName(), getUuid(), and getDescription() requirements without
boilerplate.
Return resolve() or reject() when the result is already available synchronously.
For background work such as async I/O, subprocess calls, or non-blocking HTTP requests,
use a Deferred and settle it once the operation finishes:
use ipl\Scheduler\Common\TaskProperties; use ipl\Scheduler\Contract\Task; use Ramsey\Uuid\Uuid; use React\EventLoop\Loop; use React\Promise\Deferred; use React\Promise\PromiseInterface; use Throwable; use function React\Promise\resolve; class SendReport implements Task { use TaskProperties; public function __construct() { $this->setName('send-report'); $this->setUuid(Uuid::uuid4()); } public function run(): PromiseInterface { // Use resolve()/reject() only when the result is already available synchronously. // return resolve('Report sent.'); // Use a Deferred when the work happens asynchronously, e.g. after a // subprocess call, a stream read, or a non-blocking HTTP call completes. $deferred = new Deferred(); Loop::futureTick(function () use ($deferred): void { try { // Perform actual work here. $deferred->resolve('Report sent.'); } catch (Throwable $e) { $deferred->reject($e); } }); return $deferred->promise(); } }
Note: Always call either
resolve()orreject()on every code path. ADeferredwhose promise is never settled will silently preventON_TASK_DONEandON_TASK_FAILEDfrom firing.
Scheduling with a Cron Expression
Use Cron to run a task on a repeating schedule defined by a standard
five-field cron expression:
use ipl\Scheduler\Cron; use ipl\Scheduler\Scheduler; use React\EventLoop\Loop; $scheduler = new Scheduler(); // Run every day at 08:00. $scheduler->schedule(new SendReport(), new Cron('0 8 * * *')); Loop::run();
Scheduling a One-Off Run
Use OneOff to run a task exactly once at a specific datetime:
use ipl\Scheduler\OneOff; use ipl\Scheduler\Scheduler; use React\EventLoop\Loop; $scheduler = new Scheduler(); $scheduler->schedule(new SendReport(), new OneOff(new DateTime('2026-04-01 09:00:00'))); Loop::run();
Scheduling with an iCalendar Recurrence Rule
Use RRule for complex recurrence patterns. Pass a timezone string to align
recurrences with a specific offset:
use ipl\Scheduler\RRule; use ipl\Scheduler\Scheduler; use React\EventLoop\Loop; $scheduler = new Scheduler(); // Run every day at 09:00 Europe/Berlin time. $frequency = RRule::fromFrequency(RRule::DAILY); $frequency ->setTimezone('Europe/Berlin') ->startAt(new DateTime('2026-01-01 09:00:00')); $scheduler->schedule(new SendReport(), $frequency); Loop::run();
You can also construct an RRule directly from an RRULE string:
$frequency = new RRule('FREQ=WEEKLY;BYDAY=MO,WE,FR');
Listening to Lifecycle Events
The scheduler emits events at each stage of a task's lifecycle. Register
listeners using on():
use ipl\Scheduler\Scheduler; use ipl\Scheduler\Contract\Task; use React\Promise\PromiseInterface; $scheduler = new Scheduler(); $scheduler->on(Scheduler::ON_TASK_SCHEDULED, function (Task $task, DateTime $runAt) { echo sprintf('Task "%s" scheduled for %s', $task->getName(), $runAt->format('c')); }); $scheduler->on(Scheduler::ON_TASK_RUN, function (Task $task, PromiseInterface $promise) { echo sprintf('Task "%s" is running', $task->getName()); }); $scheduler->on(Scheduler::ON_TASK_DONE, function (Task $task, mixed $result) { echo sprintf('Task "%s" completed: %s', $task->getName(), $result); }); $scheduler->on(Scheduler::ON_TASK_FAILED, function (Task $task, Throwable $e) { echo sprintf('Task "%s" failed: %s', $task->getName(), $e->getMessage()); }); $scheduler->on(Scheduler::ON_TASK_EXPIRED, function (Task $task, DateTime $expiredAt) { echo sprintf('Task "%s" expired at %s', $task->getName(), $expiredAt->format('c')); });
Available Events
| Constant | Triggered when |
|---|---|
Scheduler::ON_TASK_SCHEDULED |
An operation is queued for future execution |
Scheduler::ON_TASK_RUN |
An operation starts running |
Scheduler::ON_TASK_DONE |
An operation completes successfully |
Scheduler::ON_TASK_FAILED |
An operation throws or rejects |
Scheduler::ON_TASK_EXPIRED |
A task has passed its configured end time |
Scheduler::ON_TASK_CANCEL |
A task is removed while an operation is pending |
Removing a Task
Call remove() to cancel a scheduled task at any time. Any pending
operations are canceled and the ON_TASK_CANCEL event is emitted:
$scheduler->remove($task);
To remove all scheduled tasks at once:
$scheduler->removeTasks();
Changelog
See CHANGELOG.md for a list of notable changes.
License
ipl/scheduler is licensed under the terms of the MIT License.
ipl/scheduler 适用场景与选型建议
ipl/scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.62k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cron」 「task」 「job」 「scheduler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ipl/scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ipl/scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ipl/scheduler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle to monitor and execute commands
Provides a possibility to auto-setup crontabs required for project based on configuration file.
SlimQ Enables You to push jobs to background workers
PHP client for Kue priority job queue API
Expose the DDev executable commands to the Robo task runner.
Scheduling extension for Yii2 framework
统计信息
- 总下载量: 29.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-17