Skip to content

Commit 8dcd6e4

Browse files
committed
Added jobs to queeue pending health checks
1 parent 791b7c6 commit 8dcd6e4

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ AWS_BUCKET=
6363
AWS_USE_PATH_STYLE_ENDPOINT=false
6464

6565
VITE_APP_NAME="${APP_NAME}"
66+
67+
HEALTH_CHECK_INTERVAL=24

app/Console/Commands/PerformSiteHealthCheck.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class PerformSiteHealthCheck extends Command
2525
/**
2626
* Execute the console command.
2727
*/
28-
public function handle()
28+
public function handle(): int
2929
{
3030
CheckSiteHealth::dispatchSync(
3131
Site::findOrFail($this->input->getArgument('siteId'))
3232
);
33+
34+
return Command::SUCCESS;
3335
}
3436
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Jobs\CheckSiteHealth;
6+
use App\Models\Site;
7+
use Carbon\Carbon;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Database\Eloquent\Collection;
10+
11+
class QueueHealthChecks extends Command
12+
{
13+
protected int $totalPushed = 0;
14+
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'app:queue-health-checks';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Pushes pending health checks to queue';
28+
29+
/**
30+
* Execute the console command.
31+
*/
32+
public function handle(): int
33+
{
34+
$this->output->writeln('Pushing pending health checks');
35+
36+
Site::query()
37+
->whereDate('last_seen', '<', Carbon::now()->subHours(env('HEALTH_CHECK_INTERVAL', 24)))
38+
->chunkById(
39+
100,
40+
function (Collection $chunk) {
41+
// Show progress
42+
$this->output->write('.');
43+
44+
$this->totalPushed += $chunk->count();
45+
46+
// Push each site check to queue
47+
$chunk->each(fn($site) => CheckSiteHealth::dispatch($site));
48+
}
49+
);
50+
51+
// Result
52+
$this->output->writeln("");
53+
$this->output->writeln('Pushed ' . $this->totalPushed . ' pending jobs to queue');
54+
55+
return Command::SUCCESS;
56+
}
57+
}

app/Jobs/CheckSiteHealth.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\Site;
88
use App\Services\SiteConnectionService;
9+
use Carbon\Carbon;
910
use Illuminate\Contracts\Queue\ShouldQueue;
1011
use Illuminate\Foundation\Queue\Queueable;
1112

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

3536
// Write updated data to DB
36-
$this->site->update(
37+
$this->site->fill(
3738
$healthData->only([
3839
'php_version',
3940
'db_type',
@@ -42,5 +43,8 @@ public function handle(): void
4243
'server_os'
4344
])->toArray()
4445
);
46+
47+
$this->site->last_seen = Carbon::now();
48+
$this->site->save();
4549
}
4650
}

routes/console.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
use Illuminate\Support\Facades\Schedule;
44
use Illuminate\Queue\Console\PruneFailedJobsCommand;
5+
use App\Console\Commands\QueueHealthChecks;
56

67
Schedule::command(PruneFailedJobsCommand::class)->daily();
8+
Schedule::command(QueueHealthChecks::class)->everyFifteenMinutes();

0 commit comments

Comments
 (0)