tobento/app-queue

队列应用程序支持。

1.0.1 2024-02-21 16:18 UTC

This package is auto-updated.

Last update: 2024-09-21 17:29:25 UTC


README

使用 队列服务 的应用程序队列支持。

目录

入门

使用以下命令安装应用程序队列项目的最新版本。

composer require tobento/app-queue

要求

  • PHP 8.0 或更高版本

文档

应用程序

如果您正在使用骨架,请查看 应用程序骨架

您还可以查看 应用程序 以了解更多关于应用程序的一般信息。

队列启动

队列启动执行以下操作:

  • 安装和加载队列配置文件
  • 实现队列接口
use Tobento\App\AppFactory;
use Tobento\Service\Queue\QueueInterface;
use Tobento\Service\Queue\QueuesInterface;
use Tobento\Service\Queue\JobProcessorInterface;
use Tobento\Service\Queue\FailedJobHandlerInterface;

// 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\Queue\Boot\Queue::class);
$app->booting();

// Implemented interfaces:
$queue = $app->get(QueueInterface::class);
$queues = $app->get(QueuesInterface::class);
$jobProcessor = $app->get(JobProcessorInterface::class);
$failedJobHandler = $app->get(FailedJobHandlerInterface::class);

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

控制台备注

队列启动会自动启动 应用程序控制台启动 以通过命令运行队列工作进程。

如果您不使用 应用程序骨架,您可能需要调整根目录中的 app 文件,以包含您的应用程序路径。

// Get and run the application.
// (require __DIR__.'/app/app.php')->run();
(require __DIR__.'/path/to/app.php')->run();

队列配置

队列配置位于默认 应用程序骨架 配置位置 app/config/queue.php 文件中,您可以在此处指定应用程序的队列。

创建任务

查看 队列服务 - 创建任务 部分以了解更多关于创建任务的信息。

调度任务

查看 队列服务 - 调度任务 部分以了解更多关于创建任务的信息。

运行队列

要运行队列,您可以使用 queue:work 控制台命令运行 队列工作进程

php app queue:work

查看 队列服务 - 工作命令 部分以了解更多关于该命令的信息。

为了永久在后台运行 queue:work 进程,您应该使用进程监视器(如 Supervisor)来确保队列工作进程不会停止运行。

替代方案

或者,您可以通过使用每分钟运行一次的 应用程序调度命令任务 来运行 queue:work 命令。

use Tobento\Service\Schedule\Task\CommandTask;

$task = (new CommandTask(
    command: 'queue:work',
    input: [
        // you may stop the queue to work when it is empty:
        '--stop-when-empty' => null,
        
        // you may run only a specific queue instead of all:
        '--queue' => 'secondary',
        
        // it is advised to define the timeout in seconds
        // before your server times out, otherwise
        // failed jobs might not be handled.
        '--timeout' => 60,
    ],
))->cron('* * * * *');

失败的任务

默认情况下,失败的任务将由实现的 \Tobento\App\Queue\LogFailedJobHandler::class 处理。任何失败的任务都会重新推送到队列,直到达到最大重试次数。一旦达到最大重试次数,任务将不再排队,并将发送到应用程序日志。

应用程序日志配置 文件中,您可以定义特定的记录器。

/*
|--------------------------------------------------------------------------
| Aliases
|--------------------------------------------------------------------------
*/

'aliases' => [
    \Tobento\App\Queue\LogFailedJobHandler::class => 'error',
],

自定义处理器

您可以通过创建自定义失败任务处理器并使用应用程序的 on 方法来替换实现的处理器来更改默认行为。

use Tobento\Service\Queue\FailedJobHandlerInterface;

$app->on(FailedJobHandlerInterface::class, function (): FailedJobHandlerInterface {
    return new CustomFailedJobHandler();
});

致谢