This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
23. Scheduling checks every second

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
Up until now, we've been manually running the command performChecks. Now, obviously, that's not what we want to do. We want to schedule this to be run at a certain frequency or an interval.
00:12
Now, unfortunately, within Laravel, the minimum that we can run a scheduled command at is every minute. So let's look at how we would do that first of all, and then we're going to look at an alternative solution. So we're going to come over to our console kernel here, which is where we can schedule specific commands to be run. For example, we can schedule this inspire command, which outputs a quote hourly and so on and so forth.
00:39
Now, our command is called, if we just head up here, checks perform. Let's grab the name of that and just replace this out in here. Now, we can do this every minute. So that works for some things.
00:52
We can do it at any interval we want. The only problem is we don't have any way to do this every second, which is a problem. Let's discuss why that is. Now, our next check is extremely specific because the frequency that we're working out here is within seconds.
01:09
So we could technically run this every minute and it would run some of these, but we would get some sort of overlap at some point where some of these are not run or some of them are run too frequently. So what we really want to do is make this every second. We want to run this every single second.
01:27
And then when we have an exact match within the minutes and the seconds here, that's going to then increment. And then the next time this runs, it's going to work really nicely. So to get around this, we're going to go ahead and use the Laravel short schedule package. Let's go ahead and install this.
01:42
It's really, really easy to get set up with. We just need to go ahead and require this in and then add a method to our kernel here. So we're going to go ahead and pull that in and then we're going to go ahead and get rid of this. We're going to come down here and we're going to create out a protected method in here called short schedule.
02:02
And into this, we receive in the scheduler, which is called short schedule. Which if we just re-index our workspace, there it is. And we're just going to call that short schedule or you could call it schedule. So we're going to go ahead and take this in, replace this out.
02:20
And now what we can do is say every second or every seconds if we wanted to specify that. So now that we've got this in here, we can run this every second and it's going to be a lot more accurate. Now, we didn't look at how we standard run schedules in Laravel, but this is phpr to schedule run. You can see there are no command schedule ready to be run at the moment.
02:42
Now, with the package that we've used, this is a little bit different. We run short schedule run and that's going to go ahead and hang and wait for these to run through. Now, we don't know what's happening at the moment, but obviously at the moment we are going ahead and running this every second. But we don't have anywhere to see what's actually happening here.
03:02
So I'm going to go over to perform endpoint check and I'm just going to dump out the response or log out the response somewhere. So let's go ahead and log some info here and just say response and status. Now, these will be available in the Laravel log. So let's go ahead and clear that out and then pretty much just go ahead and set these to now.
03:24
And what we should see are two statuses logged out as these run. And there you go. You can see they've been logged out. Now, those dates and times have been incremented now or the time specifically has been incremented now. So if we just left this running in a minute's time or in our case, we've got this set up to a five minute interval, this would then run again.
03:46
So I'm going to go ahead and hang around for a little while and in a few moments we should see the next check being performed. OK, so I've just sat and waited for five minutes and sure enough, the next two requests have come in. Probably better to reduce the frequency of the checks rather than wait around. But there we go. So that's now running nicely on a schedule.
04:08
And of course, we don't have to do anything. Any endpoint that is now created will be automatically checked for the next check date. Now that we have our checks running, though, what we really want to do is start to store the information about each of the checks that we perform. And we're going to do that in the next section.
44 episodes4 hrs 59 mins

Overview

Ready to dive into Inertia? Let's build a real-world app with Laravel, Inertia and Vue! If you're already working with Inertia, you'll pick up some tips and techniques for future projects.

This uptime monitor allows you to create and switch between sites, then add endpoints to monitor with frequency intervals. Using the power of scheduling and queues, it'll automatically alert the email addresses you've added via the UI when an endpoint goes down.

In this course, you'll learn to:

  • Build a real app from scratch with Inertia
  • Work with Laravel queues
  • Perform actions on models at user-defined intervals
  • Work with sub-minute schedules in Laravel
  • Send out channel notifications with Laravel
  • Use API resources to simplify Inertia data
  • Organise apps with events and observers
  • Create modals in Vue
  • Perform inline editing with Inertia forms
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.