This episode is for members only

Sign up to access "Laravel Model Pruning" right now.

Get started
Already a member? Sign in to continue
Playing
04. Scheduling pruning

Transcript

00:00
So now we've implemented our condition to prune this, and we know how to run this with php artisan, model, and prune. You probably don't want to be doing this manually. Chances are you're going to forget,
00:12
or it's going to waste a huge amount of your time by having to come in, SSH into your server, and specifically run model prune. That's probably not what you want to be doing.
00:22
So what we're going to do is head over to the console kernel class, and we're going to define out a scheduled command in here to do this at a certain point in the day. So you've got a couple of options here.
00:33
You can go ahead and schedule this command like this, model prune, and maybe you could do this daily. What we're going to do just to test this out is do this every minute,
00:43
just so the command actually runs when we do this. But in reality, you'd probably want to do this daily, weekly, whatever suits the schedule that you need. So if we just come over here and run php artisan schedule and run,
00:56
you'll see that that goes ahead and runs that schedule command. Of course, because it's running every minute and the cron job minimum is one minute, then that's going to run every minute.
01:06
Like I said, most of the time you're going to want to run this maybe weekly, at weekends, whatever suits your application. Now, that's absolutely fine. But remember, this is going to prune all models.
01:17
You might have specific requirements for each of your models. And that's where the prune help command came in earlier, and we saw that we can define specific models or exclude things. So for example, let's just say that we had two different models.
01:33
We're not actually going to create another model just yet. But let's say that we wanted to prune one weekly and maybe one at the weekend. So what we'd want to do is as a second argument to this,
01:44
start to pass in some artisan options. So let's just pull that down so it's a little bit neater. And that is model. And it can be any of them that are available.
01:53
So now what we want to do is inside of an array here, because this argument takes in an array, we just want to define out the models that we want to prune. So let's say that we want to define deployments to be pruned weekly.
02:08
And we want to define another command in here to go ahead and, if we just pull this in, prune the user model maybe at weekends, like so. Then that would run this weekly, of course.
02:23
And this would run this only at weekends. So we've got two different conditions and two different schedules for these models. And of course, you can include other models in here if you have them.
02:34
It's really up to you how you define this. You might want to exclude certain things at weekends, whatever you need to do. So once you've gone ahead and defined your schedule commands,
02:44
really all you need on your server now is just to have a command to go ahead and run your schedule, probably a cron job that runs every single minute on your server. And then your pruning is completely taken care of automatically.

Episode summary

In this episode, we take things a step further by automating the pruning process we've just set up. Rather than running php artisan model:prune manually (and risking forgetting, or wasting time SSHing into your server every time), we look at how to schedule this pruning so it runs automatically.

We'll hop into Laravel's console kernel and set up scheduled commands to run pruning at regular intervals—maybe daily, weekly, or even just for testing, every minute. We see how to use artisan's schedule:run command and understand why you'll likely want to fine-tune the frequency for your own app.

We also discuss how to prune different models on different schedules. For example, one model might be pruned weekly, while another only gets pruned on weekends. You'll learn how to pass arguments and options to your scheduled commands, allowing you to be more specific in what gets cleaned up and when.

Finally, once everything is scheduled, all that's left is to set up a cron job on your server to kick off the Laravel scheduler. That means your application's data stays tidy and you never have to think about manual pruning again!

Episode discussion

No comments, yet. Be the first!