2

Dealing with Laravel queue, what I understand is job is the task that is kept in the queue to be performed one after another.

The Laravel doc says :

In other words, if you dispatch a job without explicitly defining which queue it should be dispatched to, the job will be placed on the queue that is defined in the queue attribute of the connection configuration:

// This job is sent to the default queue...

Job::dispatch();

// This job is sent to the "emails" queue...

Job::dispatch()->onQueue('emails');

In another place in the same doc page, I find:

 ProcessPodcast::dispatch($podcast);

ProcessPodcast is a job here which is being dispatched with an argument i.e. $podcast. But nowhere in the doc I found the syntax Job:: except in the mentioned 2 lines.

Q1) So where and how to use the syntax Job:: ?

Every element in the connections array inside config/queue.php has 'queue' => 'default',.

Q2) Where does the emails queue come from in Job::dispatch()->onQueue('emails'); ?

Istiaque Ahmed
  • 6,072
  • 24
  • 75
  • 141
  • 1
    you are literally showing an example using that syntax ... `ProcessPodcast::....` that is a **job**. In the example `Job` is a placeholder for your job class you made – lagbox Jan 11 '20 at 21:00
  • `Job::` in the line rather seems to be a particular facade . Should the doc be more unambiguous ? – Istiaque Ahmed Jan 11 '20 at 21:02
  • its not a facade ... there is no facade named `Job` ... it is a generic word like saying `Class`, you name your jobs not laravel, they don't know what your jobs are called, you have to infer your own classes – lagbox Jan 11 '20 at 21:02
  • I meant it seems to be a facade while it is not. – Istiaque Ahmed Jan 11 '20 at 21:03
  • its called a static method call, its just PHP ... just like you use static method calls with your models, they are not facades – lagbox Jan 11 '20 at 21:04

2 Answers2

2

A1: Like lagbox said, Job is a generic name for any job you would create and dispatch.

A2: The queues don't need to be defined anywhere, you just tell Laravel wich queue you want to dispatch a job to and that's it. For example, if you call Job::dispatch()->onQueue('emails'); for the database connection, a new row will be created in the queue jobs table (the table name is jobs by default), and the column queue will be filled with emails. After that, this job will only be executed if you run a worker specifying the emails queue: php artisan queue:work --queue=emails. If the default queue for the database connection specified in config/queue.php file is the emails queue, then you don't need to specify the queue when running the worker command.

If you have many queues, you can create one process for each one, so you can define how you'd like to run each jobs queue.

Thiago Brauer
  • 321
  • 4
  • 9
0

So where and how to use the syntax Job:: ?

You can use it anywhere, while you can also use the helper dispatch() or implement the trait DispatchesJobs and call $this->dispatch(). All these calls are gonna work, create your job object and dispatch it.

Where does the emails queue come from in Job::dispatch()->onQueue('emails'); ?

Queues are dependent on which driver you run, if you run horizon you are going to need to change the config (and still start the system process). Are you running other queue drivers, you will have to create the queues on the server that should run them php artisan queue:work redis --queue=emails.

Which of these queues the job is dispatched too, is dependant on which queue is specified in the onQueue() call, for maintainability and securing the jobs runs on the correct queue, i prefer to set it in on the job.

class job implements ShouldQueue {
    public $queue = 'emails';
}
mrhn
  • 17,961
  • 4
  • 27
  • 46
  • ` protected $queue = 'emails';` - where is it in the Laravel doc ? – Istiaque Ahmed Jan 12 '20 at 11:08
  • That is taken from the source code, laravel documentation is a help to get going but sometimes it is a good habbit to check it https://github.com/illuminate/bus/blob/b6da517d2cdbbf707c2df3e170e1e1a08a94863e/Queueable.php#L75. Alternatively you can call onQueue('emails') in the constructor of the job. – mrhn Jan 12 '20 at 11:37
  • The doc you mentioned has `public $queue;` instead of `protected $queue` – Istiaque Ahmed Jan 12 '20 at 12:04
  • oh yes wrote it in "as i remembered" again alternative call onQueue in the constructor – mrhn Jan 12 '20 at 12:10