2

我试图让听众使用队列。一切都已正确设置以连接到 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)“队列:默认:通知” 2)“队列:默认”

当我查看 queues:default 时,它具有 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 }

队列确实有效,但句柄函数中的代码永远不会被调用。当我将队列驱动程序设置为同步时,一切都会立即执行。

我在队列中获得了默认的 Redis 连接:

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

1 回答 1

4

我刚刚找到了解决方案。我忘了运行一个 cron 作业来执行我的队列。

如果我们使用 redis,我们需要启动并运行这个 cron 作业: php artisan queue:work redis

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

希望这可以帮助

于 2019-04-12T00:52:44.043 回答