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

Scheduled Jobs Overview0:01

So once your sites are up and running, responding to user input, sometimes you need something to happen to them on a regular scheduled basis. And if you're old school, you'll probably call this Cron and Forge, they're called scheduled jobs. And then there's also another Health Check tool for your scheduled jobs called Heartbeats. So let's dive into both. First. There are two different places

Site vs Server Scheduler0:16

So let's dive into both. First. There are two different places we can create a scheduled job. We can create it at the server level or at the site level. And they both exist in the same place on the server. But Forge allows us to distinguish them in the user interface. So we're actually gonna start counterintuitively at the site level. So we want to go to the site

Creating a Site Job0:33

counterintuitively at the site level. So we want to go to the site and we want to go to processes and scheduler. And you can see here that we don't have any scheduled jobs in this site yet, but we can create one. And when we create one, it's gonna be very familiar if you've worked with forged scheduled jobs before or if you work with kron in general, we give it a name, we write out the command, we pick the user in the frequency. And then this is new, you monitor it with Heartbeats,

we write out the command, we pick the user in the frequency. And then this is new, you monitor it with Heartbeats, but we're gonna cover that later. So the name right here is gonna be just Schedule Run. The command will be PHP. And then you wanna put your fully Qualified Pass A Home Forge. This one is laracasts tutorial Production on forge.com/current/artisan schedule Run.

on forge.com/current/artisan schedule Run. And of course you can put anything you want here. It doesn't even necessarily have to be a PHP call, but this is the most common thing that people set up with scheduled jobs. Uh, you pick the user. So if you have multiple users, you can change your user. You pick your frequencies and you can have one of these pre-made frequencies

You pick your frequencies and you can have one of these pre-made frequencies or you can use Kron style set up to have a custom frequency. We're just gonna set this one around every minute and we'll cover Heartbeats later. And we create our scheduled job. So now this site has a scheduled job attached to it. It is being served by Kron, but Forge makes it nice and easy for us to understand and we can come into this existing scheduled job

Managing Server-Level Jobs1:49

and easy for us to understand and we can come into this existing scheduled job and we can see what's the output of the last run. When is it coming up next? We can pause it, we can manually run it right now or we can edit it or delete it. So now let's go take a look at the scheduled jobs at the server level. Same server that this site was on. We go to Scheduler and we see three jobs here already.

Same server that this site was on. We go to Scheduler and we see three jobs here already. Two of these jobs are set up on every single server you set up on Forge and that's Update Composer and remove unused packages. And these are just trying to keep your Forge server up and running in good shape. This is updating the version of Composer you have running and this is allowing app to clean up unused packages. They're running weekly and composers I believe is daily

and this is allowing app to clean up unused packages. They're running weekly and composers I believe is daily and I think they might randomize it for every server so they're not hitting the composer basically with every single Forge server all at the same time every day. So if we scroll down, there's one more here and you'll notice it's that same schedule run command we just added to our site. And the one thing that's different is it has this little

just added to our site. And the one thing that's different is it has this little globe here and when you hover it, it shows you which site it's attached to. So the cool thing about this is when you go to the server level scheduler, it's gonna show you all the server scheduled events, but it's also gonna show you all the scheduled events for the sites on that server. And they look just the same here.

One-Click Laravel Scheduler3:01

for the sites on that server. And they look just the same here. You can edit them, you can delete them, you can do whatever else you want, just like the others. There's one other thing I should show you, which is that while this is one way to add this schedule run command, if you wanna add this specific command, there's actually a much easier way to do it. If you go to sites and click on any site, scroll down a little bit,

If you go to sites and click on any site, scroll down a little bit, you're gonna see the scheduler button over here. And just by clicking it here, it's gonna set up that exact same scheduled job that we just set up ourselves. If we go to processes and scheduler, we're gonna see one called Laravel Scheduler, which does the same thing except it's actually a bit smarter than what I typed. 'cause it knows which version of PHP

bit smarter than what I typed. 'cause it knows which version of PHP it's supposed to be using it with. And we didn't have to type any of the directory or remember anything, you know, make any accidental typos. It just handles it for us. So if you wanna spin up the Laravel scheduler, that button's a much better option, but it's still here in case you want to add a scheduled job that is not the Laravel scheduler.

but it's still here in case you want to add a scheduled job that is not the Laravel scheduler. So one of the things that bums me out often is I spin up a site and I forget to set up the scheduler. And then I think Myron Jobs are running and they're not. So I'm very grateful for this button on the website that just says, Hey, is the scheduler set up or not? But there's one other problem, which is it's running, but something's going wrong every time it runs. And I don't notice that at the beginning

Monitoring with Heartbeats4:09

but something's going wrong every time it runs. And I don't notice that at the beginning and maybe it doesn't get reported to my error tracking or I haven't set up error tracking correctly yet. Something that's really helpful here is Laravel. Then Ping Method. If you type Laravel, then Ping, you can see that there is a then ping method that you can chain onto your calls in the scheduler that says once this job is done,

that you can chain onto your calls in the scheduler that says once this job is done, once this command is done running Ping this URL and you have a service somewhere that says, oh this, I should expect to ping once an hour, once a day, whatever the the timing is for this. And if not, I'm gonna notify my owner, you know, the administrator of the site saying, Hey, we were expecting this ping and it didn't happen. Go check to make sure your jobs are working.

we were expecting this ping and it didn't happen. Go check to make sure your jobs are working. Super, super valuable. There's quite a few great services, including Laravel Envoy that have offered these in the past. But great news, we now get them out of the box for free in Forge. So in Forge there are two places for us to add a heartbeat. The first is when we add a scheduled job, you can click monitor with heartbeats down at the bottom.

The first is when we add a scheduled job, you can click monitor with heartbeats down at the bottom. It already knows the frequency. It should be expecting these pings because the frequency is set here. So the only setting there is how long they should wait after an expected ping before it notifies us. So we could say something like, you know, test our command can be something like this. And of course it would have to be a command that ends

our command can be something like this. And of course it would have to be a command that ends with Ping this URL. But right now let's just throw this in. And then when we create the scheduled job, once it's done installing, we can copy the heartbeat URL, and then we need to make sure that this thing that it's calling at the end of it is going to ping this URL or else we'll get notified that it's not working. But as you can probably guess, this is not gonna work

or else we'll get notified that it's not working. But as you can probably guess, this is not gonna work with using PHP Artisan Schedule Run and then using the Laravel scheduler. 'cause that's a single thing here. So even if we added a heartbeat to one of these, it wouldn't be enough heartbeats for all the things we might be triggering. So we can go up to the observe tab for our site and we can see heartbeats here.

So we can go up to the observe tab for our site and we can see heartbeats here. And these heartbeats are for you to attach to any specific job you're calling, especially in the Laravel scheduler. So let's say I had a job in the Laravel scheduler, that's my scraper. It's gonna run maybe every hour and it should notify me if it's been more than five minutes. I hit Create Heartbeat, I copy this URL,

and it should notify me if it's been more than five minutes. I hit Create Heartbeat, I copy this URL, and then I go to my Laravel scheduler in my application and I put add a then ping onto the command, just like we looked at in the docs and that then Ping can either put the URL directly in there, or more likely you're gonna put it in an environment file. And that environment file will go to a config and that config will go into your scheduler. But at some point, when that job is run,

and that config will go into your scheduler. But at some point, when that job is run, it's gonna send a ping over to this URL and then you won't get notified because it's gonna be happy 'cause the heartbeat is satisfied. And that's it.

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