【问题标题】:Server was running out of memory by using laravel queue服务器使用 laravel 队列内存不足
【发布时间】:2018-12-18 17:54:17
【问题描述】:

我正在为我的应用程序使用 laravel lumen 5.5 api。我创建了用于为用户发送通知的作业队列。此通知可以从管理员随时发送,并且 4 或 5 个通知管理员将创建并发布给用户。 我有这个工作的以下代码,

//Creating queue job logic in my API Controller
$message = $request->input('message');
$notifyrequest = new NotificationJob();
$notifyrequest->setmessage($message, $request->input('title'));
dispatch($notifyrequest);
return array('error' => false, 'message' => 'Notification sent successfully.');

//Notificaiton Job logic in job folder class
class NotificationJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
    protected $message;
    protected $title;
    /**
     */
    public function __construct()
    {}
    public function setmessage($message, $title){
        $this->message = $message;
        $this->title = $title;
    }
    public function handle()
    {
        Log::info("Request Push Notificaiton Queues Handle".$this->message, ['title' => $this->title]);
        $Onesignalnotification = new Onesignalnotification();
        $onesignalresponse = $Onesignalnotification->sendNotification($this->message, $this->title);
        Log::info("Notification has been sent", ['Response' => $onesignalresponse]);
    }
}

//Sending notifications through one signal. Using filter for active users to receive notification. Currently 10K+ users are there.
public function sendNotification($message, $title)
    {
     $content      = array(
        "en" => $message
    );
    $hashes_array = array();

    $fields = array(
        'app_id' => $this->APP_ID,
        'included_segments' => array('Active Users'),
        'data' => array(
            "nav" => "1"
        ),
        'contents' => $content,
        'web_buttons' => $hashes_array
    );

    $fields = json_encode($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic apikey'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    $response = curl_exec($ch);
    curl_close($ch);
    $return["allresponses"] = $response;
    $return = json_encode($return);

    $data = json_decode($return, true);
    return $data;
    }

我已将主管用于后台进程,我的主管程序看起来像,

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=4
redirect_stderr=true
stdout_logfile=/tmp/supervisor_worker.log

也尝试了如下方式

1.command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --daemon
2.command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --once --daemon
3.command=php /var/www/html/artisan queue:listen redis --sleep=3 --tries=3
4.command=php /var/www/html/artisan queue:listen redis --sleep=3 --tries=3 --daemon

但内存仍然存在问题。它是第一次发送通知,如果下一次更新意味着在挂起之后发送通知,则会产生内存泄漏。我的服务器进入无响应状态。

【问题讨论】:

  • 您是否尝试将MemoryUsageProcessor 添加到 Monolog 实例以检查内存使用情况? seldaek.github.io/monolog/doc/…你知道它在哪里耗尽了内存吗?
  • 您的服务器规格详细信息是什么。多少 GB 的内存和 CPU 的数量?也可以添加laravel和supervisord的错误日志吗?
  • CentOS 7 64 位 2 GB 内存。在主管和 laravel 中,我找不到任何关于此的错误。在显示 http 的服务器不再响应之后,我的队列被执行。如果我重新启动我的服务器意味着我的域开始工作。如果否则我的域不起作用。
  • 对此有何帮助或想法?

标签: laravel laravel-5 queue lumen


【解决方案1】:

使用 gc_collect_cycles 或者让你的进程定期重启(crontab)。它会释放你的记忆

【讨论】:

    猜你喜欢
    • 2018-04-07
    • 1970-01-01
    • 2014-07-02
    • 2017-07-20
    • 2012-05-06
    • 2012-07-05
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多