flexic / scheduler
提供调度扩展。
2.0.4
2023-03-19 15:48 UTC
Requires
- php: ^8.1
- dragonmantank/cron-expression: ^3.3.2
- flexic/cron-builder: ^1.0.0
- psr/log: ^3.0.0
- symfony/console: ^6.1.0
- symfony/event-dispatcher: ^6.1.0
- symfony/options-resolver: ^6.1.0
Requires (Dev)
- ergebnis/php-cs-fixer-config: ^4.9.0
- phpstan/phpstan: ^1.8.8
- phpstan/phpstan-deprecation-rules: ^1.0.0
- phpstan/phpstan-strict-rules: ^1.4.4
README
PHP调度是一个用于在PHP中安排任务的简单库。
它受到Laravel Scheduler和Symfony Messenger的启发。
安装
运行
composer require flexic/scheduler
来安装 flexic/scheduler
。
设置要安排的事件
class MyScheduleEvent implements \Flexic\Scheduler\Interfaces\AbstractScheduleEventInterface { public function __invoke(): void { // ... do something } public function configure(Flexic\Scheduler\Interfaces\ScheduleInterface $schedule): void { $schedule->cron('* * * * *'); } }
调度事件是实现了ScheduleEventInterface
的类。在configure
方法中,你可以使用Schedule
对象来定义事件何时应该被安排运行。
设置调度工作进程(控制台命令)
运行
php bin/schedule ./path/to/event_config.php ./path/to/event_config_1.php
来启动调度工作进程。工作进程将自动从给定的配置文件中加载所有事件并运行它们。
选项
设置调度工作进程(自己的脚本)
# Options for worker $options = []; $events = [ new MyScheduleEvent(), ]; $worker = new \Flexic\Scheduler\Worker( new Flexic\Scheduler\Configuration\WorkerConfiguration($options), $events, new \Symfony\Component\EventDispatcher\EventDispatcher(), ); $worker->start();
调度事件工厂
ScheduleEventInterface
已实现,允许使用工厂来创建事件。如果你想要使用依赖注入容器来创建事件,这很有用。
class MyScheduleEventFactory implements \Flexic\Scheduler\Interfaces\ScheduleEventFactoryInterface { public function create(): array { return [ new MyScheduleEvent('foo'), new MyScheduleEvent('bar'), ]; } }
调度API
cron()
方法接受字符串,或CronBuilder
& Cron
类型的对象,来自flexic/cron-builder
。
用于标记的方法允许使用来自flexic/cron-builder
的表达式。
工作进程API
工作进程生命周期事件
许可证
本软件包使用GNU许可证进行许可。
请参阅LICENSE.md。