Skip to content
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

HEALTH_CHECK_INTERVAL=24
11 changes: 7 additions & 4 deletions app/Console/Commands/PerformSiteHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ class PerformSiteHealthCheck extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
CheckSiteHealth::dispatchSync(
Site::findOrFail($this->input->getArgument('siteId'))
);
/** @var Site $site */
$site = Site::findOrFail($this->input->getArgument('siteId'));

CheckSiteHealth::dispatchSync($site);

return Command::SUCCESS;
}
}
61 changes: 61 additions & 0 deletions app/Console/Commands/QueueHealthChecks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Console\Commands;

use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

class QueueHealthChecks extends Command
{
protected int $totalPushed = 0;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:queue-health-checks';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Pushes pending health checks to queue';

/**
* Execute the console command.
*/
public function handle(): int
{
$this->output->writeln('Pushing pending health checks');

Site::query()
->whereDate(
'last_seen',
'<',
Carbon::now()->subHours((int) config('autoupdates.healthcheck_interval')) // @phpstan-ignore-line
)
->chunkById(
100,
function (Collection $chunk) {
// Show progress
$this->output->write('.');

$this->totalPushed += $chunk->count();

// Push each site check to queue
$chunk->each(fn ($site) => CheckSiteHealth::dispatch($site));
}
);

// Result
$this->output->writeln("");
$this->output->writeln('Pushed ' . $this->totalPushed . ' pending jobs to queue');

return Command::SUCCESS;
}
}
7 changes: 6 additions & 1 deletion app/Jobs/CheckSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Site;
use App\Services\SiteConnectionService;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

Expand Down Expand Up @@ -33,7 +34,7 @@ public function handle(): void
$healthData = collect($response);

// Write updated data to DB
$this->site->update(
$this->site->fill(
$healthData->only([
'php_version',
'db_type',
Expand All @@ -42,5 +43,9 @@ public function handle(): void
'server_os'
])->toArray()
);

// @phpstan-ignore-next-line
$this->site->last_seen = Carbon::now();
$this->site->save();
}
}
15 changes: 13 additions & 2 deletions app/Services/SiteConnectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,23 @@ protected function performHttpRequest(
);
}

// Return decoded body
return json_decode(
// Decode body
$return = json_decode(
(string) $response->getBody(),
true,
512,
JSON_THROW_ON_ERROR
);

// Make sure it's an array
if (!is_array($return)) {
throw new RequestException(
"Invalid JSON body",
$request,
$response
);
}

return $return;
}
}
3 changes: 1 addition & 2 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,5 @@
'maintenance' => [
'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
'store' => env('APP_MAINTENANCE_STORE', 'database'),
],

]
];
5 changes: 5 additions & 0 deletions config/autoupdates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'healthcheck_interval' => env('HEALTH_CHECK_INTERVAL', 24)
];
7 changes: 5 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ parameters:
paths:
- app/

# Level 9 is the highest level
level: 5
level: 9

ignoreErrors:
- '#Method .* return type has no value type specified in iterable type array.#'
- '#Method .* has parameter .* with no value type specified in iterable type array.#'
2 changes: 2 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

use Illuminate\Support\Facades\Schedule;
use Illuminate\Queue\Console\PruneFailedJobsCommand;
use App\Console\Commands\QueueHealthChecks;

Schedule::command(PruneFailedJobsCommand::class)->daily();
Schedule::command(QueueHealthChecks::class)->everyFifteenMinutes();
Loading