Skip to content

Commit 791b7c6

Browse files
authored
Added site health check job (#1)
1 parent a08b590 commit 791b7c6

20 files changed

+542
-102
lines changed

.github/workflows/pint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
run: composer global require laravel/pint
2121

2222
- name: Run Pint
23-
run: pint
23+
run: pint --test
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Jobs\CheckSiteHealth;
6+
use App\Models\Site;
7+
use Illuminate\Console\Command;
8+
9+
class PerformSiteHealthCheck extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'app:check-site-health {siteId}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Check site health for given site id';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle()
29+
{
30+
CheckSiteHealth::dispatchSync(
31+
Site::findOrFail($this->input->getArgument('siteId'))
32+
);
33+
}
34+
}

app/Enum/HttpMethod.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Enum;
4+
5+
enum HttpMethod
6+
{
7+
case POST;
8+
case GET;
9+
case PATCH;
10+
case HEAD;
11+
case PUT;
12+
case DELETE;
13+
}

app/Enum/WebserviceEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Enum;
4+
5+
enum WebserviceEndpoint: string
6+
{
7+
case HEALTH_CHECK = "health.json";
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
7+
class HorizonBasicAuthMiddleware
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next)
17+
{
18+
$authenticationHasPassed = false;
19+
20+
if ($request->header('PHP_AUTH_USER', null) && $request->header('PHP_AUTH_PW', null)) {
21+
$username = $request->header('PHP_AUTH_USER');
22+
$password = $request->header('PHP_AUTH_PW');
23+
24+
if ($username === config('horizon.basic_auth.username')
25+
&& $password === config('horizon.basic_auth.password')) {
26+
$authenticationHasPassed = true;
27+
}
28+
}
29+
30+
if ($authenticationHasPassed === false) {
31+
return response()->make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
32+
}
33+
34+
return $next($request);
35+
}
36+
}

app/Jobs/CheckSiteHealth.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Jobs;
6+
7+
use App\Models\Site;
8+
use App\Services\SiteConnectionService;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Queue\Queueable;
11+
12+
class CheckSiteHealth implements ShouldQueue
13+
{
14+
use Queueable;
15+
16+
/**
17+
* Create a new job instance.
18+
*/
19+
public function __construct(protected readonly Site $site)
20+
{
21+
}
22+
23+
/**
24+
* Execute the job.
25+
*/
26+
public function handle(): void
27+
{
28+
/** @var SiteConnectionService $connection */
29+
$connection = $this->site->connection;
30+
31+
$response = $connection->checkHealth();
32+
33+
$healthData = collect($response);
34+
35+
// Write updated data to DB
36+
$this->site->update(
37+
$healthData->only([
38+
'php_version',
39+
'db_type',
40+
'db_version',
41+
'cms_version',
42+
'server_os'
43+
])->toArray()
44+
);
45+
}
46+
}

app/Jobs/UpdateSite.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Jobs;
6+
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Queue\Queueable;
9+
10+
class UpdateSite implements ShouldQueue
11+
{
12+
use Queueable;
13+
14+
/**
15+
* Create a new job instance.
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Execute the job.
24+
*/
25+
public function handle(): void
26+
{
27+
//
28+
}
29+
}

app/Models/Site.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace App\Models;
46

5-
use Illuminate\Database\Eloquent\Factories\HasFactory;
6-
use Illuminate\Foundation\Auth\User as Authenticatable;
7-
use Illuminate\Notifications\Notifiable;
7+
use App\Services\SiteConnectionService;
8+
use Illuminate\Database\Eloquent\Model;
89

9-
class Site extends Authenticatable
10+
class Site extends Model
1011
{
1112
protected $fillable = [
1213
'url',
@@ -34,4 +35,14 @@ protected function casts(): array
3435
'update_major' => 'bool'
3536
];
3637
}
38+
39+
public function getUrlAttribute(string $value): string
40+
{
41+
return rtrim($value, "/") . "/";
42+
}
43+
44+
public function getConnectionAttribute(): SiteConnectionService
45+
{
46+
return new SiteConnectionService($this->url, $this->key);
47+
}
3748
}

app/Models/User.php

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Laravel\Horizon\Horizon;
6+
use Laravel\Horizon\HorizonApplicationServiceProvider;
7+
8+
class HorizonServiceProvider extends HorizonApplicationServiceProvider
9+
{
10+
/**
11+
* Overload authorization method from \Laravel\Horizon\HorizonApplicationServiceProvider
12+
* to allow access to Horizon without having a logged in user.
13+
*
14+
* @return void
15+
*/
16+
protected function authorization()
17+
{
18+
Horizon::auth(function () {
19+
return true;
20+
});
21+
}
22+
}

0 commit comments

Comments
 (0)