From 7582d7dd8b6a3a2b72b9cbd0bbeb034cce25b366 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 10 Jan 2025 19:53:13 +0100 Subject: [PATCH] refactor: streamline job status retrieval and clean up repository interface - Simplified the job status retrieval process by consolidating logic into a single `getJobStatus` function. - Removed redundant checks and methods from the `CustomJobRepositoryInterface` and `CustomJobRepository`. - Updated the `getHorizonJobStatus` method in `ApplicationDeploymentQueue` to directly utilize the new `getJobStatus` function. - Enhanced the `isThereAJobInProgress` method to improve clarity and maintainability. --- app/Console/Commands/HorizonManage.php | 7 +--- .../CustomJobRepositoryInterface.php | 5 --- app/Models/ApplicationDeploymentQueue.php | 4 -- app/Repositories/CustomJobRepository.php | 38 +------------------ bootstrap/helpers/shared.php | 9 +++-- 5 files changed, 9 insertions(+), 54 deletions(-) diff --git a/app/Console/Commands/HorizonManage.php b/app/Console/Commands/HorizonManage.php index 9acc16ed6..ca2da147c 100644 --- a/app/Console/Commands/HorizonManage.php +++ b/app/Console/Commands/HorizonManage.php @@ -173,11 +173,6 @@ class HorizonManage extends Command public function getJobStatus(string $jobId) { - $jobFound = app(JobRepository::class)->getJobs([$jobId]); - if ($jobFound->isEmpty()) { - return 'unknown'; - } - - return $jobFound->first()->status; + return getJobStatus($jobId); } } diff --git a/app/Contracts/CustomJobRepositoryInterface.php b/app/Contracts/CustomJobRepositoryInterface.php index 689b1897f..1fbd71f46 100644 --- a/app/Contracts/CustomJobRepositoryInterface.php +++ b/app/Contracts/CustomJobRepositoryInterface.php @@ -16,9 +16,4 @@ interface CustomJobRepositoryInterface extends JobRepository * Get the count of jobs with a specific status. */ public function countJobsByStatus(string $status): int; - - /** - * Get jobs that have been running longer than a specified duration in seconds. - */ - public function getLongRunningJobs(int $seconds): Collection; } diff --git a/app/Models/ApplicationDeploymentQueue.php b/app/Models/ApplicationDeploymentQueue.php index f6a88a692..95c78d725 100644 --- a/app/Models/ApplicationDeploymentQueue.php +++ b/app/Models/ApplicationDeploymentQueue.php @@ -72,10 +72,6 @@ class ApplicationDeploymentQueue extends Model public function getHorizonJobStatus() { - if (! $this->horizon_job_id) { - return 'unknown'; - } - return getJobStatus($this->horizon_job_id); } diff --git a/app/Repositories/CustomJobRepository.php b/app/Repositories/CustomJobRepository.php index af299895d..502dd252b 100644 --- a/app/Repositories/CustomJobRepository.php +++ b/app/Repositories/CustomJobRepository.php @@ -21,25 +21,12 @@ class CustomJobRepository extends RedisJobRepository implements CustomJobReposit return $this->getJobsByStatus('reserved'); } - public function getJobStatus(string $jobId): string - { - return $this->connection()->get('job:'.$jobId.':status'); - } - - /** - * Get all jobs with a specific status. - */ - public function getJobsByStatus(string $status, ?string $worker = null): Collection + public function getJobsByStatus(string $status): Collection { $jobs = new Collection; - $this->getRecent()->each(function ($job) use ($jobs, $status, $worker) { + $this->getRecent()->each(function ($job) use ($jobs, $status) { if ($job->status === $status) { - if ($worker) { - if ($job->worker !== $worker) { - return; - } - } $jobs->push($job); } }); @@ -47,32 +34,11 @@ class CustomJobRepository extends RedisJobRepository implements CustomJobReposit return $jobs; } - /** - * Get the count of jobs with a specific status. - */ public function countJobsByStatus(string $status): int { return $this->getJobsByStatus($status)->count(); } - /** - * Get jobs that have been running longer than a specified duration in seconds. - */ - public function getLongRunningJobs(int $seconds): Collection - { - $jobs = new Collection; - - $this->getRecent()->each(function ($job) use ($jobs, $seconds) { - if ($job->status === 'reserved' && - isset($job->reserved_at) && - (time() - strtotime($job->reserved_at)) > $seconds) { - $jobs->push($job); - } - }); - - return $jobs; - } - public function getQueues(): array { $queues = $this->connection()->keys('queue:*'); diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 17e17b7fa..fd7e63b75 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -1262,8 +1262,8 @@ function isAnyDeploymentInprogress() $runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('status', ApplicationDeploymentStatus::IN_PROGRESS->value)->get(); $horizonJobIds = []; foreach ($runningJobs as $runningJob) { - $horizonJobId = getJobStatus($runningJob->horizon_job_id); - if ($horizonJobId === 'unknown') { + $horizonJobStatus = getJobStatus($runningJob->horizon_job_id); + if ($horizonJobStatus === 'unknown') { return true; } $horizonJobIds[] = $runningJob->horizon_job_id; @@ -4080,8 +4080,11 @@ function convertGitUrl(string $gitRepository, string $deploymentType, ?GithubApp ]; } -function getJobStatus(string $jobId) +function getJobStatus(?string $jobId = null) { + if (blank($jobId)) { + return 'unknown'; + } $jobFound = app(JobRepository::class)->getJobs([$jobId]); if ($jobFound->isEmpty()) { return 'unknown';