Hyperf redis 非同步佇列使用

CFFIRM發表於2024-06-23

config/autoload/processes.php

use App\Book\Process\CreateQrcodeComsumer;

return [
    Mine\Crontab\MineCrontabProcess::class,
    Hyperf\AsyncQueue\Process\ConsumerProcess::class,
    CreateQrcodeComsumer::class
   //增加自定義的非同步佇列類    
];

由於hyperf只使用自己default的佇列 ,所以要重新建立一個

<?php

namespace App\Book\Process;

use Hyperf\AsyncQueue\Process\ConsumerProcess;
use Hyperf\Process\Annotation\Process;

#[Process(name: "createQrcode")]
class CreateQrcodeComsumer extends ConsumerProcess
{
    protected string $queue = 'createQrcode';
}

然後在config/autoload/async_queue.php

<?php

use App\Book\Task\ExportDataJob;

return [
    'default' => [
        'driver' => Hyperf\AsyncQueue\Driver\RedisDriver::class,
        'channel' => 'default',
        'timeout' => 2,
        'retry_seconds' => [5, 10, 20],
        'handle_timeout' => 10,
        'processes' => 1,
        'concurrent' => [
            'limit' => [
                'default' => 10,
            ],
        ],
        'redis' => [
            'pool' => 'default',
            'queue' => '{default}',
        ],
        'processors' => [
            // 在這裡新增您的任務處理器類名
            // App\Book\Task\ExportDataJob::class,
            ExportDataJob::class
        ],
    ],
    'createQrcode' => [
        'driver' => Hyperf\AsyncQueue\Driver\RedisDriver::class,
        'channel' => 'createQrcode',
        'timeout' => 2,
        'retry_seconds' => [5, 10, 20],
        'handle_timeout' => 10,
        'processes' => 1,
        'concurrent' => [
            'limit' => [
                'default' => 10,
            ],
        ],
        'redis' => [
            'pool' => 'default',
            'queue' => '{default}',
        ],
        'processors' => [
            // 在這裡新增您的任務處理器類名
        ],
    ],
];

相關文章