Skip to content

Commit 3812fb0

Browse files
authored
Update RestartHorizon.php
1 parent e5cff7c commit 3812fb0

File tree

1 file changed

+75
-25
lines changed

1 file changed

+75
-25
lines changed

src/Supervisor/Console/RestartHorizon.php

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,78 @@
66

77
class RestartHorizon extends Command
88
{
9-
protected $signature = 'supervisor:check';
10-
11-
protected $description = 'Checks if Horizon is running, starts the queue if needed.';
12-
13-
/**
14-
* The command the actually performs the logic
15-
*/
16-
public function handle()
17-
{
18-
$res = shell_exec("php artisan horizon:status");
19-
20-
if($res !== "Horizon is running.\n") {
21-
echo "Horizon is not running, starting it\n";
22-
$fp = popen("php artisan horizon", "r");
23-
while (!feof($fp)) {
24-
$buffer = fgets($fp, 4096);
25-
echo $buffer;
26-
}
27-
echo "Horizon was terminated and could not be started\n";
28-
pclose($fp);
29-
} else {
30-
echo "Horizon is running\n";
31-
}
32-
}
33-
}
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'supervisor:check {--r|resume-if-paused} {--s|silent} {--p|php-path=}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Checking if Horizon is running and Starting if not';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return int
27+
*/
28+
public function handle(): int
29+
{
30+
// First Check if horizon is installed.
31+
try {
32+
$horizon = app(\Laravel\Horizon\Contracts\MasterSupervisorRepository::class);
33+
} catch (\Exception) {
34+
$this->error('Horizon does not seem to be installed correctly.');
35+
return static::INVALID;
36+
}
37+
38+
// Get supervisors.
39+
$masterSupervisors = $horizon->all();
40+
41+
// if none is running, we should start horizon.
42+
if (count($masterSupervisors) === 0) {
43+
$this->error('Horizon is not running.');
44+
return $this->startHorizon();
45+
}
46+
47+
// Get the first supervisor abd check it's status.
48+
$masterSupervisor = $masterSupervisors[0];
49+
50+
// if paused, we can resume it.
51+
if ($masterSupervisor->status === 'paused') {
52+
$this->warn('Horizon is running, but the status is paused.');
53+
if ($this->option('resume-if-paused')) {
54+
$this->info('Resuming Horizon.');
55+
$this->call('horizon:continue');
56+
} else if (!$this->option('silent') && $this->confirm('Do you want to resume Horizon?')) {
57+
$this->info('Resuming Horizon.');
58+
$this->call('horizon:continue');
59+
} else {
60+
$this->info('You can resume Horizon by running:');
61+
$this->info('php artisan horizon:continue');
62+
}
63+
}
64+
65+
$this->info('Horizon is already running...');
66+
return static::SUCCESS;
67+
}
68+
69+
private function startHorizon(): int
70+
{
71+
$this->line('Starting Horizon...');
72+
$phpPath = $this->option('php-path') ?? 'php';
73+
$fp = popen("{$phpPath} artisan horizon", "r");
74+
while (!feof($fp)) {
75+
$buffer = fgets($fp, 4096);
76+
echo $buffer;
77+
}
78+
$this->error("Horizon was terminated and could not be started");
79+
pclose($fp);
80+
81+
return static::FAILURE;
82+
}
83+
}

0 commit comments

Comments
 (0)