Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache Status #30

Merged
merged 7 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/available-checks/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Application Cache
weight: 2
---

This check makes sure the application can connect to your cache system and read/write to the cache keys. By default, this check will make sure the `default` connection is working.

## Usage

Here's how you can register the check.

```php
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\CacheCheck;

Health::checks([
CacheCheck::new(),
]);
```


### Specifying the cache driver

To check another cache driver, call `driver()`.

```php
CacheCheck::new()->driver('another-cache-driver'),
```
3 changes: 1 addition & 2 deletions docs/available-checks/cpu-load.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: CPU load
weight: 2
weight: 3
---

This check makes sure that your CPU load isn't too high.
Expand Down Expand Up @@ -39,4 +39,3 @@ Health::checks([
->failWhenLoadIsHigherInTheLast15Minutes(1.5);
]);
```

3 changes: 1 addition & 2 deletions docs/available-checks/db-connection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: DB connection
weight: 2
weight: 4
---

This check makes sure your application can connect to a database. If the `default` database connection does not work, this check will fail.
Expand All @@ -26,4 +26,3 @@ To check another database connection, call `connectionName()`
```php
DatabaseCheck::new()->connectionName('another-connection-name'),
```

2 changes: 1 addition & 1 deletion docs/available-checks/debug-mode.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Debug mode
weight: 3
weight: 5
---

This check will make sure that debug mode is set to `false`. It will fail when debug mode is `true`.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/environment.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Environment
weight: 4
weight: 6
---

This check will make sure your application is running used the right environment. By default, this check will fail when the environment is not equal to `production`.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/flare-error-count.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Flare error count
weight: 5
weight: 7
---

This check will monitor the amount of errors and exceptions your application throws. For this check you'll need to have an account on [Flare](https://flareapp.io).
Expand Down
3 changes: 1 addition & 2 deletions docs/available-checks/horizon.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Horizon
weight: 6
weight: 8
---

This check will make sure Horizon is running. It will report a warning when Horizon is paused, and a failure when Horizon is not running.
Expand All @@ -17,4 +17,3 @@ Health::checks([
HorizonCheck::new(),
]);
```

2 changes: 1 addition & 1 deletion docs/available-checks/ping.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Ping
weight: 7
weight: 9
---

This check will send a request to a given URL. It will report a failure when that URL doesn't respond with a successfull response code within a second.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/redis.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Redis
weight: 8
weight: 10
---

This check will make sure Redis is running. By default, this check will make sure the `default` connection is working.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/schedule.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Schedule
weight: 9
weight: 11
---

This check will make sure the schedule is running. If the check detects that the schedule is not run every minute, it will fail.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/used-disk-space.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Used disk space
weight: 10
weight: 12
---

This check will monitor the percentage of available disk space.
Expand Down
56 changes: 56 additions & 0 deletions src/Checks/Checks/CacheCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Spatie\Health\Checks\Checks;

use Exception;
use function app;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;
use Throwable;

class CacheCheck extends Check
{
protected ?string $driver = null;

public function driver(string $driver): self
{
$this->driver = $driver;

return $this;
}

public function run(): Result
{
$driver = $this->driver ?? $this->defaultDriver();

$result = Result::make()->meta([
'driver' => $driver,
]);

try {
$response = $this->pingCache($driver);

return $response
? $result->ok()
: $result->failed("Could not set or retrieve an application cache value.");
} catch (Exception $exception) {
return $result->failed("An exception occurred with the application cache: `{$exception->getMessage()}`");
}
}

protected function defaultDriver(): string
{
return config('cache.default', 'file');
}

protected function pingCache(string $driver): bool
{
$payload = Str::random(5);

Cache::driver($driver)->put('laravel-health:check', $payload, 10);

return (Cache::driver($driver)->get('laravel-health:check') === $payload);
}
}
32 changes: 32 additions & 0 deletions tests/Checks/CacheCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Spatie\Health\Checks\Checks\CacheCheck;
use Spatie\Health\Enums\Status;
use Spatie\Health\Tests\TestClasses\FakeCacheCheck;

it('will determine that a working cache is ok', function () {
$result = CacheCheck::new()->run();

expect($result->status)->toBe(Status::ok());
});


it('will determine that a non-existing cache is not ok', function () {
$result = CacheCheck::new()
->driver('does-not-exist')
->run();

expect($result)
->status->toBe(Status::failed())
->getNotificationMessage()->toBe('An exception occurred with the application cache: `Cache store [does-not-exist] is not defined.`');
});

it('will return an error when it cannot set or retrieve cache key', function () {
$result = FakeCacheCheck::new()
->replyWith(fn () => false)
->run();

expect($result)
->status->toBe(Status::failed())
->notificationMessage->toBe('Could not set or retrieve an application cache value.');
});
23 changes: 23 additions & 0 deletions tests/TestClasses/FakeCacheCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\Health\Tests\TestClasses;

use Closure;
use Spatie\Health\Checks\Checks\CacheCheck;

class FakeCacheCheck extends CacheCheck
{
protected Closure $closure;

public function replyWith(Closure $closure): self
{
$this->closure = $closure;

return $this;
}

public function pingCache(string $driver): bool
{
return ($this->closure)();
}
}