tobento/app-schedule

应用调度支持。

1.0.0 2023-12-01 14:26 UTC

This package is auto-updated.

Last update: 2024-09-30 02:08:05 UTC


README

使用调度服务对应用进行调度支持。

目录

入门指南

使用此命令添加应用调度项目的最新版本。

composer require tobento/app-schedule

需求

  • PHP 8.0 或更高版本

文档

应用

如果您使用的是骨架,请查看 应用骨架

您还可以查看 应用,了解更多关于应用的信息。

调度启动

调度启动执行以下操作:

  • 实现调度接口
  • 启动 控制台启动 并向控制台添加调度命令
use Tobento\App\AppFactory;
use Tobento\Service\Schedule\ScheduleInterface;
use Tobento\Service\Schedule\ScheduleProcessorInterface;
use Tobento\Service\Schedule\TaskProcessorInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Schedule\Boot\Schedule::class);
$app->booting();

// Implemented interfaces:
$schedule = $app->get(ScheduleInterface::class);
$scheduleProcessor = $app->get(ScheduleProcessorInterface::class);
$taskProcessor = $app->get(TaskProcessorInterface::class);

// Run the app
$app->run();

如果您不使用 应用骨架,请查看 应用控制台启动 部分,您可能需要调整 app 文件。

安排任务

您可以通过多种方式安排任务:

使用应用

您可以使用应用的 on 方法来安排任务,但仅当请求调度时。

use Tobento\App\AppFactory;
use Tobento\Service\Schedule\ScheduleInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Schedule\Boot\Schedule::class);

// Adding tasks:
$app->on(ScheduleInterface::class, static function(ScheduleInterface $schedule): void {
    $schedule->task($task);
});

// Run the app
$app->run();

使用启动

您可以创建用于安排任务的启动

use Tobento\App\Boot;
use Tobento\App\Schedule\Boot\Schedule;
use Tobento\Service\Schedule\ScheduleInterface;

class MyScheduleTasksBoot extends Boot
{
    public const BOOT = [
        // you may ensure the schedule boot.
        Schedule::class,
    ];
    
    public function boot()
    {
        $this->app->on(ScheduleInterface::class, static function(ScheduleInterface $schedule): void {
            $schedule->task($task);
        });
    }
}

查看 调度服务 - 调度 部分以了解更多信息。

此外,所有 任务任务参数 都已经准备好使用,无需任何要求。

运行计划任务

要运行计划中的任务,请向您的服务器添加一个 cron 配置条目,每分钟运行一次 schedule:run 命令。

* * * * * cd /path-to-your-project && php app schedule:run >> /dev/null 2>&1

鸣谢