Skip to content

Commit

Permalink
feat: enhance horizon:manage command with worker restart check
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
andrasbacsai committed Jan 10, 2025
1 parent 765e1ea commit a364890
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
28 changes: 27 additions & 1 deletion app/Console/Commands/HorizonManage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,24 +16,38 @@

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',
'purge-queues' => 'Purge Queues',
]
);

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 = [];
Expand Down Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion app/Repositories/CustomJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a364890

Please sign in to comment.