From 3a3219394d1815e1e58078676f436e946b5a3e2e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 10 Jan 2025 20:07:01 +0100 Subject: [PATCH] refactor: enhance ApplicationDeploymentJob and HorizonServiceProvider for improved job handling - Removed the private property for application deployment queue ID in ApplicationDeploymentJob and utilized constructor property promotion. - Added a tags method in ApplicationDeploymentJob to properly identify the worker running the job. - Updated HorizonServiceProvider to handle cases where the deployment queue ID is blank, preventing potential errors during job processing. - Cleaned up the isAnyDeploymentInprogress function by removing unnecessary whitespace. --- app/Jobs/ApplicationDeploymentJob.php | 13 ++++++++----- app/Providers/HorizonServiceProvider.php | 3 +++ bootstrap/helpers/shared.php | 1 - 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 3092016531..538c2eca08 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -43,8 +43,6 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue public static int $batch_counter = 0; - private int $application_deployment_queue_id; - private bool $newVersionIsHealthy = false; private ApplicationDeploymentQueue $application_deployment_queue; @@ -166,16 +164,21 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue public $tries = 1; - public function __construct(int $application_deployment_queue_id) + public function tags() + { + // Do not remove this one, it needs to properly identify which worker is running the job + return ['App\Models\ApplicationDeploymentQueue:'.$this->application_deployment_queue_id]; + } + + public function __construct(public int $application_deployment_queue_id) { $this->onQueue('high'); - $this->application_deployment_queue = ApplicationDeploymentQueue::find($application_deployment_queue_id); + $this->application_deployment_queue = ApplicationDeploymentQueue::find($this->application_deployment_queue_id); $this->application = Application::find($this->application_deployment_queue->application_id); $this->build_pack = data_get($this->application, 'build_pack'); $this->build_args = collect([]); - $this->application_deployment_queue_id = $application_deployment_queue_id; $this->deployment_uuid = $this->application_deployment_queue->deployment_uuid; $this->pull_request_id = $this->application_deployment_queue->pull_request_id; $this->commit = $this->application_deployment_queue->commit; diff --git a/app/Providers/HorizonServiceProvider.php b/app/Providers/HorizonServiceProvider.php index 2a459e74af..0caa3a3a93 100644 --- a/app/Providers/HorizonServiceProvider.php +++ b/app/Providers/HorizonServiceProvider.php @@ -38,6 +38,9 @@ public function boot(): void $deploymentQueueId = collect($tags)->first(function ($tag) { return str_contains($tag, 'App\Models\ApplicationDeploymentQueue'); }); + if (blank($deploymentQueueId)) { + return; + } $deploymentQueueId = explode(':', $deploymentQueueId)[1]; $deploymentQueue = ApplicationDeploymentQueue::find($deploymentQueueId); $deploymentQueue->update([ diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index fd7e63b75f..32b7463def 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -1258,7 +1258,6 @@ function get_public_ips() function isAnyDeploymentInprogress() { - $runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('status', ApplicationDeploymentStatus::IN_PROGRESS->value)->get(); $horizonJobIds = []; foreach ($runningJobs as $runningJob) {