NEW FEATURE: added healthcheck

This commit is contained in:
Jeroen De Meerleer 2022-02-04 14:21:42 +01:00
parent a5b81ce36a
commit d33e200176
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 66 additions and 1 deletions

View File

@ -3,6 +3,11 @@ default:
defaults:
_controller: JeroenED\Webcron\Controller\JobController::defaultAction
health:
path: '/health'
defaults:
_controller: JeroenED\Webcron\Controller\SiteController::HealthAction
login:
path: '/login'
defaults:

View File

@ -43,7 +43,7 @@ class DaemonCommand extends Command
throw new \InvalidArgumentException('Time limit has incorrect value');
}
$jobRepo->unlockJob();
touch($this->kernel->getCacheDir() . '/daemon-running.lock');
while(1) {
if($endofscript !== false && time() > $endofscript) break;
@ -85,6 +85,8 @@ class DaemonCommand extends Command
}
$output->writeln('Ended after ' . $timelimit . ' seconds');
pcntl_wait($status);
unlink($this->kernel->getCacheDir() . '/daemon-running.lock');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace JeroenED\Webcron\Controller;
use JeroenED\Framework\Controller;
use JeroenED\Webcron\Repository\Job;
use Symfony\Component\HttpFoundation\JsonResponse;
class SiteController extends Controller
{
public function HealthAction()
{
global $kernel;
$jobRepo = new Job($this->getDbCon());
$return = [
"DaemonRunning" => file_exists($kernel->getCacheDir() . '/daemon-running.lock'),
"JobsTotal" => count($jobRepo->getAllJobs()),
"JobsDue" => count($jobRepo->getJobsDue()),
"JobsRunning" => count($jobRepo->getRunningJobs()),
"JobsFailing" => count($jobRepo->getFailingJobs()),
];
return new JsonResponse($return, $return['DaemonRunning'] ? 200 : 500);
}
}

View File

@ -46,6 +46,40 @@ class Job extends Repository
return $failingjobs;
}
public function getRunningJobs(bool $idiskey = false)
{
$runRepo = new Run($this->dbcon);
$jobsSql = "SELECT * FROM job WHERE running != 0;";
$jobsStmt = $this->dbcon->prepare($jobsSql);
$jobsRslt = $jobsStmt->executeQuery();
$jobs = $jobsRslt->fetchAllAssociative();
$returnbyid = [];
foreach ($jobs as $key=>&$job) {
$job['data'] = json_decode($job['data'], true);
$job['host-displayname'] = $job['data']['host'];
$job['host'] = $job['data']['host'];
$job['service'] = $job['data']['service'] ?? '';
$job['norun'] = isset($job['lastrun']) && $job['nextrun'] > $job['lastrun'];
$job['running'] = $job['running'] != 0;
$failed = count($runRepo->getRunsForJob($job['id'], true, $job['data']['fail-days']));
$all = count($runRepo->getRunsForJob($job['id'], false, $job['data']['fail-days']));
$job['needschecking'] = $all > 0 && (($failed / $all) * 100) > $job['data']['fail-pct'];
if(!empty($job['data']['containertype']) && $job['data']['containertype'] != 'none') {
$job['host-displayname'] = $job['data']['service'] . ' on ' . $job['data']['host'];
}
if($idiskey) $returnbyid[$job['id']] = $job;
}
if($idiskey) return $returnbyid;
array_multisort(
array_column($jobs, 'name'), SORT_ASC,
array_column($jobs, 'host'), SORT_ASC,
array_column($jobs, 'service'), SORT_ASC,
$jobs);
return $jobs;
}
public function getAllJobs(bool $idiskey = false)
{
$runRepo = new Run($this->dbcon);