Home » Php » php – Laravel event listener doesn't work when implementing ShouldQueue interface

php – Laravel event listener doesn't work when implementing ShouldQueue interface

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to change my event listeners to work with queue like so:

namespace App\Listeners;

use App\Callback;
use App\Events\TestEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    /**
     * Handle the event.
     *
     * @param  TestEvent  $event
     * @return void
     */
    public function handle(TestEvent $event)
    {
        sleep(5);
        $callback = new Callback([
            "full_name" => "Redis test " . date("h:m:s")
        ]);

        $callback->save();

//        echo "Here";
    }
}

I’ve installed Redis locally for testing.

When testing this has appeared in redis-cli console:
Redis CLI console

After this first time event listener refused to work with queue. On deleting ShouldQueue interface everything works ok!

Con you please help me to find out the problem that Queue don’t work?

How to&Answers: