Search code examples
phplaravellaravel-8php-8horizon

How to pause a supervisor in Laravel Horizon


I have multiple supervisor in horizon, and they work normally, the problem is that I want to interact with them by my own web interface, and by interacting I mean pause them and continue (unpause them).

To do that I want to be able as much as possible, without using system (in artisan horizon:pause-supervisor it sends posix_kill($supervisor->pid, 12)).

I tried to instantiate the supervisor by doing this :

class HorizonManager
{
    private SupervisorRepository       $supervisors;
    private MasterSupervisorRepository $masters;
    private WorkloadRepository         $workload;
    private RedisJobRepository         $jobRepository;
    private QueueManager               $queueManager;

    public function __construct(MasterSupervisorRepository $masters, SupervisorRepository $supervisors, WorkloadRepository $workload, RedisJobRepository $jobRepository, QueueManager $manager)
    {
        $this->masters = $masters;
        $this->supervisors = $supervisors;
        $this->workload = $workload;
        $this->jobRepository = $jobRepository;
        $this->queueManager = $manager;

    }

    public function pauseSupervisor(string $supervisorName){
        $supervisor = $this->supervisors->find($supervisorName);
        $supervisorOpt = new SupervisorOptions(...$supervisor->options);
        $sup = new Supervisor($supervisorOpt);
        $sup->pause();
        $sup->persist();
        return $this->supervisors->find($supervisorName);
    }
}

In the return from the function, I have the supervisor paused, but it's not really paused (even If I persist the instantiate supervisor it's still running as a process)


Solution

  • For those interested I failed doing it by instanciating it so instead I send the command using artisan call :

    define('SIGUSR2', 12);
    Artisan::call('horizon:pause-supervisor', ['name'=>$supervisorName]);
    $supervisor = $this->supervisors->find($supervisorName);
    $supervisor->status = 'paused';