17
AugLaravel Health Check is a very useful package for checking the health of your Laravel & Lumen applications. The purpose of this package is to surface a health-check endpoint on /health
which, when hit, returns the status of all the services and dependencies your project relies on, along with the overall health of your system. This is useful in both development and production for debugging issues with a faulty application.
This package also adds a /ping
endpoint. Just hit /ping
and receive pong
in response.
To install the package you have to run this command to add the package to your dependencies.
composer require ans-group/laravel-health-check
Scheduler Health Check
The scheduler health check works by using a time-limited cache key on your project every minute. You will need to register the CacheSchedulerRunning
command to run every minute in your projects Kernel.php
. You can customize the cache key and length of time in minutes before the scheduler not running will trigger an error.
$schedule->command(CacheSchedulerRunning::class)->everyMinute();
It’s very simple to create your own health checks.
In this example, we’ll create a health check for Redis.
You first need to create your health-check class, you can put this inside App\HealthChecks
. In this case, the class would be App\HealthChecks\RedisHealthCheck
.
Every health check needs to extend the base HealthCheck
class and implement a status()
method. You should also set the $name
property for display purposes.
<?php
namespace App\HealthChecks;
use UKFast\HealthCheck\HealthCheck;
class RedisHealthCheck extends HealthCheck
{
protected $name = 'my-fancy-redis-check';
public function status()
{
return $this->okay();
}
}
Now we’ve got our basic class setup, we can add it to the list of checks to run in our config/healthcheck.php
file.
Open up config/healthcheck.php
and go to the ‘checks’ array. Add your class to the list of those checks:
'checks' => [
// ...
App\HealthChecks\RedisHealthCheck::class,
]
If you hit the /health
endpoint now, you’ll see that there’s a my-fancy-redis-check
property and it should return OK
for the status.
We can now go about actually implementing the check properly.
Go back to the status()
method in the RedisHealthCheck
class.
Add in the following code:
public function status()
{
try {
Redis::ping();
} catch (Exception $e) {
return $this->problem('Failed to connect to redis', [
'exception' => $this->exceptionContext($e),
]);
}
return $this->okay();
}
You’ll need to import the following at the top as well.
use Illuminate\Support\Facades\Redis;
use UKFast\HealthCheck\HealthCheck;
use Exception;
Finally, hit the /health
endpoint, depending on if your app can actually hit Redis, you’ll see the status of Redis. If it’s still returning OK
try changing REDIS_HOST
to something that doesn’t exist to trip the error.
Have an awesome idea? Then Codebrisk is always here to convert your ideas into reality. Our crew of expert developers is experienced in Laravel custom web app development, CRM software development, and e-commerce app development. We run with a superior level of agility and a vast Laravel ecosystem to customize the software for your business. For more details, please contact us or you can get a free estimate of your project here.
Published at : 17-08-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project