wyrihaximus/react-cron

⏱️ 在 ReactPHP 事件循环中运行的类似 Cron 的调度器


README

Continuous Integration Latest Stable Version Total Downloads Code Coverage License

安装

要通过 Composer 安装,请使用以下命令,它将自动检测最新版本并将其绑定到 ^

composer require wyrihaximus/react-cron

用法

在 ReactPHP 事件循环中安排操作

use React\Promise\PromiseInterface;
use WyriHaximus\React\Cron;
use WyriHaximus\React\Cron\Action;

use function React\Promise\resolve;

Cron::create(
    new Action(
        'Hour', // Identifier used for mutex locking locking
        60, // TTL for the mutex lock, always set this way higher than the expected execution time, but low enough any failures during the run will cause issues
        '@hourly', // The cron expression used to schedule this action
        function (): PromiseInterface { // The callable ran when this action is due according to it's schedule
            echo 'Another hour has passed!', PHP_EOL;

            return resolve(true); // This callable MUST return a promise, which is used for releasing the mutex lock
        }
    ),
    new Action(
        'Minute',
        0.1,
        '* * * * *',
        function (): PromiseInterface {
            echo 'Another minute has passed!', PHP_EOL;

            return resolve(true);
        }
    )
);

// Stops scheduling new action runs
$cron->stop();

工厂方法

  • Cron::create($loop, ...$actions):具有内存互斥锁的 Cron。
  • Cron::createWithMutex($loop, $mutex, ...$actions):具有提供的互斥锁的 Cron。

互斥锁

所有互斥锁都必须实现 wyrihaximus/react-mutex 以提供除默认内存实现之外的额外实现。这旨在进行分布式锁定 cron 作业。

错误处理

使用 promise v3,未捕获的拒绝承诺将不再冒泡。因此,运行中的 con 实例现在在发生错误时将发出错误,并且必须处理这些错误。

$cron = Cron::create(...$actions);
$cron->on('error', static function (Throwable $throwable): void {
    // Handle error
});

许可

MIT 许可证 (MIT)

版权所有 (c) 2023 Cees-Jan Kiewiet

特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得软件的人进行此类操作,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,没有任何形式的保证,明示或暗示,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,无论是源于、因之或与此软件或软件的使用或其他交易有关。