Skip to content

Commit ead1049

Browse files
authored
Merge pull request #79 from dbpolito/horizon
Adding Horizon Is Running Check
2 parents e3d6d9e + 6794a9a commit ead1049

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
// ],
9191
// 'restarted_within' => 300,
9292
//],
93+
//\BeyondCode\SelfDiagnosis\Checks\HorizonIsRunning::class,
9394
],
9495
],
9596

src/Checks/HorizonIsRunning.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Checks;
4+
5+
use BeyondCode\SelfDiagnosis\Checks\Check;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class HorizonIsRunning implements Check
9+
{
10+
private $error = null;
11+
12+
/**
13+
* The name of the check.
14+
*
15+
* @param array $config
16+
* @return string
17+
*/
18+
public function name(array $config): string
19+
{
20+
return trans('self-diagnosis::checks.horizon_is_running.name');
21+
}
22+
23+
/**
24+
* Perform the actual verification of this check.
25+
*
26+
* @param array $config
27+
* @return bool
28+
*/
29+
public function check(array $config): bool
30+
{
31+
try {
32+
Artisan::call('horizon:status');
33+
$output = Artisan::output();
34+
35+
return strstr($output, 'Horizon is running.');
36+
} catch (\Exception $e) {
37+
$this->error = $e->getMessage();
38+
}
39+
40+
return false;
41+
}
42+
43+
/**
44+
* The error message to display in case the check does not pass.
45+
*
46+
* @param array $config
47+
* @return string
48+
*/
49+
public function message(array $config): string
50+
{
51+
if ($this->error !== null) {
52+
return trans('self-diagnosis::checks.horizon_is_running.message.unable_to_check', [
53+
'reason' => $this->error,
54+
]);
55+
}
56+
57+
return trans('self-diagnosis::checks.horizon_is_running.message.not_running');
58+
}
59+
}

tests/HorizonIsRunningTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Tests;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use Orchestra\Testbench\TestCase;
7+
use BeyondCode\SelfDiagnosis\Checks\HorizonIsRunning;
8+
9+
class HorizonIsRunningTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_succeeds_when_horizon_is_running()
13+
{
14+
$check = new HorizonIsRunning();
15+
16+
Artisan::shouldReceive('call');
17+
18+
Artisan::shouldReceive('output')
19+
->andReturn('Horizon is running.');
20+
21+
$this->assertTrue($check->check([]));
22+
}
23+
24+
/** @test */
25+
public function is_fails_when_horizon_is_not_running()
26+
{
27+
$check = new HorizonIsRunning();
28+
29+
Artisan::shouldReceive('call');
30+
31+
Artisan::shouldReceive('output')
32+
->andReturn('Horizon is paused.');
33+
34+
$this->assertFalse($check->check([]));
35+
}
36+
}

translations/en/checks.php

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
'message' => 'These environment variables are defined in your .env file, but are missing in your .env.example:' . PHP_EOL . ':variables',
5050
'name' => 'The example environment variables are up-to-date',
5151
],
52+
'horizon_is_running' => [
53+
'message' => [
54+
'not_running' => 'Horizon is not running.',
55+
'unable_to_check' => 'Unable to check for horizon: :reason',
56+
],
57+
'name' => 'Horizon is running',
58+
],
5259
'locales_are_installed' => [
5360
'message' => [
5461
'cannot_run_on_windows' => 'This check cannot be run on Windows.',

0 commit comments

Comments
 (0)