带有队列的Laravel侦听器无法执行

时间:2019-04-12 00:20:53

标签: laravel redis queue listener laravel-events

我正在尝试使侦听器使用队列。一切均已正确设置以连接到Redis服务器。

事件

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

监听器

class BillingEventListener implements ShouldQueue
{
    use InteractsWithQueue;

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

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

这是我触发事件的方式:

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

偶发火灾后,我看一下Redis存储器,看是否保存了某些东西。

1)“ queues:default:notify” 2)“队列:默认”

当我查看queues:default时,它具有JSON。

  

{       “ displayName”:“ App \ Listeners \ BillingEventListener”,       “ job”:“ Illuminate \ Queue \ CallQueuedHandler @ call”,       “ maxTries”:null,       “超时”:null,       “ timeoutAt”:null,       “数据”:{           “ commandName”:“ Illuminate \ Events \ CallQueuedListener”,           “ command”:“ O:36:\” Illuminate \ Events \ CallQueuedListener \“:7:{s:5:\” class \“; s:34:\” App \ Listeners \ BillingEventListener \“; s:6: \“方法\”; s:6:\“句柄\”; s:4:\“数据\”; 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”,       “尝试”:0   }

队列确实起作用,但是handle函数中的代码从未被调用。当我将队列驱动程序设置为同步时,一切都会立即执行。

我在队列中默认使用Redis连接:

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

1 个答案:

答案 0 :(得分:0)

我刚刚找到了解决问题的方法。我忘了执行cron作业来执行我的队列。

如果我们使用redis,则需要启动并运行此cron作业: php artisan queue:工作重做

这适用于同步队列选项: php artisan queue:work

希望这会有所帮助