Skip to content

Commit

Permalink
Added jobs to queeue pending health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Nov 16, 2024
1 parent 791b7c6 commit 8dcd6e4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion app/Console/Commands/PerformSiteHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class PerformSiteHealthCheck extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
CheckSiteHealth::dispatchSync(
Site::findOrFail($this->input->getArgument('siteId'))
);

return Command::SUCCESS;
}
}
57 changes: 57 additions & 0 deletions app/Console/Commands/QueueHealthChecks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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(env('HEALTH_CHECK_INTERVAL', 24)))

Check failure on line 37 in app/Console/Commands/QueueHealthChecks.php

View workflow job for this annotation

GitHub Actions / phpstan

Called 'env' outside of the config directory which returns null when the config is cached, use 'config'.
->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;
}
}
6 changes: 5 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,8 @@ public function handle(): void
'server_os'
])->toArray()
);

$this->site->last_seen = Carbon::now();

Check failure on line 47 in app/Jobs/CheckSiteHealth.php

View workflow job for this annotation

GitHub Actions / phpstan

Property App\Models\Site::$last_seen (string) does not accept Carbon\Carbon.
$this->site->save();
}
}
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();

0 comments on commit 8dcd6e4

Please sign in to comment.