fix: wrap database updates in a transaction for consistency in GetContainersStatus
This commit is contained in:
parent
761f177b1e
commit
23c165d4d1
1 changed files with 15 additions and 10 deletions
|
|
@ -10,6 +10,7 @@
|
||||||
use App\Models\ServiceDatabase;
|
use App\Models\ServiceDatabase;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Lorisleiva\Actions\Concerns\AsAction;
|
use Lorisleiva\Actions\Concerns\AsAction;
|
||||||
|
|
||||||
class GetContainersStatus
|
class GetContainersStatus
|
||||||
|
|
@ -376,6 +377,10 @@ public function handle(Server $server, ?Collection $containers = null, ?Collecti
|
||||||
if (isset($this->applicationContainerRestartCounts) && $this->applicationContainerRestartCounts->has($applicationId)) {
|
if (isset($this->applicationContainerRestartCounts) && $this->applicationContainerRestartCounts->has($applicationId)) {
|
||||||
$containerRestartCounts = $this->applicationContainerRestartCounts->get($applicationId);
|
$containerRestartCounts = $this->applicationContainerRestartCounts->get($applicationId);
|
||||||
$maxRestartCount = $containerRestartCounts->max() ?? 0;
|
$maxRestartCount = $containerRestartCounts->max() ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap all database updates in a transaction to ensure consistency
|
||||||
|
DB::transaction(function () use ($application, $maxRestartCount, $containerStatuses) {
|
||||||
$previousRestartCount = $application->restart_count ?? 0;
|
$previousRestartCount = $application->restart_count ?? 0;
|
||||||
|
|
||||||
if ($maxRestartCount > $previousRestartCount) {
|
if ($maxRestartCount > $previousRestartCount) {
|
||||||
|
|
@ -398,18 +403,18 @@ public function handle(Server $server, ?Collection $containers = null, ?Collecti
|
||||||
$url = null;
|
$url = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Aggregate status after tracking restart counts
|
// Aggregate status after tracking restart counts
|
||||||
$aggregatedStatus = $this->aggregateApplicationStatus($application, $containerStatuses, $maxRestartCount);
|
$aggregatedStatus = $this->aggregateApplicationStatus($application, $containerStatuses, $maxRestartCount);
|
||||||
if ($aggregatedStatus) {
|
if ($aggregatedStatus) {
|
||||||
$statusFromDb = $application->status;
|
$statusFromDb = $application->status;
|
||||||
if ($statusFromDb !== $aggregatedStatus) {
|
if ($statusFromDb !== $aggregatedStatus) {
|
||||||
$application->update(['status' => $aggregatedStatus]);
|
$application->update(['status' => $aggregatedStatus]);
|
||||||
} else {
|
} else {
|
||||||
$application->update(['last_online_at' => now()]);
|
$application->update(['last_online_at' => now()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue