Before we write a single line of code, give me two minutes to provide a real-life illustration of queues and workers. We'll use the example of a pizza delivery restaurant.
Now that you have a basic idea of what a queue is, let's dispatch our first job using Laravel. We'll also learn about delayed job execution, and when you might reach for it.
Now that you understand how to dispatch a simple closure, let's next create a dedicated
Job class, while discussing serialization and daemons.
We've learned how to use the global
dispatch() function, but there's an alternative option you should consider as well. By default, each
Job class includes a
So far, we've reviewed the "happy path" for a job, but what if an error or exception is thrown along the way? If you don't specify a maximum number of tries for your queue worker, the job will retry over and over again. In this episode, we'll learn how to fix this by creating a "failed_jobs" table.
When working with queues, it can often prove difficult to see what's happening. For example, how many jobs ran in the last hour? Did any of them fail? How many workers are running? And what about only the jobs that match a particular tag? Let's solve this by installing Laravel Horizon, a dashboard for your Redis-backed queues.
In this episode, we'll review how to create multiple queue stacks and workers, as well as when and why you might do so.
If you don't have much control over the environment your application runs in, you might consider using the database queue connection. With this approach, rather than using Redis or Memcached, your jobs will instead be stored and fetched from the database.
Before we dig in to learn how Laravel dispatches jobs to the queue, we must first learn about pipelines. It's possible that you've never come across Laravel's
Pipeline API! It's a fluent interface for sending an object or piece of data through a series of "pipes," each of which has the opportunity to modify or verify it in some way.
Now that you understand how pipelines work, let's dive into the Laravel source code to figure out what precisely happens when we call
Source diving isn't always the most enjoyable thing in the world, but, nonetheless, it's an excellent way to improve your understanding of an API. In this episode, we'll learn what happens when a queued job is dispatched, as well as how the worker executes the job in a separate request.
*Series still in development. Check back often for updates.