Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix: restrict concurrent deployments per server

Andras Bacsaie7cafe68508b
diff --git a/app/Livewire/Project/Application/Heading.php b/app/Livewire/Project/Application/Heading.php
index d91f44b802..fd01790c34 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 57cb433027..bd52061006 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 a26d5cd475..042812760b 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 02aedb8a72..884173ab3e 100644
--- a/bootstrap/helpers/applications.php
+++ b/bootstrap/helpers/applications.php
@@ -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) {
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 0000000000..1441ad19fd
--- /dev/null
+++ b/database/migrations/2024_01_25_073212_add_server_id_to_queues.php
@@ -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');
+ });
+ }
+};
diff --git a/routes/api.php b/routes/api.php
index 470339c462..e03762b033 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 777789940c..77484c133a 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,