Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 23, 2024
1 parent 0158fa6 commit e08c8f9
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 27 deletions.
88 changes: 88 additions & 0 deletions docs/available-checks/backups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: Backups
weight: 2
---

Using a package like [spatie/laravel-backup](https://spatie.be/docs/laravel-backup) you can create backups of your application. The backups are stored as zip files in a directory.

The `BackupCheck` will verify if your backups are up to date. It can check:
- if the youngest backup has been made before a certain date
- if the oldest backup was made after a certain date
- the number of backups
- the size of the backups

## Usage

Here's how you can register the check.

You can use the `locatedAt` method to specify the directory where the backups are stored. The `locatedAt` method accepts a glob pattern.

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

Health::checks([
BackupsCheck::new()->locatedAt('/path/to/backups/*.zip'),
]);
```

### Check the number of the backups

You can use the `numberOfBackups` method to check if the number of backups is within a certain range.
The function accepts a `min` and/or a `max` parameter.

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

Health::checks([
BackupsCheck::new()->locatedAt('/path/to/backups/*.zip')->numberOfBackups(min: 5, max: 10),
]);
```

### Check the age of the backups

You can use the `youngestBackShouldHaveBeenMadeBefore` method to check if the youngest backup was made before a certain date.

Here's an example where we make sure the most recent backup is not older than 1 day.

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

Health::checks([
BackupsCheck::new()
->locatedAt('/path/to/backups/*.zip')
->youngestBackShouldHaveBeenMadeBefore(now()->subDays(1)),
]);
```

You can use the `oldestBackShouldHaveBeenMadeAfter` method to check if the oldest backup was made after a certain date.

Here's an example where we make sure the oldest backup is older than 1 week.

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

Health::checks([
BackupsCheck::new()
->locatedAt('/path/to/backups/*.zip')
->oldestBackShouldHaveBeenMadeAfter(now()->subWeeks(1)),
]);
```

### Specify a minimum size

You can use the `atLeastSizeInMb` method to specify a minimum size for the backups. Only backups that are larger than the specified size will be considered valid.

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

Health::checks([
BackupsCheck::new()
->locatedAt('/path/to/backups/*.zip')
->atLeastSizeInMb(20),
]);
```
2 changes: 1 addition & 1 deletion docs/available-checks/cache.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Application Cache
weight: 2
weight: 3
---

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.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/cached-config-routes-and-events.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Cached config, routes, and events
weight: 3
weight: 4
---

To improve performance, Laravel can cache configuration files, routes and events. Using the `OptimizedAppCheck` you can make sure these things are actually cached.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/cpu-load.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: CPU load
weight: 4
weight: 5
---

This check makes sure that your CPU load isn't too high.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/db-connection-count.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: DB connection count
weight: 6
weight: 7
---

This check makes sure your database doesn't have too much active connections. This check supports MySQL and Postgres.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/db-connection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: DB connection
weight: 5
weight: 6
---

This check makes sure your application can connect to a database. If the `default` database connection does not work, this check will fail.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/db-size-check.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: DB size
weight: 7
weight: 8
---

This check makes sure that your database is not too big. This check supports MySQL and Postgres.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/db-table-size-check.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: DB table size
weight: 8
weight: 9
---

This check makes sure the tables of your database are not too big. This check supports MySQL and Postgres.
Expand Down
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: 9
weight: 10
---

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: 10
weight: 11
---

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: 11
weight: 12
---

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
2 changes: 1 addition & 1 deletion docs/available-checks/horizon.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Horizon
weight: 12
weight: 13
---

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 Down
4 changes: 2 additions & 2 deletions docs/available-checks/meilisearch.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: MeiliSearch
weight: 13
weight: 14
---

[meilisearch.md](meilisearch.md)
This check will verify if MeiliSearch is running. It will call MeiliSearch's [built-in health endpoint](https://docs.meilisearch.com/reference/api/health.html) and verify that its status returns `available`.

## Usage
Expand Down
1 change: 1 addition & 0 deletions docs/available-checks/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Using this package you can register one or more checks to verify the health of y
These are the checks created by us:

- [Application Cache](cache)
- [Backups](backups)
- [CPU Load](cpu-load)
- [Database Connection](db-connection)
- [Database Connection Count](db-connection-count)
Expand Down
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: 14
weight: 15
---

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

This check will make sure that queued jobs are running. This check works by dispatching a test job (this will be done via a scheduled command), and verify if that test job is handled on time.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/redis-memory-usage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Redis memory usage
weight: 17
weight: 18
---

This check makes sure that Redis is not consuming too much memory.
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: 16
weight: 17
---

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: 18
weight: 19
---

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/security-advisories.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Security advisories
weight: 19
weight: 20
---

This check will check if the PHP packages installed in your project have known security vulnerabilities. This check works using [Packagist's security vulnerability API](https://php.watch/articles/composer-audit#packagist-vuln-list-api).
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: 20
weight: 21
---

This check will monitor the percentage of available disk space.
Expand Down
21 changes: 16 additions & 5 deletions src/Checks/Checks/BackupsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class BackupsCheck extends Check

protected int $minimumSizeInMegabytes = 0;

protected int $minimumNumberOfBackups = 0;
protected ?int $minimumNumberOfBackups = null;
protected ?int $maximumNumberOfBackups = null;


public function locatedAt(string $globPath): self
{
Expand Down Expand Up @@ -48,9 +50,10 @@ public function atLeastSizeInMb(int $minimumSizeInMegabytes): self
return $this;
}

public function atLeastNumberOfBackups(int $minimumNumberOfBackups): self
public function numberOfBackups(int $min = null, int $max = null): self
{
$this->minimumNumberOfBackups = $minimumNumberOfBackups;
$this->minimumNumberOfBackups = $min;
$this->maximumNumberOfBackups = $max;

return $this;
}
Expand All @@ -75,8 +78,16 @@ public function run(): Result
return Result::make()->failed('No backups found that are large enough');
}

if ($eligableBackups->count() < $this->minimumNumberOfBackups) {
return Result::make()->failed('Not enough backups found');
if ($this->minimumNumberOfBackups) {
if ($eligableBackups->count() < $this->minimumNumberOfBackups) {
return Result::make()->failed('Not enough backups found');
}
}

if ($this->maximumNumberOfBackups) {
if ($eligableBackups->count() > $this->maximumNumberOfBackups) {
return Result::make()->failed('Too many backups found');
}
}

if ($this->youngestShouldHaveBeenMadeBefore) {
Expand Down
21 changes: 19 additions & 2 deletions tests/Checks/BackupsCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,35 @@

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastNumberOfBackups(2)
->numberOfBackups(min: 2)
->run();
expect($result)->status->toBe(Status::failed());

addTestFile($this->temporaryDirectory->path('second.zip'));

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->atLeastNumberOfBackups(2)
->numberOfBackups(min: 2)
->run();
expect($result)->status->toBe(Status::ok());
});


it('can make sure that there are not too much backups', function() {
addTestFile($this->temporaryDirectory->path('first.zip'));
addTestFile($this->temporaryDirectory->path('second.zip'));

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->numberOfBackups(max: 2)
->run();
expect($result)->status->toBe(Status::ok());

addTestFile($this->temporaryDirectory->path('third.zip'));

$result = $this->backupsCheck
->locatedAt($this->temporaryDirectory->path('*.zip'))
->numberOfBackups(max: 2)
->run();
expect($result)->status->toBe(Status::failed());
});

0 comments on commit e08c8f9

Please sign in to comment.