creatortsv/scheduler
Composer 安装命令:
composer require creatortsv/scheduler
包简介
Stand-alone package which provides functionality to schedule script execution
README 文档
README
Stand-alone package which provides functionality to schedule script execution.
Installation
composer install creatortsv/scheduler
Getting Started
It's easy to start using the scheduler.
use Creatortsv\Scheduler\Registrar\ScheduleRegistrar; use Creatortsv\Scheduler\Scheduler; $job = function (): void { /* your job logic here ... */ } $scheduler = new Scheduler(); $scheduler->getRegistrar()->register($job); $scheduler->run();
The example above just registered simple closure function as a job with default cron expression * * * * * (which means that it will be executed every minute) and run it.
Cron Expression
For complete documentation of the Cron Expression object visit https://github.com/dragonmantank/cron-expression
Each job has its own cron expression object. The register method of the ScheduleRegistrarInterface returns a cron expression object with default value * * * * *. So you can change it after your job had been registered.
/* Initializing scheduler ... */ $expr = $scheduler->getRegistrar()->register($job); $expr->setExpression('0 */2 * * *'); /* every two days at 00:00 */
Schedule Provider
Sometimes you want to register several jobs that could be grouped by specific logic. In that case Schedule Provider is more useful.
use Creatortsv\Scheduler\Provider\ScheduleProviderInterface; use Creatortsv\Scheduler\Registrar\ScheduleRegistrarInterface; class MyScheduleProvider implements ScheduleProviderInterface { public function boot(ScheduleRegistrarInterface $registrar): void { // Register specific jobs here ... } }
Don't forget to add your provider via scheduler object
/* Initializing scheduler ... */ $scheduler->add(new MyScheduleProvider());
Run Scheduler
/* Initializing scheduler ... */ $scheduler->run();
By default, the Scheduler is running with the current DateTime object for each job, that means it will create the DateTime object and each Cron Expression object using the same timestamp determine if it is the time to execute job or not.
But sometimes you need run your schedule on the specific timestamp, testing for example. Use Scheduler::at method to change default behaviour.
/* Initializing scheduler ... */ $date = new DateTime(); $date->modify('-3 days'); $scheduler->at($date)->run();
Advanced Usage
Sometimes you may want to control which jobs must be executed in addition of default behaviour. Only one instance of the registered job should be running at the current time, for example.
In that cases you can use Scheduler::boot method instead of Scheduler::run. The boot method returns ScheduleIterator object, each item is a job instance which must be executed by the given timestamp and each key is cron expression of given job.
Let's imagine that you have some service which determines that job is already being executed
class JobManager { public function isReleased(SpecificJobInterface $job): bool { /* your logic is here ... */ } }
So you can use this service
/* Initializing $scheduler and your $jobManager ... */ foreach ($scheduler->boot() as $job) { if ($job instanceof SpecificJobInterface) { $jobManager->isReleased($job) && $job(); } }
But there is the best way to do so
/* Initializing $scheduler and your $jobManager ... */ $scheduler->getModerator()->register($jobManager->isReleased(...)); $scheduler->run();
And you shouldn't have to worry about if the job has different type of callable object
creatortsv/scheduler 适用场景与选型建议
creatortsv/scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.63k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 creatortsv/scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 creatortsv/scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-12