Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 26, 2023
1 parent f0e8581 commit fb235bd
Show file tree
Hide file tree
Showing 28 changed files with 400 additions and 5 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^10.0",
"laravel-notification-channels/discord": "^1.5",
"laravel-notification-channels/telegram": "^4.0",
"mtdowling/cron-expression": "^1.2",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
85 changes: 85 additions & 0 deletions src/Checks/Base/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Vormkracht10\LaravelOK\Checks;

use Cron\CronExpression;
use Illuminate\Console\Scheduling\ManagesFrequencies;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Vormkracht10\LaravelOK\Enums\Status;

abstract class Check
{
use ManagesFrequencies;

protected string $expression = '* * * * *';

protected ?string $name = null;

protected ?string $label = null;

protected bool $shouldRun = true;

public function __construct()
{
}

public static function config(): static
{
$instance = app(static::class);

$instance->everyMinute();

return $instance;
}

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

return $this;
}

public function getName(): string
{
if ($this->name) {
return $this->name;
}

$baseName = class_basename(static::class);

return Str::of($baseName)->beforeLast('Check');
}

public function shouldRun(): bool
{
if (! $this->shouldRun) {
return false;
}

$date = Date::now();

return (new CronExpression($this->expression))->isDue($date->toDateTimeString());
}

public function if(bool $condition)
{
$this->shouldRun = $condition;

return $this;
}

public function unless(bool $condition)
{
$this->shouldRun = ! $condition;

return $this;
}

abstract public function run(): Result;

public function markAsCrashed(): Result
{
return new Result(Status::CRASHED);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
103 changes: 103 additions & 0 deletions src/Checks/Base/CheckUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Spatie\Health\Checks\Checks;

use Exception;
use Illuminate\Support\Facades\Http;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;
use Spatie\Health\Exceptions\InvalidCheck;

class CheckUrl extends Check

Check failure on line 11 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Spatie\Health\Checks\Checks\CheckUrl extends unknown class Spatie\Health\Checks\Check.
{
protected ?string $url = null;

protected ?string $failureMessage = null;

protected int $timeout = 1;

protected int $retryTimes = 1;

protected string $method = 'GET';

/** @var array<string, string> */
protected array $headers = [];

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

return $this;
}

public function timeout(int $seconds): self
{
$this->timeout = $seconds;

return $this;
}

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

return $this;
}

public function retryTimes(int $times): self
{
$this->retryTimes = $times;

return $this;
}

/**
* @param array<string, string> $headers
* @return $this
*/
public function headers(array $headers = []): self
{
$this->headers = $headers;

return $this;
}

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

return $this;
}

public function run(): Result

Check failure on line 72 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Health\Checks\Checks\CheckUrl::run() has invalid return type Spatie\Health\Checks\Result.
{
if (is_null($this->url)) {
throw InvalidCheck::urlNotSet();

Check failure on line 75 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method urlNotSet() on an unknown class Spatie\Health\Exceptions\InvalidCheck.
}

try {
$request = Http::timeout($this->timeout)
->withHeaders($this->headers)
->retry($this->retryTimes)
->send($this->method, $this->url);

if (! $request->successful()) {
return $this->failedResult();
}
} catch (Exception) {
return $this->failedResult();
}

return Result::make()

Check failure on line 91 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method make() on an unknown class Spatie\Health\Checks\Result.
->ok()
->shortSummary('Reachable');
}

protected function failedResult(): Result

Check failure on line 96 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Health\Checks\Checks\CheckUrl::failedResult() has invalid return type Spatie\Health\Checks\Result.
{
return Result::make()

Check failure on line 98 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method make() on an unknown class Spatie\Health\Checks\Result.
->failed()
->shortSummary('Unreachable')
->notificationMessage($this->failureMessage ?? "Pinging {$this->getName()} failed.");

Check failure on line 101 in src/Checks/Base/CheckUrl.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Spatie\Health\Checks\Checks\CheckUrl::getName().
}
}
80 changes: 80 additions & 0 deletions src/Checks/Base/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Vormkracht10\LaravelOK\Checks;

use Carbon\CarbonInterface;
use Vormkracht10\LaravelOK\Enums\Status;

class Result
{
public Check $check;

public ?CarbonInterface $ended_at;

public static function make(string $message = ''): self
{
return new self(Status::OK, $message);
}

public function __construct(
public Status $status,
public string $notificationMessage = '',
) {
}

public function check(Check $check): self
{
$this->check = $check;

return $this;
}

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

return $this;
}

public function ok(string $message = ''): self
{
$this->notificationMessage = $message;

$this->status = Status::ok();

Check failure on line 43 in src/Checks/Base/Result.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Vormkracht10\LaravelOK\Enums\Status::ok().

return $this;
}

public function warning(string $message = ''): self
{
$this->notificationMessage = $message;

$this->status = Status::warning();

Check failure on line 52 in src/Checks/Base/Result.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Vormkracht10\LaravelOK\Enums\Status::warning().

return $this;
}

public function failed(string $message = ''): self
{
$this->notificationMessage = $message;

$this->status = Status::failed();

Check failure on line 61 in src/Checks/Base/Result.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Vormkracht10\LaravelOK\Enums\Status::failed().

return $this;
}

/** @param array<string, mixed> $meta */
public function meta(array $meta): self
{
$this->meta = $meta;

return $this;
}

public function endedAt(CarbonInterface $carbon): self
{
$this->ended_at = $carbon;

return $this;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions src/Checks/DebugModeCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Vormkracht10\LaravelOK\Checks\Checks;

use function config;
use Vormkracht10\LaravelOK\Checks\Result;

class DebugModeCheck extends Check
{
protected bool $expected = false;

public function expectedToBe(bool $bool): self
{
$this->expected = $bool;

return $this;
}

public function run(): Result
{
$actual = config('app.debug');

$result = Result::make();

return $this->expected === $actual
? $result->ok()
: $result->failed("The debug mode was expected to be `{$this->convertToWord((bool) $this->expected)}`, but actually was `{$this->convertToWord((bool) $actual)}`");
}

protected function convertToWord(bool $boolean): string
{
return $boolean ? 'true' : 'false';
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added src/Checks/HorizonCheck.php
Empty file.
Empty file added src/Checks/LogCheck.php
Empty file.
Empty file.
Empty file.
Empty file added src/Checks/RedisMemoryCheck.php
Empty file.
Empty file added src/Checks/RoutesCacheCheck.php
Empty file.
Empty file added src/Checks/SchedulerCheck.php
Empty file.
Empty file added src/Checks/StorageCheck.php
Empty file.
Empty file added src/Checks/ViewsCacheCheck.php
Empty file.
11 changes: 11 additions & 0 deletions src/Enums/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Vormkracht10\LaravelOK\Enums;

enum Status: string
{
case OK = 'ok';
case FAILED = 'failed';
case SKIPPED = 'skipped';
case CRASHED = 'crashed';
}
4 changes: 2 additions & 2 deletions src/Facades/OK.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Illuminate\Support\Facades\Facade;

/**
* @see \Vormkracht10\LaravelOK\LaravelOK
* @see \Vormkracht10\LaravelOK\OK
*/
class LaravelOK extends Facade
class OK extends Facade
{
protected static function getFacadeAccessor()
{
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelOKServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function configurePackage(Package $package): void
$package
->name('laravel-ok')
->hasConfigFile()
->hasMigration('create_laravel_ok_table')
// ->hasMigration('create_laravel_ok_table')
->hasCommands(
RunChecksCommand::class,
);
Expand Down
Loading

0 comments on commit fb235bd

Please sign in to comment.