diff --git a/app/Livewire/Project/Application/Heading.php b/app/Livewire/Project/Application/Heading.php index d91f44b80..fd01790c3 100644 --- a/app/Livewire/Project/Application/Heading.php +++ b/app/Livewire/Project/Application/Heading.php @@ -55,6 +55,7 @@ class Heading extends Component $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 @@ class Heading extends Component $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 @@ class Heading extends Component $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 @@ class Heading extends Component $this->setDeploymentUuid(); queue_application_deployment( application_id: $this->application->id, + server_id: $this->application->destination->server->id, deployment_uuid: $this->deploymentUuid, restart_only: true, ); diff --git a/app/Livewire/Project/Application/Previews.php b/app/Livewire/Project/Application/Previews.php index 57cb43302..bd5206100 100644 --- a/app/Livewire/Project/Application/Previews.php +++ b/app/Livewire/Project/Application/Previews.php @@ -48,6 +48,7 @@ class Previews extends Component } 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, diff --git a/app/Livewire/Project/Application/Rollback.php b/app/Livewire/Project/Application/Rollback.php index a26d5cd47..042812760 100644 --- a/app/Livewire/Project/Application/Rollback.php +++ b/app/Livewire/Project/Application/Rollback.php @@ -25,6 +25,7 @@ class Rollback extends Component queue_application_deployment( application_id: $this->application->id, + server_id: $this->application->destination->server->id, deployment_uuid: $deployment_uuid, commit: $commit, force_rebuild: false, diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.php index 02aedb8a7..884173ab3 100644 --- a/bootstrap/helpers/applications.php +++ b/bootstrap/helpers/applications.php @@ -1,21 +1,18 @@ $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) { diff --git a/database/migrations/2024_01_25_073212_add_server_id_to_queues.php b/database/migrations/2024_01_25_073212_add_server_id_to_queues.php new file mode 100644 index 000000000..1441ad19f --- /dev/null +++ b/database/migrations/2024_01_25_073212_add_server_id_to_queues.php @@ -0,0 +1,28 @@ +integer('server_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('application_deployment_queues', function (Blueprint $table) { + $table->dropColumn('server_id'); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index 470339c46..e03762b03 100644 --- a/routes/api.php +++ b/routes/api.php @@ -50,6 +50,7 @@ Route::group([ $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, diff --git a/routes/webhooks.php b/routes/webhooks.php index 777789940..77484c133 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -149,6 +149,7 @@ Route::post('/source/gitlab/events/manual', function () { 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 @@ Route::post('/source/gitlab/events/manual', function () { ]); } 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 @@ Route::post('/source/github/events/manual', function () { 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 @@ Route::post('/source/github/events/manual', function () { ]); } 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 @@ Route::post('/source/github/events', function () { 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 @@ Route::post('/source/github/events', function () { ]); } queue_application_deployment( + server_id: $application->destination->server->id, application_id: $application->id, pull_request_id: $pull_request_id, deployment_uuid: $deployment_uuid,