Handling Failures with Catch0:00
There might be some things we wish to do when any of the drops that belong to a workflow, which is a chain or a patch, when any of these drops fail. For that we can use the catch method. So before calling the dispatch method on a patch or a chain, we are going to call the catch method. And the catch method accepts a closure, and the closure accepts two arguments, the first is the patch, here in case of a patch, and the second is an instance of the exception that was thrown from inside the drop that failed. Now any code that we put here is going to be invoked whenever any of the drops in the patch or a chain, if it was a chain, if any of these drops fail.
Configuring Queue and Connection0:39
Now any code that we put here is going to be invoked whenever any of the drops in the patch or a chain, if it was a chain, if any of these drops fail. We can also configure the queue and the connection we want to dispatch the drop of our chain or patch to. So here we are going to call the onQueue method and configure this patch to be dispatched to the deployments queue. We can also use the onConnection method to configure the connection that we want to dispatch the drop of this patch to. In our case here we are going to dispatch to the database connection. Remember that all three methods, catch and onQueue and onConnection, all these three
Running After Success with Then1:13
In our case here we are going to dispatch to the database connection. Remember that all three methods, catch and onQueue and onConnection, all these three methods can be used on a patch or a chain. Now let's focus a bit on patches, we know that we can use the catch method here to run some code whenever a drop in a patch fails. But what if we want to run code after all drops in a patch have completed successfully? For that we can use the then method. So here we call the then method and pass a closure. And this closure accepts an instance of the patch. Any code that we put here is going to run whenever all three drops inside the patch
Finalizing with Finally1:52
And this closure accepts an instance of the Patch. Any code that we put here is going to run whenever all three drops inside the patch have completed successfully. We can also run some code after all drops in a patch have ran at least one, even if some of the drops fail. To do that we can use the finally method. So here similarly to then we are going to call the finally method and pass a closure. And this closure accepts an instance of the Patch. And any code that we put here is going to be executed after the patch finishes, even if some of the drops of that patch has failed.
Nesting Chains in a Patch2:28
And any code that we put here is going to be executed after the patch finishes, even if some of the drops of that patch has failed. Okay here is something interesting. In Laravel, we can dispatch a chain within a patch. So before we try that, let's clean all this and remove the drops from here. And inside the array of the patch, we are going to create another array. An array within an array inside a patch represents a chain. So in our first chain, we are going to pull a repo for projectOne. And then we are going to dispatch the runTests drop on this same project. And then finally, we are going to dispatch the deploy drop on this project as well.
And then we are going to dispatch the runTests drop on this same project. And then finally, we are going to dispatch the deploy drop on this project as well. And let's copy this and replicate it here. But now we are going to dispatch drops that are related to project number two. Now when dispatch is dispatched, workers will be executing both chains in parallel. And inside the chain itself, drops will run in sequence as we explained in a previous video. This opens the door for so many interesting workflows. Also similarly, we can dispatch a patch inside a chain. So let's clean this method here.
Dispatching a Patch in Chain3:49
Also similarly, we can dispatch a patch inside a chain. So let's clean this method here. And instead of dispatching a patch, we're going to dispatch a chain using the Pass facade. Start an array. And inside the chain, let's put the first drop which is deploy. For example, we're just going to use it for an example. And for the second value of the array, we are going to invoke a closure. And then inside the closure, we're going to use the Pass facade and call the patch method. And here we put all the drops inside that patch and finally call the dispatch method on the patch.
And here we put all the drops inside that patch and finally call the dispatch method on the patch. And don't forget to also call the dispatch method on the chain. Now when this chain is dispatched, the deploy job will run first and then the closure will be invoked, which is going to dispatch a patch to the queue. Imagine all the possible workflows that you can achieve with this setup. I hope you are as impressed with this as I am.
