Lavel 5.x与Redis Queues生成大量日志

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lavel 5.x与Redis Queues生成大量日志相关的知识,希望对你有一定的参考价值。

我有一个与Laravel和Redis Queues一起设置的项目。应用程序配置为在收到消息时发送推送通知。收到消息后,它会立即存储在数据库中并生成推送:

dispatch(new CreateMessageNotification($message));

Project正在使用Redis队列和服务提供者。问题是如果我们不执行以下命令,队列调度不起作用:

php artisan queue:work redis --daemon

但是,当我们运行此命令时,应用程序开始生成下面的错误日志。似乎某些例程被卡在某处,我们无法弄清楚在哪里,为什么以及如何?如果我们不运行此脚本,则推送通知不起作用。如果我们确实运行此脚本,推送通知将延迟工作,并生成错误日志,并且日志大小在一小时内达到GB。我们在这做错了什么?

我们正在使用Redis / Predis:

'redis' => [
    'client' => 'predis',
    'cluster' => env('REDIS_CLUSTER', false),
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
        'timeout' => 15.0,
    ],
],

对于广播,我们使用Pusher:

'default' => env('BROADCAST_DRIVER', 'pusher'),

对于队列,我们​​使用的是Redis:

'default' => env('QUEUE_DRIVER', 'sync'),

QUEUE_DRIVER=redis

对于缓存,我们使用的是Redis:

'default' => env('CACHE_DRIVER', 'file'),

CACHE_DRIVER=redis

CreateMessageNotification

<?php
namespace AppJobsEventHandlers;

use AppEventsNotificationsNewMessageCreatedEvent;
use AppRepositoriesUserDevicesRepository;
use Event;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;

class CreateMessageNotification implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    private $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function handle()
    {
        Event::fire(new NewMessageCreatedEvent($this->message));
    }
}

NewMessageCreatedEvent

<?php
namespace AppEventsNotifications;

use AppModelsChatMessage;
use Event;
use IlluminateQueueSerializesModels;

class NewMessageCreatedEvent extends Event
{
    use SerializesModels;

    public function __construct(Message $message)
    {
        $message->load(['other', 'chat', 'chat.chatUsers']);

        foreach ($message->chat->chatUsers as $chatUser) {
            if ($chatUser->user_id !== $message->other->id) {
                Event::fire(new NewMessageNotificationEvent($message, $chatUser->user_id));
            }
        }
    }
}

错误日志

[2018-09-06 06:28:55] local.ERROR: exception 'IlluminateBroadcastingBroadcastException' with message 'API call failed since (daily) message quota exceeded
' in /var/www/html/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:106
Stack trace:
#0 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php(49): IlluminateBroadcastingBroadcastersPusherBroadcaster->broadcast(Array, 'new_$
#1 [internal function]: IlluminateBroadcastingBroadcastEvent->handle(Object(IlluminateBroadcastingBroadcastersPusherBroadcaster))
#2 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(30): call_user_func_array(Array, Array)
#3 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#4 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationAppli$
#5 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(539): IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), Arr$
#6 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): IlluminateContainerContainer->call(Array)
#7 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): IlluminateBusDispatcher->IlluminateBus{closure}(Object(IlluminateBroadcastingBroa$
#8 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateBroadcast$
#9 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): IlluminatePipelinePipeline->then(Object(Closure))
#10 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(43): IlluminateBusDispatcher->dispatchNow(Object(IlluminateBroadcastingBroadcastEv$
#11 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(69): IlluminateQueueCallQueuedHandler->call(Object(IlluminateQueueJobsRedisJob), Array)
#12 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(317): IlluminateQueueJobsJob->fire()
#13 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(267): IlluminateQueueWorker->process('redis', Object(IlluminateQueueJobsRedisJob), Object(Il$
#14 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(113): IlluminateQueueWorker->runJob(Object(IlluminateQueueJobsRedisJob), 'redis', Object(Ill$
#15 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): IlluminateQueueWorker->daemon('redis', 'default', Object(IlluminateQueueWo$
#16 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): IlluminateQueueConsoleWorkCommand->runWorker('redis', 'default')
#17 [internal function]: IlluminateQueueConsoleWorkCommand->fire()
#18 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(30): call_user_func_array(Array, Array)
#19 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#20 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationAppl$
#21 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(539): IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), Ar$
#22 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): IlluminateContainerContainer->call(Array)
#23 /var/www/html/project/vendor/symfony/console/Command/Command.php(252): IlluminateConsoleCommand->execute(Object(SymfonyComponentConsoleInputArgvInput), Object(Illuminate$
#24 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(168): SymfonyComponentConsoleCommandCommand->run(Object(SymfonyComponentConsoleInputAr$
#25 /var/www/html/project/vendor/symfony/console/Application.php(946): IlluminateConsoleCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentC$
#26 /var/www/html/project/vendor/symfony/console/Application.php(248): SymfonyComponentConsoleApplication->doRunCommand(Object(IlluminateQueueConsoleWorkCommand), Object(Symf$
#27 /var/www/html/project/vendor/symfony/console/Application.php(148): SymfonyComponentConsoleApplication->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(Symfon$
#28 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): SymfonyComponentConsoleApplication->run(Object(SymfonyComponentConsoleIn$
#29 /var/www/html/project/artisan(36): IlluminateFoundationConsoleKernel->handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConso$
#30 {main}

[2018-09-06 06:28:55] local.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in /var/www/html/project/app/Events/Notifications/NewMessageCreate$
Stack trace:
#0 /var/www/html/project/app/Events/Notifications/NewMessageCreatedEvent.php(27): IlluminateFoundationBootstrapHandleExceptions->handleError(8, 'Trying to get p...', '/var/www/h$
#1 /var/www/html/project/app/Jobs/EventHandlers/CreateMessageNotification.php(38): AppEventsNotificationsNewMessageCreatedEvent->__construct(Object(AppModelsChatMessage))
#2 [internal function]: AppJobsEventHandlersCreateMessageNotification->handle()
#3 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(30): call_user_func_array(Array, Array)
#4 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#5 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationAppli$
#6 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(539): IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), Arr$
#7 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(94): IlluminateContainerContainer->call(Array)
#8 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): IlluminateBusDispatcher->IlluminateBus{closure}(Object(AppJobsEventHandlersCreat$
#9 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(AppJobsEventHandle$
#10 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(98): IlluminatePipelinePipeline->then(Object(Closure))
#11 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(43): IlluminateBusDispatcher->dispatchNow(Object(AppJobsEventHandlersCreateMessag$
#12 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(69): IlluminateQueueCallQueuedHandler->call(Object(IlluminateQueueJobsRedisJob), Array)
#13 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(317): IlluminateQueueJobsJob->fire()
#14 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(267): IlluminateQueueWorker->process('redis', Object(IlluminateQueueJobsRedisJob), Object(Il$
#15 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(113): IlluminateQueueWorker->runJob(Object(IlluminateQueueJobsRedisJob), 'redis', Object(Ill$
#16 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(102): IlluminateQueueWorker->daemon('redis', 'default', Object(IlluminateQueueWo$
#17 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(86): IlluminateQueueConsoleWorkCommand->runWorker('redis', 'default')
#18 [internal function]: IlluminateQueueConsoleWorkCommand->fire()
#19 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(30): call_user_func_array(Array, Array)
#20 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#21 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): IlluminateContainerBoundMethod::callBoundMethod(Object(IlluminateFoundationAppl$
#22 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(539): IlluminateContainerBoundMethod::call(Object(IlluminateFoundationApplication), Ar$
#23 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): IlluminateContainerContainer->call(Array)
#24 /var/www/html/project/vendor/symfony/console/Command/Command.php(252): IlluminateConsoleCommand->execute(Object(SymfonyComponentConsoleInputArgvInput), Object(Illuminate$
#25 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(168): SymfonyComponentConsoleCommandCommand->run(Object(SymfonyComponentConsoleInputAr$
#26 /var/www/html/project/vendor/symfony/console/Application.php(946): IlluminateConsoleCommand->run(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentC$
#27 /var/www/html/project/vendor/symfony/console/Application.php(248): SymfonyComponentConsoleApplication->doRunCommand(Object(IlluminateQueueConsoleWorkCommand), Object(Symf$
#28 /var/www/html/project/vendor/symfony/console/Application.php(148): SymfonyComponentConsoleApplication->doRun(Object(SymfonyComponentConsoleInputArgvInput), Object(Symfon$
#29 /var/www/html/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): SymfonyComponentConsoleApplication->run(Object(SymfonyComponentConsoleIn$
#30 /var/www/html/project/artisan(36): IlluminateFoundationConsoleKernel->handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConso$
答案

对我有用的是从队列中删除所有失败的作业(在尝试再重试一次之后):

php artisan queue:restart
php artisan queue:work redis --daemon --tries=1

重新运行队列:

php artisan queue:work redis --daemon

此外,我不得不摆脱对Pusher的调用,这些调用未能使其发挥作用。

以上是关于Lavel 5.x与Redis Queues生成大量日志的主要内容,如果未能解决你的问题,请参考以下文章

Redis消息队列(Message queues)

微信支付lavel是啥?

redis缓存数据库简单使用

Rabbitmq_02 Work Queues

Redis异构集群数据实时迁移

Work Queues(点对多)