You can install the package via composer:
composer require spatie/laravel-health
##Publishing the config file
Optionally, you can publish the health
config file with this command.
php artisan vendor:publish --tag="health-config"
This is the content of the published config file:
return [
'result_stores' => [
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
'model' => Spatie\Health\Models\HealthCheckResultHistoryItem::class,
'keep_history_for_days' => 5,
],
],
'notifications' => [
'enabled' => true,
'notifications' => [
Spatie\Health\Notifications\CheckFailedNotification::class => ['mail'],
],
'notifiable' => Spatie\Health\Notifications\Notifiable::class,
'throttle_notifications_for_minutes' => 60,
'mail' => [
'to' => 'your@example.com',
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
],
'slack' => [
'webhook_url' => env('HEALTH_SLACK_WEBHOOK_URL', ''),
'channel' => null,
'username' => null,
'icon' => null,
],
],
'oh_dear_endpoint' => [
'enabled' => false,
'always_send_fresh_results' => true,
'secret' => env('OH_DEAR_HEALTH_CHECK_SECRET'),
'url' => '/oh-dear-health-check-results',
],
'theme' => 'light',
'silence_health_queue_job' => true,
];
##Migrating the database
This package can store health check results in various ways. When using the EloquentHealthResultStore
the check results will be stored in the database. To create the health_check_result_history_items
table, you must create and run the migration.
php artisan vendor:publish --tag="health-migrations"
php artisan migrate
These steps are not necessary when using the JsonFileResultStore
.
##Running the checks by scheduling a command
If you want to let your application send notifications when something is wrong, you should schedule the RunHealthChecksCommand
to run every minute.
protected function schedule(Schedule $schedule)
{
$schedule->command(\Spatie\Health\Commands\RunHealthChecksCommand::class)->everyMinute();
}
##Running the checks by sending HTTP requests
If you don't want to let your application send notification, but let a service like Oh Dear monitor the health of your app, you can trigger a run of all health checks by visiting the HTTP endpoint or JSON endpoint and use the ?fresh
parameter in the URL.