Search code examples
laravelqueuelive

How do I enable queue:work from live server?


This one is working fine on my local server as I can run the command.

php artisan queue:work

But how I can implement this on the live server? I am really stuck on that problem. But as I want to send the notification through mail. So, I am following to use that laravel default queue feature as it will make no delay and automatically do it's job. So, if you can help really appreciate that.

Advance Thanks..

//my .env configuration

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120

my jobs table from database

//my NewScholarship Class

namespace App\Notifications;

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

class NewScholarship extends Notification implements ShouldQueue
{
  use Queueable;



public $scholarship;


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

/**
 * 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)
                ->greeting('Hello, Subscriber!')
                ->subject('New Scholarship has been published')
                ->line('New Scholarship info has been published to our website')
                ->line('Titled as: '.$this->scholarship->title)
                ->line('To have a look click the button below')
                ->action('View details', url('s/details',$this->scholarship->slug))
                ->line('Thank you for your subscription!');
}

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

Solution

  • The idea on production is same as development you should run queue:work but of course you can't ssh to your server every now and then and check if the command is still running, that is why you should use something like supervisor as recommended by Laravel.

    Here is a quick introduction to supervisor in the Laravel documentation Basically, if your server is debian, you should install the supervisor package by running:

    sudo apt-get install supervisor after you ssh to your server

    After the installation is complete, use vi vim or nano to access /etc/supervisor/conf.d and create a configuration file ex: laravel-worker.conf

    Below is a sample configuration

    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3 #"home/forge/app.com/artisan" is the path to your project root
    autostart=true
    autorestart=true
    user=forge
    numprocs=8
    redirect_stderr=true
    stdout_logfile=/home/forge/app.com/worker.log
    stopwaitsecs=3600
    

    Notes from the laravel documentation:

    • You should ensure that the value of stopwaitsecs is greater than the number of seconds consumed by your longest running job. Otherwise, Supervisor may kill the job before it is finished processing.
    • In this example, the numprocs directive will instruct Supervisor to run 8 queue:work processes and monitor all of them, automatically restarting them if they fail.
    • You should change the queue:work sqs portion of the command directive to reflect your desired queue connection.

    Note that this information is from the laravel documentation linked above

    If your server is not debian please see the official documentation of suppervisor

    Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

    sudo supervisorctl reread

    sudo supervisorctl update

    sudo supervisorctl start laravel-worker:*