gpt4 book ai didi

laravel - 如何使用 Redis 在 Laravel 5.8 中调度异步作业?

转载 作者:行者123 更新时间:2023-12-03 06:37:53 24 4
gpt4 key购买 nike

我需要在 Laravel 5.8 中异步运行一个非常耗时的任务。这是.env文件

...
QUEUE_CONNECTION=sync
QUEUE_DRIVER=redis
...
队列驱动必须是Redis,因为网站使用 Laravel-Echoredissocket.io广播消息,但我无法将队列驱动程序更改为 database .
这是我创建的工作
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class BroadcastRepeatJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
sleep(30);
}
}
这是 HomeController :
public function index()
{
BroadcastRepeatJob::dispatch()->onQueue("default");
dd(1);
...
}
我还运行以下工匠命令
php artisan queue:work
php artisan queue:listen
当我访问 /indexHomeController我希望看到 dd(1)不紧随其后 30秒因为 sleep(30)必须在队列中运行,但这不会发生,我必须等待 30 秒才能看到 dd(1) .如何在后台异步运行作业?
提前致谢。

最佳答案

尝试切换您的 QUEUE_CONNECTIONredis而不是 sync

 /*
|--------------------------------------------------------------------------
| Queue Connections
|--------------------------------------------------------------------------
|
| Here you may configure the connection information for each server that
| is used by your application. A default configuration has been added
| for each back-end shipped with Laravel. You are free to add more.
|
| Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null"
|
*/

'connections' => [

'sync' => [
'driver' => 'sync',
],

'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],

'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],

],

关于laravel - 如何使用 Redis 在 Laravel 5.8 中调度异步作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63137423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com