gpt4 book ai didi

带队列的 Laravel 监听器不执行

转载 作者:行者123 更新时间:2023-12-02 03:56:28 25 4
gpt4 key购买 nike

我正在尝试让监听器使用队列。一切都已正确设置以连接到 Redis 服务器。

事件

class BillingEvent extends BaseEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

private $event;
private $data;

public function __construct(Subscription $subscription, $event, $data = [])
{
parent::__construct($subscription);
$this->event = $event;
$this->data = $data;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}

public function getEvent()
{
return $this->event;
}

/**
* If we need to know additional data.
* @return array
*/
public function getData(): array
{
return $this->data;
}
}

监听器

class BillingEventListener implements ShouldQueue
{
use InteractsWithQueue;

public function handle(BillingEvent $event)
{
Log::error($event->getEvent()." test !!! ");
}

public function failed(BillingEvent $event, $exception)
{
//
}
}

这就是我触发事件的方式:

$sub = Subscription::find(1);
event(new BillingEvent($sub, LogEvents::BILLING_SUBSCRIPTION_CANCELLED));

均匀触发后,我查看我的 Redis 存储,看看是否保存了某些内容,并且确实保存了。

1) “队列:默认:通知”2)“队列:默认”

当我查看queues:default时,它有JSON。

{ "displayName": "App\Listeners\BillingEventListener", "job": "Illuminate\Queue\CallQueuedHandler@call", "maxTries": null, "timeout": null, "timeoutAt": null, "data": { "commandName": "Illuminate\Events\CallQueuedListener", "command": "O:36:\"Illuminate\Events\CallQueuedListener\":7:{s:5:\"class\";s:34:\"App\Listeners\BillingEventListener\";s:6:\"method\";s:6:\"handle\";s:4:\"data\";a:1:{i:0;O:23:\"App\Events\BillingEvent\":3:{s:30:\"\u0000App\Events\BillingEvent\u0000event\";s:30:\"billing_subscription_cancelled\";s:29:\"\u0000App\Events\BillingEvent\u0000data\";a:0:{}s:6:\"socket\";N;}}s:5:\"tries\";N;s:9:\"timeoutAt\";N;s:7:\"timeout\";N;s:6:\"\u0000*\u0000job\";N;}" }, "telescope_uuid": "8d6dcd7a-5747-41e5-84ec-082828c94ffa", "id": "hUmv4Pis9adXyBW5kLHoCAai18sFExBe", "attempts": 0 }

队列确实可以工作,但句柄函数中的代码永远不会被调用。当我将队列驱动程序设置为同步时,一切都会立即执行。

我在队列中获得了默认的 Redis 连接:

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

最佳答案

我刚刚找到了如何修复解决方案。我忘记运行 cron 作业来执行我的队列。

如果我们使用 redis,我们需要启动并运行这个 cron 作业:php artisan 队列:工作 redis

这适用于同步队列选项:php artisan 队列:工作

希望这有帮助

关于带队列的 Laravel 监听器不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55642826/

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