Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 5.5 email notification not updating content

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class VerifyEmailNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $token;

    /**
    * Create a new notification instance.
    *
    * @return void
    */
    public function __construct($token)
    {
        $this->token = $token;
    }

    /**
    * Get the notification's delivery channels.
    *
    * @param  mixed  $notifiable
    * @return array
    */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
    * Get the mail representation of the notification.
    *
    * @param  mixed  $notifiable
    * @return \Illuminate\Notifications\Messages\MailMessage
    */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject(config('constants.title') . ' - Please Verify Your Email')
            ->line('You are receiving this email because you have sign up on ' . config('constants.title') . '.')
            ->action('Verify Email', url(config('app.url').route('verify_email', ['token' => $this->token], false)))
            ->line('If you did not sign up on ' . config('constants.title') . ', no further action is required.');
    }

    /**
    * Get the array representation of the notification.
    *
    * @param  mixed  $notifiable
    * @return array
    */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

I am using laravel 5.5 email notification. I have changed this mail notification, but somewhere it has been cached. My Application is sending me mail with old content, not with the current code snippet that i have shared here. I am using supervisor to monitor queue processes.

I have also cleared the view cache by running below command but it does work

php artisan view:clear

I have also restarted the queue

php artisan queue:restart

I have also ran

php artisan config:cache

but nothing seems to work for me.

Is this issue can be related to supervisor?

like image 849
faizan.sh Avatar asked Aug 17 '18 12:08

faizan.sh


1 Answers

This issue is not related to cache at all. When you run the queue worker all the notifications classes will be loaded once.

Any changes happen to these classes will not take effect as the worker already loaded the old classes.

You can read this at Laravel documentation:

Running Worker Section:

Remember, queue workers are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers.

Queue Workers & Deployment Section:

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command.

So, to have your notification content updated, you have to kill all queue workers running and restart them.

This suggested solution (since you are using Supervisor) to restart Supervisor will work perfectly for you.

supervisorctl restart all

But, I do not recommend doing that as restarting Supervisor will hard-killing your queue workers and the current processed job will be lost!

Edit: Using Supervisor restart command is safe for Laravel 5.4+ but, make sure that to set "stopwaitsecs" (in your supervisor config file of the worker) to a value higher than the estimated job processing time.

That is why the artisan command to restart queue is exists:

php artisan queue:restart

You should be using this command to kill the queue workers and the Supervisor will get them started again for you.

But, keep in mind that this command will take some time until it takes effect as it will broadcast a restart signal to all the queue workers running and the queue workers will only capture the signal once it finishes processing their current job. That what is called graceful-killing.

To get this artisan command work, be sure to setup a proper cache driver for Laravel as the restart signal is broadcasted via cache.

like image 160
Mohamed El-Refaie Avatar answered Nov 17 '22 16:11

Mohamed El-Refaie