Laravel Supervisor start stop restart and update database queue

Today, We want to share with you Laravel Supervisor.In this post we will show you supervisor setup & configuration, hear for check if queue is running we will give you demo and example for implement.In this post, we will learn about How To Configure Supervisor For Laravel 5/6/7 Queue Jobs? with an example.

Laravel Queue worker and Supervisor process monitor

In this tutorial we learn to all about Laravel Supervisor Examples like as a Laravel Installing and setup Supervisor, start, stop, restart and update on database queue or many more.

How to setup laravel queue with database?

# add database migration tables
$ php artisan queue:table

# run the migration
$ php artisan migrate​

Installing Supervisor on Linux

sudo apt-get install supervisor

How to setup supervisor?

here step by step Supervisor configuration for Laravel

# install supervisor
$ sudo apt-get install supervisor

# go to supervisor config directory
cd /etc/supervisor/conf.d

# create a new laravel configuration
$ sudo nano laravel-worker.conf

# paste following some contents to our file
# save changes using Ctrl + X + Y + Enter
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/domain-name.com/artisan queue:work sqs --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/domain-name.com/worker.log​

numprocs=8 will run 8 worker process.

Read Also:  php send email with attachment example

Edit .conf File in Live Server

[[email protected] supervisord.d]# nano laravel-worker.conf

supervisor reread, update and check status

here supervisor run following command to initialize new configurations:

# read the new config
$ sudo supervisorctl reread

# activate our configuration
$ sudo supervisorctl update

# start queue command
$ sudo supervisorctl start laravel-worker:*

# check the status of our new config
$ sudo supervisorctl status​

Additional Supervisor Command

START THE PROCESSES IN SUPERVISOR

After Code changes Last Process

[[email protected] supervisord.d]# sudo supervisorctl start
[[email protected] supervisord.d]# sudo supervisorctl reread
[[email protected] supervisord.d]# sudo supervisorctl update
[[email protected] supervisord.d]# sudo supervisorctl restart laravel-worker:*

//or

$ supervisorctl start all
$ supervisorctl start job_worker

STOP THE PROCESSES IN SUPERVISOR

[[email protected] supervisord.d]# sudo supervisorctl stop

UPDATE THE PROCESSES IN SUPERVISOR

[[email protected] supervisord.d]# sudo supervisorctl update

RESTART THE PROCESSES IN SUPERVISOR

[[email protected] supervisord.d]# sudo supervisorctl reread
[[email protected] supervisord.d]# sudo supervisorctl update
[[email protected] supervisord.d]# sudo supervisorctl restart laravel-worker:*

Starting Supervisor

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-queue-worker:*

How to create queue in laravel?

# create a new job

$ php artisan make:job autoPostArticle

app/Jobs/autoPostArticle.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class autoPostArticle implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        // define any class variables here
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // your logic for notifying subscriber goes here
    }
}

update our article controller

<?php

namespacenamespac  App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;
use App\Jobs\SendReminderEmail;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * Save article and notify subscribers
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function sendReminderEmail(Request $request, $id)
    {
        // find or create fresh article
        $article = Article::firstOrNew($id);

        // your logic to save article goes here

        // pass article object to our job
        // our article class has info regarding fresh article
        $this->dispatch( (new autoPostArticle($article))->onQueue('emails') );
    }
}

update our job class

Read Also:  Laravel 6 Traits Example Tutorial - The PHP Framework

<?php

namespace App\Jobs;

use App\Article;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class autoPostArticle implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $article;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // your logic for notifying subscriber goes here
        foreach($this->article->subscribers() as $user) {
           // send them an email
        } 
    }
}

Last Change

QUEUE_DRIVER=database​

I hope you get an idea about Laravel Supervisor.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Get Table name From Model using Laravel 5.7 Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Top 10 Great Cloud Storage File Sharing Services
  2. Read Also:  jQuery Accordion Vertical Menu using CSS
  3. Read Also:  Go Program to Print Pyramid Pattern using numbers
  4. Read Also:  How to Change User Password in Laravel 6
  5. Read Also:  Laravel Delete Queued Jobs
  6. Read Also:  Auto Post On LinkedIn Using PHP script
  7. Read Also:  how to insert multiple image in database using codeigniter?
  8. Read Also:  PHP wp_get_attachment_image_url wordpress function
  9. Read Also:  Queue Job in Laravel 5.8 Tutorial
  10. Read Also:  Laravel 6 Clear Queued Jobs Example