diff --git a/app/Livewire/Project/Application/DeploymentNavbar.php b/app/Livewire/Project/Application/DeploymentNavbar.php index 39dee287c..4c5d2135a 100644 --- a/app/Livewire/Project/Application/DeploymentNavbar.php +++ b/app/Livewire/Project/Application/DeploymentNavbar.php @@ -42,22 +42,25 @@ public function cancel() { try { $kill_command = "docker rm -f {$this->application_deployment_queue->deployment_uuid}"; - $previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR); + if ($this->application_deployment_queue->logs) { + $previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR); - $new_log_entry = [ - 'command' => $kill_command, - 'output' => "Deployment cancelled by user.", - 'type' => 'stderr', - 'order' => count($previous_logs) + 1, - 'timestamp' => Carbon::now('UTC'), - 'hidden' => false, - ]; - $previous_logs[] = $new_log_entry; - $this->application_deployment_queue->update([ - 'logs' => json_encode($previous_logs, flags: JSON_THROW_ON_ERROR), - ]); - instant_remote_process([$kill_command], $this->server); + $new_log_entry = [ + 'command' => $kill_command, + 'output' => "Deployment cancelled by user.", + 'type' => 'stderr', + 'order' => count($previous_logs) + 1, + 'timestamp' => Carbon::now('UTC'), + 'hidden' => false, + ]; + $previous_logs[] = $new_log_entry; + $this->application_deployment_queue->update([ + 'logs' => json_encode($previous_logs, flags: JSON_THROW_ON_ERROR), + ]); + instant_remote_process([$kill_command], $this->server); + } } catch (\Throwable $e) { + ray($e); return handleError($e, $this); } finally { $this->application_deployment_queue->update([ diff --git a/bootstrap/helpers/applications.php b/bootstrap/helpers/applications.php index 44060421a..424ec02b2 100644 --- a/bootstrap/helpers/applications.php +++ b/bootstrap/helpers/applications.php @@ -11,8 +11,6 @@ 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) { $server = Application::find($application_id)->destination->server; - $deployments_per_server = ApplicationDeploymentQueue::where('server_id', $server_id)->where('status', 'queued')->orWhere('status', 'in_progress')->get(); - $deployment = ApplicationDeploymentQueue::create([ 'application_id' => $application_id, 'server_id' => $server_id, @@ -24,13 +22,14 @@ function queue_application_deployment(int $application_id, int $server_id, strin 'commit' => $commit, 'git_type' => $git_type ]); - $deployments = ApplicationDeploymentQueue::where('application_id', $application_id); + $deployments_per_server = ApplicationDeploymentQueue::where('server_id', $server_id)->whereIn('status', ['in_progress', 'queued'])->get(); + $deployments = ApplicationDeploymentQueue::where('application_id', $application_id)->where('server_id', $server_id); $queued_deployments = $deployments->where('status', 'queued')->get()->sortByDesc('created_at'); $running_deployments = $deployments->where('status', 'in_progress')->get()->sortByDesc('created_at'); - ray($deployments_per_server->count(), $server->settings->concurrent_builds); - 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); + ray("serverId:{$server->id}", "concurrentBuilds:{$server->settings->concurrent_builds}", "deployments:{$deployments_per_server->count()}", "queued:{$queued_deployments->count()}", "running:{$running_deployments->count()}"); + // 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); @@ -42,7 +41,7 @@ function queue_application_deployment(int $application_id, int $server_id, strin if ($running_deployments->count() > 0) { return; } - if ($deployments_per_server->count() >= $server->settings->concurrent_builds) { + if ($deployments_per_server->count() > $server->settings->concurrent_builds) { return; } if ($is_new_deployment) { @@ -59,7 +58,10 @@ function queue_application_deployment(int $application_id, int $server_id, strin function queue_next_deployment(Application $application, bool $isNew = false) { - $next_found = ApplicationDeploymentQueue::where('status', 'queued')->get()->sortBy('created_at')->first(); + $server_id = $application->destination->server_id; + $next_found = ApplicationDeploymentQueue::where('server_id', $server_id)->where('status', 'queued')->get()->sortBy('created_at')->first();; + // $next_found = ApplicationDeploymentQueue::where('status', 'queued')->get()->sortBy('created_at')->first(); + ray($next_found, $server_id); if ($next_found) { if ($isNew) { dispatch(new ApplicationDeploymentNewJob( diff --git a/routes/api.php b/routes/api.php index e03762b03..efccce36c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -6,6 +6,7 @@ use App\Actions\Database\StartPostgresql; use App\Actions\Database\StartRedis; use App\Actions\Service\StartService; +use App\Models\ApplicationDeploymentQueue; use App\Models\User; use App\Providers\RouteServiceProvider; use Illuminate\Http\Request; @@ -23,9 +24,28 @@ | */ +$middlewares = ['auth:sanctum']; +if (isDev()) { + $middlewares = []; +} + Route::get('/health', function () { return 'OK'; }); +Route::group([ + 'middleware' => $middlewares, + 'prefix' => 'v1' +], function () { + Route::get('/deployments', function() { + return ApplicationDeploymentQueue::whereIn("status", ["in_progress", "queued"])->get([ + "id", + "server_id", + "status" + ])->groupBy("server_id")->map(function($item) { + return $item; + })->toArray(); + }); +}); Route::group([ 'middleware' => ['auth:sanctum'], 'prefix' => 'v1'