How do we know when all of the jobs within a batch have completed? Inside of the import CSV job, it's pretty easy to work out the amount of rows that we've inserted.
00:11
But how do we get aware of the entire batch as a whole? Well, what we can actually do is onto the end of the bus batch here, or just before we dispatch this, we can go ahead and use a finally callback.
00:24
What that's going to do is allow us to perform some sort of action after all of this has completed. That action is going to be updating the import to say that it was completed. So once each of these jobs have successfully finished and we have this finally callback,
00:41
we can go ahead and update the import in the database to tell us that it's completed. So very, very straightforward, but worth its own episode if you are new to bus batching. Okay. So let's go ahead and first of all,
00:54
bring the import into scope, and we're going to go ahead and say import. For this, again, we could say update or we could just say touch, and we could touch the completed date,
01:06
and that will insert for us the current date and time. Now, there are loads of other callbacks that we can use here, like we can catch errors if we want to. You might want to add these on if you want to deal with
01:17
any particular jobs that have failed within this batch. We may cover that later as well. But this is enough for now just so we can see that this is completed. Okay. So we shouldn't need to restart our cube.
01:29
I'm just going to go ahead and do this anyway, and we can go ahead and again process this and see this working. So again, I'm going to get rid of the imports in this table. I'm going to get rid of all of these customers,
01:40
although we don't necessarily need to because we're upserting, and we're going to come over and import this again. So let's hit upload a file. Let's choose customers.
01:49
Go ahead and map these up as we've been doing, and let's hit import. Okay. So let's head over to the database. We should see in imports here, this increment for every single job that we have.
02:02
And then finally, when we get to the end, and that particular batch has finished, we have gone ahead and touched the complete to that timestamp, which has given us the date and time that this particular import has finished.
25 episodes•2 hrs 20 mins•2 years ago
Overview
Let's build a powerful CSV importer with Livewire, completely from scratch. This can handle millions of rows, be reused for multiple models, and by using job batches, doesn't require the browser to be open.
This course is for you if:
You need a robust importer component for your models that you have full control over
You want to brush up on some advanced Livewire concepts
You want to learn about job batching and queues in Laravel