Introducing Job Chains0:00
So far, we have been pushing single jobs to the queue. But sometimes we want to push multiple jobs to the queue that are logically grouped together. You can call it a workflow. In Laravel, there are two types of queued workflows, chains and patches. A chain is a group of jobs that run one after the other. For example, if we are implementing a deployment workflow, we may want to dispatch three jobs that are processed one after the other. So let's configure this chain, we start by declaring a variable called chain, and it's an array. And we put the three jobs of the deployment workflow in this array. So newPoolRepo, and it represents a job and another job that runs tests on tests. And the final job is the deploy job. Now when we push this chain of jobs to the queue, if one of these jobs fail, the entire chain will be removed from the queue and won't continue. Also, the runResults job here will not run.
Dispatching and Testing Chains0:57
we push this chain of jobs to the queue, if one of these jobs fail, the entire chain will be removed from the queue and won't continue. Also, the runResultsJob here will not run until the poolRepoJob finishes. Similarly, the deployJob here will not run until the poolRepoJob and the runTestsJob finishes completely. Now to dispatch this chain to the queue, we are going to use the Pass facade. So we are going to import the Illuminate\Support\Facades\Pass facade, and we are going to call the chain method and then pass the chain variable. And finally call dispatch. Now let's go to the terminal to test this start a worker by php artisan queue:work and then dispatch the chain by visiting the route. And as you can see the worker pick the jobs and it process them in the right order, poolRepo, runTests, and deploy. Let's stop this worker and go back to PHPStorm. And for example, let's go
Chain Failure Behavior1:54
can see the worker pick the jobs and it process them in the right order, pool repo run tests and deploy. Let's stop this worker and go back to PHPStorm. And for example, let's go to the runTests job and inside the handle method, we will throw an Exception. Now let's go back to the terminal, clear it and start a worker queue:work and then dispatch the chain again. And as we can see the first job was processed successfully, then the second job ran and failed. And the third job, which is the deploy job, it didn't run because one of the jobs in the chain has failed. Perfect. The other type of workflows is the batch. A batch is a group of jobs that can run in parallel, they don't depend on each other. And to dispatch a batch, we can use the batch method on the Bus facade. Let's first clear the handle method here and go back to our routes file. And instead of using a chain,
Creating a Job Batch2:52
And to dispatch a batch, we can use the patch method on the Pass facade. Let's first clear the handle method here and go back to our routes file. And instead of using a chain, we are going to dispatch a patch. So a patch. So let's say we are running a patch that pools multiple repos instead of one repo. So the first repo is for ClaraCasts project number one. And the second is LaraCasts project number two, and the third is LaraCasts project number three. And to dispatch a patch, we are going to use the patch method instead of the chain method and change the variable here. We will also need to call the dispatch method at the end. Now the three jobs will be dispatched to the queue and multiple workers will be processing them in parallel. But before we can dispatch this patch, we must add the Patchable trait inside each of these jobs. So we are
to the queue and multiple workers will be processing them in parallel. But before we can dispatch this patch, we must add the Patchable trait inside each of these jobs. So we are going to go to the PoolRepoDrop class and use the Patchable trait here. We also must publish the patches table migration and migrate our database. So let's go to the terminal and call php artisan queue:table. And this creates the migration. And then we call php artisan migrate. And now the database is ready. Let's run the code and dispatch dispatch. So first we clear the terminal and we start a worker php artisan queue:work. Remember, if you want to process the jobs in the patch in parallel, we can start multiple workers. But for the sake of this example, we are just going to start one worker and then visit the route to dispatch the patch. And as you can see the jobs in the patch were
Handling Batch Cancellation5:20
Perfect. Now there is something you need to know, when a job inside a batch fails, the entire batch will be marked as canceled. And to ensure the jobs inside this canceled batch are also canceled, you need to add this check to the handle method of the Job class. So inside the handle method, we are going to add this condition. If this batch was canceled, we are going to just return. And if we have any business logic inside the handle method, we are going to add it after this check. Now when a worker processes the job and it finds the batch was canceled, it's going to return here and the worker is going to consider this job as done and removes it from the queue. If you want, however, you may disable this auto canceling behavior so that a job failure does not automatically mark the entire batch as canceled.
Allowing Failures in Batches6:10
If you want, however, you may disable this auto canceling behavior so that a job failure does not automatically mark the entire patch as canceled. To do that, let's go to the routes file. And before dispatching the batch, we are going to call the allowFailures method. So allowFailures here. Now if any of these jobs in the batch fails, the batch will continue without canceling the rest of the jobs. And now that we have learned how to dispatch queued workflows, we are going to look into configuring those workflows to handle complex situations. That's on the next episode.
