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.
This commit is contained in:
Andras Bacsai
2025-01-10 15:48:23 +01:00
parent 765e1ea04b
commit a3648901ed
2 changed files with 27 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\ApplicationDeploymentQueue;
use App\Repositories\CustomJobRepository; use App\Repositories\CustomJobRepository;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
@@ -15,17 +16,21 @@ use function Laravel\Prompts\table;
class HorizonManage extends Command class HorizonManage extends Command
{ {
protected $signature = 'horizon:manage'; protected $signature = 'horizon:manage {--can-i-restart-this-worker}';
protected $description = 'Manage horizon'; protected $description = 'Manage horizon';
public function handle() public function handle()
{ {
if ($this->option('can-i-restart-this-worker')) {
return $this->canIRestartThisWorker();
}
$action = select( $action = select(
label: 'What to do?', label: 'What to do?',
options: [ options: [
'pending' => 'Pending Jobs', 'pending' => 'Pending Jobs',
'running' => 'Running Jobs', 'running' => 'Running Jobs',
'can-i-restart-this-worker' => 'Can I restart this worker?',
'workers' => 'Workers', 'workers' => 'Workers',
'failed' => 'Failed Jobs', 'failed' => 'Failed Jobs',
'failed-delete' => 'Failed Jobs - Delete', 'failed-delete' => 'Failed Jobs - Delete',
@@ -33,6 +38,16 @@ class HorizonManage extends Command
] ]
); );
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') { if ($action === 'pending') {
$pendingJobs = app(JobRepository::class)->getPending(); $pendingJobs = app(JobRepository::class)->getPending();
$pendingJobsTable = []; $pendingJobsTable = [];
@@ -136,4 +151,15 @@ class HorizonManage extends Command
$redisJobRepository->purge($queueName); $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;
}
} }

View File

@@ -31,7 +31,6 @@ class CustomJobRepository extends RedisJobRepository implements CustomJobReposit
$this->getRecent()->each(function ($job) use ($jobs, $status, $worker) { $this->getRecent()->each(function ($job) use ($jobs, $status, $worker) {
if ($job->status === $status) { if ($job->status === $status) {
if ($worker) { if ($worker) {
dump($job);
if ($job->worker !== $worker) { if ($job->worker !== $worker) {
return; return;
} }