From a3648901ed88f0147f01633f88bc4a4c99e87494 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 10 Jan 2025 15:48:23 +0100 Subject: [PATCH] feat: enhance horizon:manage command with worker restart check - Added a new option `--can-i-restart-this-worker` to the `horizon:manage` command. - Implemented logic to check if the current worker can be restarted based on running jobs in the ApplicationDeploymentQueue. - Refactored the command to include a new method `canIRestartThisWorker` for better code organization. - Removed unnecessary dump statement from the CustomJobRepository. --- app/Console/Commands/HorizonManage.php | 28 +++++++++++++++++++++++- app/Repositories/CustomJobRepository.php | 1 - 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/HorizonManage.php b/app/Console/Commands/HorizonManage.php index a03c41a7ba..de4a5a2642 100644 --- a/app/Console/Commands/HorizonManage.php +++ b/app/Console/Commands/HorizonManage.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Models\ApplicationDeploymentQueue; use App\Repositories\CustomJobRepository; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; @@ -15,17 +16,21 @@ class HorizonManage extends Command { - protected $signature = 'horizon:manage'; + protected $signature = 'horizon:manage {--can-i-restart-this-worker}'; protected $description = 'Manage horizon'; public function handle() { + if ($this->option('can-i-restart-this-worker')) { + return $this->canIRestartThisWorker(); + } $action = select( label: 'What to do?', options: [ 'pending' => 'Pending Jobs', 'running' => 'Running Jobs', + 'can-i-restart-this-worker' => 'Can I restart this worker?', 'workers' => 'Workers', 'failed' => 'Failed Jobs', 'failed-delete' => 'Failed Jobs - Delete', @@ -33,6 +38,16 @@ public function handle() ] ); + if ($action === 'can-i-restart-this-worker') { + $runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('horizon_job_status', 'reserved')->get(); + $count = $runningJobs->count(); + if ($count > 0) { + return false; + } + + return true; + } + if ($action === 'pending') { $pendingJobs = app(JobRepository::class)->getPending(); $pendingJobsTable = []; @@ -136,4 +151,15 @@ public function handle() $redisJobRepository->purge($queueName); } } + + public function canIRestartThisWorker() + { + $runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('horizon_job_status', 'reserved')->get(); + $count = $runningJobs->count(); + if ($count > 0) { + return false; + } + + return true; + } } diff --git a/app/Repositories/CustomJobRepository.php b/app/Repositories/CustomJobRepository.php index 3d67cdd1a9..518be844f6 100644 --- a/app/Repositories/CustomJobRepository.php +++ b/app/Repositories/CustomJobRepository.php @@ -31,7 +31,6 @@ public function getJobsByStatus(string $status, ?string $worker = null): Collect $this->getRecent()->each(function ($job) use ($jobs, $status, $worker) { if ($job->status === $status) { if ($worker) { - dump($job); if ($job->worker !== $worker) { return; }