fix: restrict concurrent deployments per server

This commit is contained in:
Andras Bacsai 2024-01-25 08:36:47 +01:00
parent 1385a86084
commit e7cafe6850
7 changed files with 49 additions and 7 deletions

View File

@ -55,6 +55,7 @@ public function deployNew()
$this->setDeploymentUuid();
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $this->deploymentUuid,
force_rebuild: false,
is_new_deployment: true,
@ -83,6 +84,7 @@ public function deploy(bool $force_rebuild = false)
$this->setDeploymentUuid();
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $this->deploymentUuid,
force_rebuild: $force_rebuild,
);
@ -112,6 +114,7 @@ public function restartNew()
$this->setDeploymentUuid();
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $this->deploymentUuid,
restart_only: true,
is_new_deployment: true,
@ -128,6 +131,7 @@ public function restart()
$this->setDeploymentUuid();
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $this->deploymentUuid,
restart_only: true,
);

View File

@ -48,6 +48,7 @@ public function deploy(int $pull_request_id, string|null $pull_request_html_url
}
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $this->deployment_uuid,
force_rebuild: false,
pull_request_id: $pull_request_id,

View File

@ -25,6 +25,7 @@ public function rollbackImage($commit)
queue_application_deployment(
application_id: $this->application->id,
server_id: $this->application->destination->server->id,
deployment_uuid: $deployment_uuid,
commit: $commit,
force_rebuild: false,

View File

@ -1,21 +1,18 @@
<?php
use App\Jobs\ApplicationDeployDockerImageJob;
use App\Jobs\ApplicationDeploymentJob;
use App\Jobs\ApplicationDeploymentNewJob;
use App\Jobs\ApplicationDeploySimpleDockerfileJob;
use App\Jobs\ApplicationRestartJob;
use App\Jobs\MultipleApplicationDeploymentJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\ApplicationPreview;
use App\Models\Server;
use Symfony\Component\Yaml\Yaml;
function queue_application_deployment(int $application_id, string $deployment_uuid, int | null $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $restart_only = false, ?string $git_type = null, bool $is_new_deployment = false)
function queue_application_deployment(int $application_id, int $server_id, string $deployment_uuid, int | null $pull_request_id = 0, string $commit = 'HEAD', bool $force_rebuild = false, bool $is_webhook = false, bool $restart_only = false, ?string $git_type = null, bool $is_new_deployment = false)
{
$deployment = ApplicationDeploymentQueue::create([
'application_id' => $application_id,
'server_id' => $server_id,
'deployment_uuid' => $deployment_uuid,
'pull_request_id' => $pull_request_id,
'force_rebuild' => $force_rebuild,
@ -26,10 +23,14 @@ function queue_application_deployment(int $application_id, string $deployment_uu
]);
$server = Application::find($application_id)->destination->server;
$deployments = ApplicationDeploymentQueue::where('application_id', $application_id);
$queued_deployments = $deployments->where('status', 'queued')->get()->sortByDesc('created_at');
$running_deployments = $deployments->where('status', 'in_progress')->get()->sortByDesc('created_at');
$all_deployments = $deployments->where('status', 'queued')->orWhere('status', 'in_progress')->get();
$deployments_per_server = ApplicationDeploymentQueue::where('server_id', $server_id)->where('status', 'queued')->orWhere('status', 'in_progress')->get();
ray('Q:' . $queued_deployments->count() . 'R:' . $running_deployments->count() . '| Queuing deployment: ' . $deployment_uuid . ' of applicationID: ' . $application_id . ' pull request: ' . $pull_request_id . ' with commit: ' . $commit . ' and is it forced: ' . $force_rebuild);
if ($queued_deployments->count() > 1) {
$queued_deployments = $queued_deployments->skip(1);
$queued_deployments->each(function ($queued_deployment, $key) {
@ -40,7 +41,7 @@ function queue_application_deployment(int $application_id, string $deployment_uu
if ($running_deployments->count() > 0) {
return;
}
if ($all_deployments->count() >= $server->settings->concurrent_builds) {
if ($deployments_per_server->count() >= $server->settings->concurrent_builds) {
return;
}
if ($is_new_deployment) {

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('application_deployment_queues', function (Blueprint $table) {
$table->integer('server_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('application_deployment_queues', function (Blueprint $table) {
$table->dropColumn('server_id');
});
}
};

View File

@ -50,6 +50,7 @@
$type = $resource->getMorphClass();
if ($type === 'App\Models\Application') {
queue_application_deployment(
server_id: $resource->destination->server->id,
application_id: $resource->id,
deployment_uuid: new Cuid2(7),
force_rebuild: $force,

View File

@ -149,6 +149,7 @@
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
@ -177,6 +178,7 @@
]);
}
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
pull_request_id: $pull_request_id,
deployment_uuid: $deployment_uuid,
@ -296,6 +298,7 @@
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
@ -319,6 +322,7 @@
]);
}
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
pull_request_id: $pull_request_id,
deployment_uuid: $deployment_uuid,
@ -424,6 +428,7 @@
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
@ -447,6 +452,7 @@
]);
}
queue_application_deployment(
server_id: $application->destination->server->id,
application_id: $application->id,
pull_request_id: $pull_request_id,
deployment_uuid: $deployment_uuid,