Skip to content

Commit 6f956e6

Browse files
authored
Develop (#58)
* Improve queue stats (#56) * increase productio worker count * Improve queue stats output * cs fix * further increase worker count (#55) * Improve exception handling and logging (#57) * increase productio worker count * improve exceptoin handling and logging * adjust message * fix stan error * include status code changes in exception
1 parent f72fc84 commit 6f956e6

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

app/Health/QueueLengthCheck.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Health;
4+
5+
use Laravel\Horizon\Contracts\JobRepository;
6+
use Spatie\Health\Checks\Check;
7+
use Spatie\Health\Checks\Result;
8+
9+
class QueueLengthCheck extends Check
10+
{
11+
public function run(): Result
12+
{
13+
/** @var JobRepository $jobs */
14+
$jobs = app(JobRepository::class);
15+
16+
$result = Result::make()
17+
->appendMeta([
18+
'pending' => $jobs->countPending(),
19+
'failed' => $jobs->countFailed(),
20+
'completed' => $jobs->countCompleted()
21+
]);
22+
23+
if ($jobs->countPending() > 50000) {
24+
return $result
25+
->failed('Job queue larger than 50.000 items')
26+
->shortSummary('Excessive queue length');
27+
}
28+
29+
return $result->ok();
30+
}
31+
}

app/Jobs/UpdateSite.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ public function handle(): void
5454
$connection = $this->site->connection;
5555

5656
// Test connection and get current version
57-
$healthResult = $connection->checkHealth();
57+
try {
58+
$healthResult = $connection->checkHealth();
59+
} catch (\Throwable $e) {
60+
throw new UpdateException(
61+
"checkHealth",
62+
$e->getMessage(),
63+
(int) $e->getCode(),
64+
$e instanceof \Exception ? $e : null
65+
);
66+
}
5867

5968
// Check the version
6069
if (version_compare($healthResult->cms_version, $this->targetVersion, ">=")) {
@@ -106,7 +115,16 @@ public function handle(): void
106115
return;
107116
}
108117

109-
$prepareResult = $connection->prepareUpdate(["targetVersion" => $this->targetVersion]);
118+
try {
119+
$prepareResult = $connection->prepareUpdate(["targetVersion" => $this->targetVersion]);
120+
} catch (\Throwable $e) {
121+
throw new UpdateException(
122+
"prepareUpdate",
123+
$e->getMessage(),
124+
(int) $e->getCode(),
125+
$e instanceof \Exception ? $e : null
126+
);
127+
}
110128

111129
// Perform the actual extraction
112130
try {
@@ -139,7 +157,12 @@ public function handle(): void
139157
if ($afterUpdateCode !== $this->preUpdateCode) {
140158
throw new UpdateException(
141159
"afterUpdate",
142-
"Status code has changed after update for site: " . $this->site->id
160+
sprintf(
161+
"Status code has changed from %s to %s after update for site: %s",
162+
$this->preUpdateCode,
163+
$afterUpdateCode,
164+
$this->site->id
165+
)
143166
);
144167
}
145168

@@ -213,10 +236,19 @@ public function failed(\Exception $exception): void
213236
$connection = $this->site->connection;
214237

215238
// Get base execption information
216-
$failedStep = $exception instanceof UpdateException ? $exception->getStep() : null;
239+
$failedStep = $exception instanceof UpdateException ? $exception->getStep() : get_class($exception);
217240
$failedMessage = $exception->getMessage();
218241
$failedTrace = $exception->getTraceAsString();
219242

243+
// Log response body
244+
if ($exception instanceof RequestException) {
245+
$failedMessage .= "\n Response Body: " . $exception->getResponse()?->getBody();
246+
}
247+
248+
if ($exception->getPrevious() instanceof RequestException) {
249+
$failedMessage .= "\n Response Body: " . $exception->getPrevious()->getResponse()?->getBody();
250+
}
251+
220252
// Notify users
221253
try {
222254
$connection->notificationFailed(["fromVersion" => $this->site->cms_version, "toVersion" => $this->targetVersion]);

app/Providers/HealthCheckProvider.php

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

33
namespace App\Providers;
44

5+
use App\Health\QueueLengthCheck;
56
use Illuminate\Support\ServiceProvider;
67
use Spatie\CpuLoadHealthCheck\CpuLoadCheck;
78
use Spatie\Health\Checks\Checks\DatabaseCheck;
@@ -29,6 +30,7 @@ public function boot(): void
2930
UsedDiskSpaceCheck::new(),
3031
DatabaseCheck::new(),
3132
HorizonCheck::new(),
33+
QueueLengthCheck::new(),
3234
RedisCheck::new(),
3335
CpuLoadCheck::new()
3436
]);

routes/console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
Schedule::command(CleanupSitesList::class)->everySixHours();
99
Schedule::command(PruneFailedJobsCommand::class)->daily();
1010
Schedule::command(QueueHealthChecks::class)->everyFifteenMinutes();
11+
Schedule::command('horizon:snapshot')->everyFiveMinutes();

0 commit comments

Comments
 (0)