ArtisanTinkerer.github.io

Blog

View on GitHub

https://laravel.com/docs/5.7/queues#running-the-queue-worker

Queues

Dispatching jobs

Job::dispatch();

Job::dispatch()->onQueue('emails');
php artisan queue:work --queue=high,default

Creating Jobs

php artisan make:job ProcessPodcast

Dispatching Jobs

ProcessPodcast::dispatch($podcast);

ProcessPodcast::dispatch($podcast)
  ->delay(now()->addMinutes(10));
  

Job Chaining

ProcessPodcast::withChain([
  new OptimizePodcast,
  new ReleasePodcast
])->dispatch();

Chain Connection and Queue

ProcessPodcast::withChain([
  new OptimizePodcast,
  new ReleasePodcast
])->dispatch()->allOnCOnnection('redis')->allOnQueue('podcasts');

Customizing Queue and Connection

Dispatching To a Particular Queue

ProcessPodcast::dispatch($podcast)->onQueue('processing');

Dispatching To a Particular Connection

ProcessPodcast::dispatch($podcast)->onConnection('sqs');

Specifying Mac Job Attempts /Timeout Values

Max Attempts

On the queue worker

php artisan queue:work --tries=3

On the job itself

public tries =5;

Time Based Attempts

Instead of specifying the number of retries, you can set the retry period (on the job class):

public function retryUntil()
{
  return now()->addSeconds(5);
}

Timeout

Can be set on the queue worker:

php artisan queue:work --timeout-30

or on the job:

public $timeout = 120; //seconds

Rate Limiting

If using Redis - can throttle queued jobs - eg: can only run 10 times in 60 seconds

Error Handling

Running The Queue Worker

php artisan queue:work

They will not notice changes in your code after they have been started

Specifying Connection and Queue

php artisan queue:work redis --queue=emails

Process A Single Job

php artisan queue:work --once

Process All Queued Jobs and Exit

php artisan queue:work --stop-when-empty

Resource Considerations

You should free any heavy resources after each job completes.

Queue Priorities

dispatch((new Job)->onQueue('high'));

Queue Workers + Deployment

Workers won’t pick up code changes, so on deployment:

php artisan queue:restart

Job Expirations & Timeouts

retry_after