定制 tobento/app-queue 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tobento/app-queue

最新稳定版本:2.0

Composer 安装命令:

composer require tobento/app-queue

包简介

Queue app support.

README 文档

README

Queue support for the app using the Queue Service.

Table of Contents

Getting Started

Add the latest version of the app queue project running this command.

composer require tobento/app-queue

Requirements

  • PHP 8.4 or greater

Documentation

App

Check out the App Skeleton if you are using the skeleton.

You may also check out the App to learn more about the app in general.

Queue Boot

The queue boot does the following:

  • installs and loads queue config file
  • implements queue interfaces
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();

Console Note

The queue boot automatically boots the App Console Boot as to run the queue worker using commands.

If you are not using the App Skeleton you may adjust the app file in the root directory with the path to your app:

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

Queue Config

The configuration for the queue is located in the app/config/queue.php file at the default App Skeleton config location where you can specify the queues for your application.

Creating Jobs

Check out the Queue Service - Creating Jobs section to learn more about creating jobs.

Dispatching Jobs

Check out the Queue Service - Dispatching Jobs section to learn more about creating jobs.

Running Queues

To run queues you may run the Queue Worker using the queue:work console command.

php ap queue:work

Check out the Queue Service - Work Command section to learn more about the command.

To keep the queue:work process running permanently in the background, you should use a process monitor such as Supervisor to ensure that the queue worker does not stop running.

Alternatives

Alternatively, you may run the queue:work command using the App Schedule by using the Command Task running every minute:

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('* * * * *');

Failed Jobs

By default, failed jobs will be handled by the implemented \Tobento\App\Queue\LogFailedJobHandler::class. Any failed job will be repushed to the queue until it reaches the maximum retries. Once it has reached the maximum retries the job will not be queued anymore and will be sent into the app log.

On the App Logging Config file you may define a specific logger:

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

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

Custom Handler

You can change the default behavior by creating a custom failed job handler and using the app on method to replace the implemented handler:

use Tobento\Service\Queue\FailedJobHandlerInterface;

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

Credits

统计信息

  • 总下载量: 83
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-29