基于Laravel事件文档,我印象中可以通过在侦听器上添加“ implements 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秒钟,因此不再发生。
但是Jobs表从未在其中行,而当我运行队列工作器时,什么也没有发生,因此该作业无法执行。
我也曾尝试将config / queue.php从Laravel复制到我的lumen应用程序中,但这似乎没有什么区别。当它在同步驱动程序中并且不使用ShouldQueue时,该工作确实曾经发生过。
所以我最终找到了问题。归结为Lumen / Laravel手册中没有的细节。
您需要将此添加到具有服务提供商注册的部分的Lumen引导程序中:
$app->configure('queue');
$app->register(Illuminate\Queue\QueueServiceProvider::class);