flexic/scheduler

提供调度扩展。

2.0.4 2023-03-19 15:48 UTC

This package is auto-updated.

Last update: 2024-09-19 18:47:26 UTC


README

PHP调度是一个用于在PHP中安排任务的简单库。
它受到Laravel SchedulerSymfony 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

Donate