In this episode, we're focusing on when to notify a user if one of their monitored endpoints goes down. It might sound simple—just notify every time a check fails, right? But that would be super annoying, especially if you're checking every minute (or, in our testing case, every 10 seconds!). No one wants a flood of emails, so we need to be a little smarter.
We start with a bit of setup: clearing out old endpoints and checks, and adjusting the check frequency so we can see results quickly. After that, we build the logic to detect when a notification should be sent. Instead of notifying on every failure, we set things up to only send a notification the first time an endpoint goes down, then stay quiet until the endpoint comes back up and fails again in the future. This involves creating a new observer for our check model in Laravel, and writing code to log (for now, not actually email!) when a notification would go out.
The main checks are: if the current check fails and the last one succeeded (or if it's the first check ever and it's a failure), that's when we notify. We also cover how to grab the previous check for an endpoint and how to make this logic efficient and clear.
To wrap up, we test our logic by changing an endpoint to deliberately fail and watch our logs to make sure notifications are handled just right. If you really want to be notified every single time, you could, but that's not the best user experience! Next time, we'll move on to actually sending out those email notifications when endpoints go down.