mardi 10 juillet 2018

Server was running out of memory by using laravel queue

I'm using laravel lumen 5.5 api for my application. I have created job queue for sending notification for users. This notification can send from admin for any time and 4 or 5 notification admin will create and post to users. I'm having the below code for this job,

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

I have used supervisor for background process and my supervisor program looks like,

[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

And also tried as like below way

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

But still having an issue on memory. It is sending notification first time , if next post update means sending notification after that getting hang it produce memory leak. My server went to unresponse state.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire