Miljan Rakita
Miljan Rakita

Reputation: 1533

Laravel listeners with queue doesn't execute

I am trying to make listeners work with a queue. Everything is set up correctly to connect to a Redis server.

Event

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

Listener

class BillingEventListener implements ShouldQueue
{
    use InteractsWithQueue;

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

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

This is how I fire an event:

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

After the even fires, I take a look at my Redis storage to see if something is saved and it has.

1) "queues:default:notify" 2) "queues:default"

When I take a look at queues:default it has 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 }

The queue does work, but the code in handle function never gets called. When I set my queue driver to sync, everything executes straightaway.

I got default a Redis connection in queue:

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

Upvotes: 2

Views: 2837

Answers (1)

Miljan Rakita
Miljan Rakita

Reputation: 1533

I just found how to fix solution. I forgot to run a cron job to execute my queues.

If we use redis we need to have this cron job up and running: php artisan queue:work redis

This works for sync queue option: php artisan queue:work

Hope this helps

Upvotes: 4

Related Questions