首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有队列的Laravel侦听器不执行

带有队列的Laravel侦听器不执行
EN

Stack Overflow用户
提问于 2019-04-12 08:20:53
回答 1查看 1.6K关注 0票数 2

我正在尝试让监听器使用队列工作。一切都已正确设置,可以连接到Redis服务器。

事件

代码语言:javascript
复制
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;
    }
}

侦听器

代码语言:javascript
复制
class BillingEventListener implements ShouldQueue
{
    use InteractsWithQueue;

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

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

这就是我触发事件的方式:

代码语言:javascript
复制
$sub = Subscription::find(1);
event(new BillingEvent($sub, LogEvents::BILLING_SUBSCRIPTION_CANCELLED));

在even fires之后,我查看了一下我的Redis存储,看看是否有东西被保存了。

1) "queues:default:notify“队列:default”

当我看一下queues:default时,它有JSON。

{ "displayName":"App\Listeners\BillingEventListener","job":“照明\队列\呼叫队列处理程序@call”,"maxTries":null,“超时”:null,"timeoutAt":null,"data":{ "commandName":“照明\事件\呼叫队列侦听器”,"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",”尝试“:0}

队列可以工作,但handle函数中的代码永远不会被调用。当我将队列驱动程序设置为同步时,一切都会立即执行。

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

代码语言:javascript
复制
'redis' => [
    'driver' => 'redis',
    'connection' => 'default',
    'queue' => env('REDIS_QUEUE', 'default'),
    'retry_after' => 90,
    'block_for' => null,
]
EN

回答 1

Stack Overflow用户

发布于 2019-04-12 08:52:44

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

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

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

希望这能有所帮助

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55642826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档