我需要在Laravel 5.8中运行一个非常耗时的异步任务。这是.env文件
...
QUEUE_CONNECTION=sync
QUEUE_DRIVER=redis
...队列驱动程序必须是Redis,因为网站使用带有redis和socket.io的Laravel-Echo来广播消息,而我不能将队列驱动程序更改为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);
...
}我还运行以下artisan命令
php artisan queue:work
php artisan queue:listen当我访问HomeController的/index时,我希望立即看到dd(1),而不是在30秒之后,因为sleep(30)必须在队列中运行,但这并没有发生,我必须等待30秒才能看到dd(1)。如何在后台异步运行作业?
提前谢谢。
发布于 2020-07-28 23:28:54
尝试将QUEUE_CONNECTION切换到redis而不是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,
],
],https://stackoverflow.com/questions/63137423
复制相似问题