首页 > 解决方案 > Lumen Database Queued Event Listener 事件消失

问题描述

根据 Laravel 事件文档,我的印象是我可以通过在侦听器上添加“实现 ShouldQueue”来异步创建事件队列。

namespace App\Listeners;

use Log;
use App\Events\MeetEntryConfirmationEvent;
use App\Mail\MeetEntryConfirmation;
use Illuminate\Contracts\Queue\ShouldQueue;

class MeetEntryConfirmationListener implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  MeetEntryConfirmationEvent  $event
     * @return void
     */
    public function handle(MeetEntryConfirmationEvent $entryEvent)
    {
        Log::debug('Attempt to send MeetEntryConfirmationEvent');
        // Do time consuming stuff
    }
}

我的事件是这样实现的:

class MeetEntryConfirmationEvent extends Event
{
    use SerializesModels;

    public $entry;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(MeetEntry $entry)
    {
        $this->entry = $entry;
        Log::debug('Created MeetEntryConfirmationEvent');
    }
}

我正在像这样派遣他们。

event(new MeetEntryConfirmationEvent($entry));

我已将 QUEUE_DRIVER 设置为数据库,作业表就在那里。当事件被调度时,它似乎工作正常,因为它立即返回。当我让它处于同步模式时,这项工作需要 8 秒,所以不再发生这种情况。

但是,作业表永远不会在其中获得一行,并且当我运行队列工作者时,没有任何反应,作业不会执行。

我也尝试将 config/queue.php 从 Laravel 复制到我的 lumen 应用程序中,但这似乎没有什么不同。当它在同步驱动程序中并且当我不使用 ShouldQueue 时,确实会发生这项工作。

标签: laravellumen

解决方案


所以我最终发现了问题。它归结为 Lumen/Laravel 手册中没有的细节。

您需要将其添加到服务提供者注册部分的 Lumen 引导程序中:

$app->configure('queue');
$app->register(Illuminate\Queue\QueueServiceProvider::class);

推荐阅读