در حال بارگذاری ...

Introducing the Scheduler0:00

Alright, why don't we start out with easily one of my favorite things in Laravel 5, and that is the new scheduler. So let's go to app, and we want to visit our Kernel class. Now don't confuse this with your HTTP Kernel. We want our ConsoleKernel, because we will be scheduling what translates to cronjobs. So ConsoleKernel. And you'll see right here, in addition to giving us a new location to register artisan commands, which is a lot cleaner, you'll see we have a new method, schedule. This is brand new in Laravel 5, and I think you're going to love it. So what does this exactly do?

This is brand new in Laravel 5, and I think you're going to love it. So what does this exactly do? Well think about all of the various cron jobs that you need to run for your applications. In fact, I can give you a few that Laracast does right now. Once every day around 11:55 PM, I fire off what amounts to a daily report to myself. And it simply breaks down who signed up, who canceled, how much revenue came in for the day, what was the breakdown of monthly to yearly to business subscriptions. It's just a typical daily breakdown. So that's one job that I automatically run. Another one is monthly based.

So that's one job that I automatically run. Another one is monthly based. Once a month, I fire a php artisan command that just does some database cleanup. It clears out some records that are just too old and don't need to be maintained anymore. So before this, well, it was kind of awkward, right? Because we would have to create these cron jobs and register them, but it wouldn't really be part of our application. I wouldn't be able to open up my folder and figure out what kind of jobs am I running? What is the schedule? There's no way for that to be in version control, right?

Scheduling Commands and Exec1:58

What is that one? What is that? LayerFile makes it a lot easier using the scheduler component. So take a look at this. If we need to schedule an artisan command every single hour, then why don't we write it in that exact same way in our code base? That way it's more readable and it's under version control. So take a look. schedule receives an instance of the Schedule class, and we can say schedule command. Note this is specifically an artisan command.

Schedule receives an instance of the Schedule class, and we can say schedule command. Note this is specifically an artisan command. So this would be the equivalent of saying every single hour fire php artisan inspire. And if you're curious, after we go over the basics in the next video, we're actually going to dig into the source code itself to figure out how all of this actually works, which should be fun. Now what other sorts of things can we do? Well, yes, you'll often want to trigger artisan commands, but you also might want to just do some regular terminal commands. In those situations, we just use exec, like this.

do some regular terminal commands. In those situations, we just use exec, like this. Schedule and we're going to execute what? Maybe something ludicrous like create a file. touch foo.txt every five minutes. And of course, maybe foo is dynamic. It's ridiculous. It doesn't really matter. The basic idea is you can execute anything you want, and then you can specify the interval in a very readable way.

The basic idea is you can execute anything you want, and then you can specify the interval in a very readable way. So in this case, well, you'll often want to do things every 5 minutes or every 10 minutes. Well, in that case, Laravel has some helpers that will just simplify the whole process. So you have every 5 minutes. You also have every 10 minutes. But just be careful. This isn't like a dynamic magic method. So it's not like you can do every 31 minutes. That's not available.

Real-World Scheduling Examples3:59

Or every day at 1030, do this. Well, you could say daily. That would be fine. Or if you need to specify the time, you can say daily at 1030. So cool, right? So let me show you. What were those examples we talked about for Laracasts? Once a month, I fire a php artisan command to clear out some database records. Okay. Schedule.

Okay. Schedule. Command. Laracasts. I keep everything under a Laracasts namespace for my artisan command. So Laracasts. And I believe that's called clear history. So I want to trigger this artisan command once a month. So monthly. And I'm done.

So monthly. And I'm done. So for the initial setup, there's literally nothing else I have to do here. What about the other one where once a day, I fire off a daily report? Schedule. Command. Laracasts. Daily. Report. And we want to do this daily at 23:55.

Exploring Scheduler API6:42

If you really want to see the full API, always just go to the source. So if we click through to schedule, though, well, we'll take a look and we don't see anything. And don't worry. That's just the way this code is built to make it really readable. If we are scheduling a command, well, you can see that's just calling exec behind the scenes. So the commands method is a wrapper around this exec. We'll talk about all of this stuff more in the next video where we dig into the source. I just want to show you the basic API first. Now we can see that we are returning a SchedulerEvent class.

I just want to show you the basic API first. Now we can see that we are returning a Event class. And here is where we can see the full API. So for example, every five minutes, every 10, I'm sorry, there's no every 15, but there's an every 30 minutes. And then you can see, you could call days, which allows you to basically specify a number that refers to the day of the week, like on Mondays, I want to do this. So yeah, you could call that. But again, days one is a little weird. Instead, let's make it easy.

But again, days one is a little weird. Instead, let's make it easy. Let's just call Mondays. So that means you could say schedule command, pass the name of your command. And we want to do that every single Monday. How cool is that? And of course, the same is true for the rest of the days of the week. Next we have things like do this every single week, weekly on. And also just in general, if you want to learn more about how to write cron jobs, well, they're just calling a master cron method here.

Setting Up Server Cron8:31

Do we just start updating this file and we're good to go on our server? Well, no, but you only have to do about 30 seconds worth of work. I'm going to switch over to Chrome. And if we visit the documentation for artisan, you can review the full docs for the scheduler component. You'll see the way this works is you just need to hit one artisan command every single minute. And that way, when we hit it, Laravel can basically check to see, do we have any commands that need to be fired? If so, let's go ahead and execute those.

that need to be fired? If so, let's go ahead and execute those. And if not, then we don't have to do anything. That's the way this is handled. Every single minute, we fire a single cron job. And now that's the only cron job you will write. So you can see here, * * * * *, every minute, fire php artisan schedule:run. That's a new artisan command in Laravel 5. Again, you'll only ever do this once.

That's a new artisan command in Laravel 5. Again, you'll only ever do this once. And now if this stuff is confusing to you, you really don't need to worry about it too much. But if you're curious, well, one would be like your standard output. So you're saying a standard output append, it doesn't really matter whether that's one greater than or two greater than signs. But anyways, take the standard output and just send it through to /dev/null. We don't care about the output. We don't want it.

We don't want to worry about any of that stuff. So on your server, all you have to do is register this. If you're using something like Laravel Forge, well, here, I'll show you real quick. Let's go to the Laracast server, over to scheduler. And yeah, here you can see the default actually gives you a little hint. Just provide the path to artisan. Let me show you that real quick. We'll use, how about just my VM here. And now let's go into cd code, our folder for the series, and then just get the present working directory.

And now let's go into cd, our folder for the series, and then just get the present working directory. So you would want that path and then php artisan schedule:run and schedule the job every single minute and you're good to go. Otherwise, just do it however you normally would. Or if you want to test this out with Homestead, remember, there's really no reason why you would ever fire cron jobs on your development machine. The only reason you would do it is if you're just curious to make sure it works and you would only do it temporarily. In that case, you could do crontab -e.

Keeping Logic in Commands11:01

would only do it temporarily. In that case, you could do crontab -e. And if we go to the very bottom, you can just paste that in right there and you'll get the exact same end result. But yeah, for Homestead, there's really no reason why you would ever do that. If you want to test that a command works, then just trigger the commands and make sure it works. And that's the final important thing that I want to review here. If we go back to our Kernel class, notice that there's no logic here. So if you find yourself with tons of lines here where you kind of set up things, you

If we go back to our Kernel class, notice that there's no logic here. So if you find yourself with tons of lines here where you kind of set up things, you make a database query to figure out which records you're working with, and then maybe you pass a closure and you trigger something, it's really too much work. If you have a lot of logic that decides how you proceed, well, an artisan command is exactly where you should handle that. So run php artisan make:command, and in that class is where you do your logic. Now in your Kernel class, you simply trigger that command at the interval you require. It couldn't be simpler.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟