Skip to content

Commit d542a74

Browse files
committed
First commit
0 parents  commit d542a74

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.idea
2+
/vendor
3+
/node_modules
4+
package-lock.json
5+
composer.phar
6+
composer.lock
7+
phpunit.xml
8+
.phpunit.result.cache
9+
.DS_Store
10+
Thumbs.db

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Laravel Healthcheck
2+
3+
This package provides a simple healthcheck endpoint for your Laravel application.
4+
5+
![Import Action](https://raw.githubusercontent.com/coreproc/laravel-healthcheck/main/docs/healthcheck.png)
6+
7+
## Installation
8+
9+
You can install the package via composer:
10+
11+
```bash
12+
composer require coreproc/laravel-healthcheck
13+
```
14+
15+
Publish the config file:
16+
17+
```bash
18+
php artisan vendor:publish --provider="Coreproc\LaravelHealthcheck\HealthcheckServiceProvider"
19+
```
20+
21+
## Usage
22+
23+
Once installed, you can access the healthcheck endpoint at `/healthcheck`.
24+
25+
You can configure the path in the `config/healthcheck.php` file along with specifying which services you want to check.
26+
27+
```php
28+
// config/healthcheck.php
29+
return [
30+
31+
'path' => 'healthcheck',
32+
33+
'database' => true,
34+
35+
'redis' => true,
36+
37+
'horizon' => true,
38+
39+
];
40+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "coreproc/laravel-healthcheck",
3+
"type": "library",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Coreproc\\LaravelHealthcheck\\": "src/"
8+
}
9+
},
10+
"extra": {
11+
"laravel": {
12+
"providers": [
13+
"Coreproc\\LaravelHealthcheck\\HealthcheckServiceProvider"
14+
]
15+
}
16+
},
17+
"authors": [
18+
{
19+
"name": "Chris Bautista",
20+
"email": "chris.bautista@coreproc.com"
21+
}
22+
],
23+
"minimum-stability": "dev",
24+
"require": {}
25+
}

config/healthcheck.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
5+
'path' => 'healthcheck',
6+
7+
'database' => true,
8+
9+
'redis' => true,
10+
11+
'horizon' => true,
12+
13+
];

docs/healthcheck.png

12 KB
Loading

routes/healthcheck.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Coreproc\LaravelHealthcheck\HealthCheckController;
4+
5+
Route::get(config('healthcheck.path', 'healthcheck'), HealthCheckController::class);

src/HealthCheckController.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Coreproc\LaravelHealthcheck;
4+
5+
use Exception;
6+
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Routing\Controller;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Redis;
10+
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
11+
12+
class HealthCheckController extends Controller
13+
{
14+
public function __invoke(): JsonResponse
15+
{
16+
$status = 200; // Default status code is OK
17+
18+
$response = [];
19+
20+
// Check database connection
21+
if (config('healthcheck.database', true) === true) {
22+
try {
23+
DB::connection()->getPdo();
24+
$dbStatus = 'OK';
25+
} catch (Exception $e) {
26+
$status = 500;
27+
$dbStatus = 'Unable to connect to the database: ' . $e->getMessage();
28+
}
29+
$response['db_status'] = $dbStatus;
30+
}
31+
32+
// Check Redis connection
33+
if (config('healthcheck.redis', true) === true) {
34+
try {
35+
Redis::ping();
36+
$redisStatus = 'OK';
37+
} catch (Exception $e) {
38+
$status = 500;
39+
$redisStatus = 'Unable to connect to Redis: ' . $e->getMessage();
40+
}
41+
$response['redis_status'] = $redisStatus;
42+
}
43+
44+
// Check Horizon status
45+
if (config('healthcheck.horizon', true) === true) {
46+
$horizonStatus = 'Queue driver is not Redis';
47+
if (config('queue.default') === 'redis') {
48+
$horizonCurrentStatus = $this->horizonCurrentStatus();
49+
$horizonStatus = 'Horizon is currently ' . $horizonCurrentStatus;
50+
if ($horizonCurrentStatus === 'running') {
51+
$horizonStatus = 'OK';
52+
}
53+
}
54+
$response['horizon_status'] = $horizonStatus;
55+
}
56+
57+
// Assign status to the response but put it in the beginning of the array
58+
$response = array_merge(['status' => $status], $response);
59+
60+
return response()->json($response, $status);
61+
}
62+
63+
protected function horizonCurrentStatus(): string
64+
{
65+
if (!$masters = app(MasterSupervisorRepository::class)->all()) {
66+
return 'inactive';
67+
}
68+
69+
return collect($masters)->every(function ($master) {
70+
return $master->status === 'paused';
71+
}) ? 'paused' : 'running';
72+
}
73+
}

src/HealthcheckServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Coreproc\LaravelHealthcheck;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class HealthcheckServiceProvider extends ServiceProvider
8+
{
9+
public function boot(): void
10+
{
11+
// Publish config file
12+
$this->publishes([
13+
__DIR__ . '/../config/healthcheck.php' => config_path('healthcheck.php'),
14+
], 'healthcheck-config');
15+
16+
$this->loadRoutesFrom(__DIR__ . '/../routes/healthcheck.php');
17+
}
18+
}

0 commit comments

Comments
 (0)