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.
This commit is contained in:
Andras Bacsai
2025-01-10 19:53:13 +01:00
parent a0fc46dee4
commit 7582d7dd8b
5 changed files with 9 additions and 54 deletions

View File

@@ -173,11 +173,6 @@ class HorizonManage extends Command
public function getJobStatus(string $jobId) public function getJobStatus(string $jobId)
{ {
$jobFound = app(JobRepository::class)->getJobs([$jobId]); return getJobStatus($jobId);
if ($jobFound->isEmpty()) {
return 'unknown';
}
return $jobFound->first()->status;
} }
} }

View File

@@ -16,9 +16,4 @@ interface CustomJobRepositoryInterface extends JobRepository
* Get the count of jobs with a specific status. * Get the count of jobs with a specific status.
*/ */
public function countJobsByStatus(string $status): int; 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;
} }

View File

@@ -72,10 +72,6 @@ class ApplicationDeploymentQueue extends Model
public function getHorizonJobStatus() public function getHorizonJobStatus()
{ {
if (! $this->horizon_job_id) {
return 'unknown';
}
return getJobStatus($this->horizon_job_id); return getJobStatus($this->horizon_job_id);
} }

View File

@@ -21,25 +21,12 @@ class CustomJobRepository extends RedisJobRepository implements CustomJobReposit
return $this->getJobsByStatus('reserved'); return $this->getJobsByStatus('reserved');
} }
public function getJobStatus(string $jobId): string public function getJobsByStatus(string $status): Collection
{
return $this->connection()->get('job:'.$jobId.':status');
}
/**
* Get all jobs with a specific status.
*/
public function getJobsByStatus(string $status, ?string $worker = null): Collection
{ {
$jobs = new 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 ($job->status === $status) {
if ($worker) {
if ($job->worker !== $worker) {
return;
}
}
$jobs->push($job); $jobs->push($job);
} }
}); });
@@ -47,32 +34,11 @@ class CustomJobRepository extends RedisJobRepository implements CustomJobReposit
return $jobs; return $jobs;
} }
/**
* Get the count of jobs with a specific status.
*/
public function countJobsByStatus(string $status): int public function countJobsByStatus(string $status): int
{ {
return $this->getJobsByStatus($status)->count(); 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 public function getQueues(): array
{ {
$queues = $this->connection()->keys('queue:*'); $queues = $this->connection()->keys('queue:*');

View File

@@ -1262,8 +1262,8 @@ function isAnyDeploymentInprogress()
$runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('status', ApplicationDeploymentStatus::IN_PROGRESS->value)->get(); $runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('status', ApplicationDeploymentStatus::IN_PROGRESS->value)->get();
$horizonJobIds = []; $horizonJobIds = [];
foreach ($runningJobs as $runningJob) { foreach ($runningJobs as $runningJob) {
$horizonJobId = getJobStatus($runningJob->horizon_job_id); $horizonJobStatus = getJobStatus($runningJob->horizon_job_id);
if ($horizonJobId === 'unknown') { if ($horizonJobStatus === 'unknown') {
return true; return true;
} }
$horizonJobIds[] = $runningJob->horizon_job_id; $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]); $jobFound = app(JobRepository::class)->getJobs([$jobId]);
if ($jobFound->isEmpty()) { if ($jobFound->isEmpty()) {
return 'unknown'; return 'unknown';