Skip to content

Commit 49fcc9d

Browse files
authored
Added jobs to queeue pending health checks (#2)
* Added jobs to queeue pending health checks * cs fixes
1 parent 791b7c6 commit 49fcc9d

File tree

9 files changed

+102
-11
lines changed

9 files changed

+102
-11
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ class PerformSiteHealthCheck extends Command
2525
/**
2626
* Execute the console command.
2727
*/
28-
public function handle()
28+
public function handle(): int
2929
{
30-
CheckSiteHealth::dispatchSync(
31-
Site::findOrFail($this->input->getArgument('siteId'))
32-
);
30+
/** @var Site $site */
31+
$site = Site::findOrFail($this->input->getArgument('siteId'));
32+
33+
CheckSiteHealth::dispatchSync($site);
34+
35+
return Command::SUCCESS;
3336
}
3437
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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(
38+
'last_seen',
39+
'<',
40+
Carbon::now()->subHours((int) config('autoupdates.healthcheck_interval')) // @phpstan-ignore-line
41+
)
42+
->chunkById(
43+
100,
44+
function (Collection $chunk) {
45+
// Show progress
46+
$this->output->write('.');
47+
48+
$this->totalPushed += $chunk->count();
49+
50+
// Push each site check to queue
51+
$chunk->each(fn ($site) => CheckSiteHealth::dispatch($site));
52+
}
53+
);
54+
55+
// Result
56+
$this->output->writeln("");
57+
$this->output->writeln('Pushed ' . $this->totalPushed . ' pending jobs to queue');
58+
59+
return Command::SUCCESS;
60+
}
61+
}

app/Jobs/CheckSiteHealth.php

Lines changed: 6 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,9 @@ public function handle(): void
4243
'server_os'
4344
])->toArray()
4445
);
46+
47+
// @phpstan-ignore-next-line
48+
$this->site->last_seen = Carbon::now();
49+
$this->site->save();
4550
}
4651
}

app/Services/SiteConnectionService.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,23 @@ protected function performHttpRequest(
9595
);
9696
}
9797

98-
// Return decoded body
99-
return json_decode(
98+
// Decode body
99+
$return = json_decode(
100100
(string) $response->getBody(),
101101
true,
102102
512,
103103
JSON_THROW_ON_ERROR
104104
);
105+
106+
// Make sure it's an array
107+
if (!is_array($return)) {
108+
throw new RequestException(
109+
"Invalid JSON body",
110+
$request,
111+
$response
112+
);
113+
}
114+
115+
return $return;
105116
}
106117
}

config/app.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,5 @@
121121
'maintenance' => [
122122
'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
123123
'store' => env('APP_MAINTENANCE_STORE', 'database'),
124-
],
125-
124+
]
126125
];

config/autoupdates.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'healthcheck_interval' => env('HEALTH_CHECK_INTERVAL', 24)
5+
];

phpstan.neon.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ parameters:
77
paths:
88
- app/
99

10-
# Level 9 is the highest level
11-
level: 5
10+
level: 9
11+
12+
ignoreErrors:
13+
- '#Method .* return type has no value type specified in iterable type array.#'
14+
- '#Method .* has parameter .* with no value type specified in iterable type array.#'

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)