DEV Community

loading...

How to Set up Job Queue in Laravel?

pakainfocom profile image pakainfo ・4 min read

There are several steps involved in setup the job queue with supervisor.

Step 1: Queue Configuration

The queue configuration file is stored in config/queue.php. In this file you will find connection configurations for each of the queue drivers that are included with the framework, which includes a database, Beanstalkd, Amazon SQS, Redis, and a synchronous driver that will execute jobs immediately (for local use). A null queue driver is also included which discards queued jobs.

https://www.pakainfo.com/laravel-supervisor-start-stop-restart-and-update-database-queue/

Here I am providing database as queue driver setting up option.

In .env file add / edit following value

QUEUE_DRIVER=database

As it has sync as by default value in config/queue.php file.

Note: there are many other drivers are available see the following doc for more reference:

https://laravel.com/docs/5.6/queues

Step 2: Setting up the database table for queue

So run following command to create queue table schema migration.

“php artisan queue:table”

Then run “php artisan migrate” to run migration for queue table.

Step 3: Create a job

To create a job there you can run “php artisan make:job ProcessPodcast”, that will create a new job file under ‘app\Jobs’ directory.

It will have following file contents


namespace App\Jobs;

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

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

    protected $podcast;

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

    /**
     * Execute the job.
     *
     * @param
  AudioProcessor  $processor
     * @return void
     */
    public function handle(AudioProcessor $processor)
    {
       // Process uploaded podcast...
    }
}

view rawPodcastJob.php hosted with ❤ by GitHub

The ‘handle’ method is called when the job is processed by the queue. Note that we are able to type-hint dependencies on the handle method of the job. The Laravel service container automatically injects these dependencies.

Step 4: Dispatch the job

Once you have written your job class, you may dispatch it using the ‘dispatch’ method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor:

From your controller file you can dispatch job by adding


Public function testfunction(Request $request, $podcast)
{
  ProcessPodcast::dispatch($podcast);
}

view rawTestController.php hosted with ❤ by GitHub

Step 5: Process queue

To process created job queue need to run following command

php artisan queue:work --tries=3

One approach to specifying the maximum number of times a job may be attempted is via the --tries switch on the Artisan command line:

You can also specify try to job file

public $tries = 5; like this way.

Once you have written your job class, you may dispatch it using the ‘dispatch’ method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor.

Alternate of step 5: Setup up the Supervisor

If you have live server then it is recommended that you should setup the supervisor on server and it will automatically run queue worker.

To configure supervisor there are below steps involved

i) Installing Supervisor
Run “sudo apt-get install supervisor” command.

ii) Configuring Supervisor

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let’s create a laravel-worker.conf file that starts and monitors a queue:work process:

[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
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

view rawlaravel-worker.conf hosted with ❤ by GitHub
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. Of course, you should change the queue:work sqs portion of the command directive to reflect your desired queue connection.

iii) Starting Supervisor

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:*

For more information on Supervisor, consult the Supervisor documentation.

Step 6: Dealing With Failed Jobs

Sometimes your queued jobs will fail. Don’t worry, things don’t always go as planned! Laravel includes a convenient way to specify the maximum number of times a job should be attempted. After a job has exceeded this amount of attempts, it will be inserted into the failed_jobs database table. To create a migration for the failed_jobs table, you may use the queue:failed-table command:

php artisan queue:failed-table

php artisan migrate

Then, when running your queue worker, you should specify the maximum number of times a job should be attempted using the –tries switch on the queue:work command. If you do not specify a value for the –tries option, jobs will be attempted indefinitely:

php artisan queue:work redis --tries=3

Job queue is very useful in system like if you have large amount of data on one server and want to transfer using API to another server, then it is highly recommended to use job queue.

Discussion

pic
Editor guide