Laravel Supervisor Setup with Example Tutorial

10-Apr-2023

.

Admin

Laravel Supervisor Setup with Example Tutorial

Hello Friends,

Do you require background task processing for the Laravel 9 project? Typically, large-scale operations that require a significant task, such sending emails or uploading large files, may take a long time to complete. And you don't want your user to wait till the task has been completed successfully. Because of this, Laravel has a queueing approach that allows you to use Laravel Supervisor to process your task in the background. The user is then simply informed that your task has been processed correctly.

In this tutorial, you will be able to learn how to set up the Laravel 9 supervisor and Laravel queue with the database.

Step 1: Install Laravel


This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Supervisor Setup

With the Laravel queue connection, we have a wide range of alternatives, including redis, amazon sqs, and even databases. However, we are utilising the database in this instance.

To start kindly run the following command

// to generate jobs table in migrations

php artisan queue:table

// then run migrate after the jobs migrations generated

php artisan migrate

Step 3: Setup Laravel Supervisor

To configure the supervisor you need to open your Linux terminal window and run the following command:

//update dependencies and install supervisor

sudo apt update && sudo apt install supervisor

//check supervisor status after the installation

sudo systemctl status supervisor

Output

As we can see our supervisor is now active. So let's configure our Laravel project file for our supervisor worker.

# then navigate supervisor config folder

cd /etc/supervisor/conf.d

# then create Laravel project configuration file

sudo nano project-name-worker.conf

Then paste the below code:

[program:project-name-worker]

process_name=%(program_name)s_%(process_num)02d

command=php /var/www/project-name.com/public_html/artisan queue:work --sleep=3 --tries=3

autostart=true

autorestart=true

user=ubuntu

numprocs=8

redirect_stderr=true

stdout_logfile=/var/www/project-name.com/public_html/storage/logs/worker.log

stopwaitsecs=3600

NOTES

1.Be sure that you input your exact project directory for replacing /var/www/project-name.com/public_html

2.user value must be your logged user name. In my case is ubuntu.

3.and also the stdout_logfile change it to your project directory.

Let's run the following command

//to read the new supervisor configurations

sudo supervisorctl reread

//then activate the new configuration

sudo supervisorctl update

//to start the queue command

//take note that this is the name of our config file above.

//You can change it depending on your project name.

sudo supervisorctl start project-name-worker:*

//then check status if configured correctly

sudo supervisorctl status

Another method is this

// it will stop all queue

sudo supervisorctl stop all

// then read new configuration

sudo supervisorctl reread

// then update so that it will activate then newly added

sudo supervisorctl update

// then start all queues

sudo supervisorctl start all

// then check status

sudo supervisorctl status

Step 4: Setup Laravel Queue

So in this example, let's say you have user management and the admin can invite users via email. So let's implement the Laravel queue on this so that the user invitation will run in the background.

Then run the following command

php artisan make:job UserInvitationJob

App/Jobs/UserInvitationJob.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldBeUnique;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Queue\SerializesModels;

class UserInvitationJob implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**

* Create a new job instance.

*

* @return void

*/

public function __construct()

{

//

}

/**

* Execute the job.

*

* @return void

*/

public function handle()

{

//

}

}

Now we have our UserInvitationJob class for our Laravel queue user invitation. Next, we will update user UsersController.php for our invitation or create a new user and then send the invitation.

App/Http/Controllers/UsersController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;

use Illuminate\Http\Request;

use App\Http\Requests\StoreUserRequest;

class UsersController extends Controller

{

/**

* Store a newly created user

*

* @param User $user

* @param StoreUserRequest $request

*

* @return \Illuminate\Http\Response

*/

public function store(User $user, StoreUserRequest $request)

{

//For demo purposes only. When creating user or inviting a user

// you should create a generated random password and email it to the user

$user = $user->create(array_merge($request->validated(), [

'password' => 'test'

]));

dispatch(new \App\Jobs\UserInvitationJob($user));

return redirect()->route('users.index')

->withSuccess(__('User created successfully.'));

}

}

As you can see we added dispatch(new \App\Jobs\UserInvitationJob($user)); code which is we use to invite user and implement it with Laravel queue.

Then in our UserInvitationJob class, we will update the code to send the email invitation and run it through the background with the Laravel supervisor.

App/Jobs/UserInvitationJob

<?php

namespace App\Jobs;

use App\Models\User;

use Illuminate\Bus\Queueable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Contracts\Queue\ShouldBeUnique;

class UserInvitationJob implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $user;

/**

* Create a new job instance.

*

* @return void

*/

public function __construct(User $user)

{

$this->user = $user;

}

/**

* Execute the job.

*

* @return void

*/

public function handle()

{

$userDetails = $this->user;

//send email invitation here

}

}

After all, our Laravel supervisor and Laravel queue are ready. You can integrate it with your own functionalities. But before we do that don't forget to update the ENV config for the QUEUE_CONNECTION value.

QUEUE_CONNECTION=database

I hope it can help you...

#Laravel