Files
coolify/tests/Feature/ApplicationDeploymentStatusSideEffectsTest.php
Andras Bacsai e15bd9b7e7 fix(deploy): keep the queue moving and keep healthy containers on late failures
A queued deployment whose commit no longer validates (for example one
queued before an upgrade) made the job dispatch throw. The row stayed
in_progress, blocked every later deployment of the application, and
stopped the queue loop. Such a row is now marked failed with a log
entry and a failure notification, and the next queued deployment
starts.

An exception after the new container passed its health check (for
example in additional destinations, the next-deployment loop, or
notifications) made failed() remove the new container, which could be
the only one running. failed() now keeps a healthy new container, and
errors in the steps after a status change are logged without flipping
a finished deployment to failed.
2026-10-01 20:31:00 +02:00

72 lines
2.8 KiB
PHP

<?php
use App\Events\ApplicationConfigurationChanged;
use App\Jobs\ApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\Environment;
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Queue;
uses(RefreshDatabase::class);
beforeEach(function () {
Server::flushIdentityMap();
InstanceSettings::unguarded(fn () => InstanceSettings::firstOrCreate(['id' => 0]));
$team = Team::factory()->create();
$this->server = Server::factory()->create(['team_id' => $team->id]);
$this->destination = StandaloneDocker::where('server_id', $this->server->id)->firstOrFail();
$project = Project::factory()->create(['team_id' => $team->id]);
$environment = $project->environments()->first()
?? Environment::factory()->create(['project_id' => $project->id]);
$this->application = Application::factory()->create([
'environment_id' => $environment->id,
'destination_id' => $this->destination->id,
'destination_type' => $this->destination->getMorphClass(),
]);
Process::fake(['*' => Process::result(output: '')]);
Queue::fake();
Notification::fake();
});
function createDeploymentForSideEffectsTest(string $status, string $createdAt): ApplicationDeploymentQueue
{
$deployment = ApplicationDeploymentQueue::create([
'application_id' => test()->application->id,
'deployment_uuid' => fake()->uuid(),
'status' => $status,
'server_id' => test()->server->id,
'destination_id' => test()->destination->id,
'commit' => 'HEAD',
'pull_request_id' => 0,
]);
$deployment->forceFill(['created_at' => $createdAt])->save();
return $deployment;
}
test('a failing post-success side effect keeps the deployment finished and starts the next queued deployment', function () {
$current = createDeploymentForSideEffectsTest('in_progress', '2026-10-01 10:00:00');
$next = createDeploymentForSideEffectsTest('queued', '2026-10-01 10:01:00');
Event::listen(ApplicationConfigurationChanged::class, fn () => throw new RuntimeException('broadcast failed'));
$job = new ApplicationDeploymentJob($current->id);
(fn () => $this->completeDeployment())->call($job);
$current->refresh();
expect($current->status)->toBe('finished')
->and($current->logs)->toContain('broadcast failed')
->and($next->fresh()->status)->toBe('in_progress');
Queue::assertPushed(ApplicationDeploymentJob::class, fn (ApplicationDeploymentJob $job) => $job->application_deployment_queue_id === $next->id);
});