Search code examples
laravellumen

Lumen Database Queued Event Listener event disappears


Based on the Laravel event documentation I was under the impression that I can make an Event queue asynchronously by adding "implements ShouldQueue" on the listener.

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
    }
}

My event is implemented thusly:

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');
    }
}

And I'm dispatching them like this.

event(new MeetEntryConfirmationEvent($entry));

I've set QUEUE_DRIVER to database, the jobs table is there. When the event is dispatched it seems to be working correctly because it returns immediately. When I had it in sync mode the job would take 8 seconds, so that's no longer happening.

However the jobs table never gets a row in it, and when I run the queue worker nothing happens, the job doesn't execute.

I've also tried copying in the config/queue.php from Laravel into my lumen app, but that doesn't seem to make a difference. The job did used to happen when it was in sync driver and when I wasn't using ShouldQueue.


Solution

  • So I ultimately found the problem. It comes down to details that aren't in the Lumen/Laravel manuals.

    You need to add this to the Lumen bootstrap in the section with service provider registrations:

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