Skip to content

Commit

Permalink
added check
Browse files Browse the repository at this point in the history
  • Loading branch information
roblesterjr04 committed Jul 20, 2023
1 parent f21ab13 commit 81cd301
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 49 deletions.
50 changes: 1 addition & 49 deletions src/Checks/Checks/GitHubCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GuzzleHttp\Client;
use Carbon\Carbon;

class GitHubCheck extends Check
class GitHubCheck extends TerminalCheck
{
private $timeout = 5;

Expand All @@ -28,52 +28,4 @@ public function timeout($timeout): self
return $this;
}

private function executeTerminal($cmd, $stdin="", &$stdout = "", &$stderr = "", $timeout=false)
{
$pipes = array();
$process = proc_open(
$cmd,
array(array('pipe','r'),array('pipe','w'),array('pipe','w')),
$pipes
);
$start = time();
$stdout = '';
$stderr = '';

if(is_resource($process))
{
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
}

while(is_resource($process))
{
//echo ".";
$stdout .= stream_get_contents($pipes[1]);
$stderr .= stream_get_contents($pipes[2]);

if($timeout !== false && time() - $start > $timeout)
{
proc_terminate($process, 9);
return 1;
}

$status = proc_get_status($process);
if(!$status['running'])
{
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $status['exitcode'];
}

usleep(100000);
}

return 1;
}

}
86 changes: 86 additions & 0 deletions src/Checks/Checks/TerminalCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Lester\Health\Checks\Checks;

use Spatie\Health\Checks\Check;
use Lester\Health\Checks\Result;
use GuzzleHttp\Client;
use Carbon\Carbon;

class TerminalCheck extends Check
{
private $timeout = 5;
private $command = 'pwd';

public function run(): Result
{
$out = "";
$commandResult = $this->executeTerminal($this->command, $out, $out, $this->timeout);

$result = Result::make();
if ($commandResult > 0) return $result->failed("{$this->command} resulted in an error");

return $result->ok();
}

public function command($command): self
{
$this->command = $command;
return $this;
}

public function timeout($timeout): self
{
$this->timeout = $timeout;
return $this;
}

public function executeTerminal($cmd, $stdin="", &$stdout = "", &$stderr = "", $timeout=false)
{
$pipes = array();
$process = proc_open(
$cmd,
array(array('pipe','r'),array('pipe','w'),array('pipe','w')),
$pipes
);
$start = time();
$stdout = '';
$stderr = '';

if(is_resource($process))
{
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
}

while(is_resource($process))
{
//echo ".";
$stdout .= stream_get_contents($pipes[1]);
$stderr .= stream_get_contents($pipes[2]);

if($timeout !== false && time() - $start > $timeout)
{
proc_terminate($process, 9);
return 1;
}

$status = proc_get_status($process);
if(!$status['running'])
{
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $status['exitcode'];
}

usleep(100000);
}

return 1;
}

}
10 changes: 10 additions & 0 deletions tests/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Lester\Health\Checks\Checks\GitHubCheck;
use Lester\Health\Checks\Checks\TerminalCheck;
use PHPUnit\Framework\TestCase;
use SpoofsLaravelApp;

Expand All @@ -19,5 +20,14 @@ public function testGithubCheck()
$this->assertInstanceOf('Lester\Health\Checks\Result', $result);

}

public function testTerminalCheck()
{
$check = TerminalCheck::new();

$result = $check->command('ls')->run();

$this->assertInstanceOf('Lester\Health\Checks\Result', $result);
}

}

0 comments on commit 81cd301

Please sign in to comment.