diff --git a/config/routes.yaml b/config/routes.yaml index a58c21b..b84cd84 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -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: diff --git a/src/Command/DaemonCommand.php b/src/Command/DaemonCommand.php index 60c3bf1..19e27ac 100644 --- a/src/Command/DaemonCommand.php +++ b/src/Command/DaemonCommand.php @@ -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; } } diff --git a/src/Controller/SiteController.php b/src/Controller/SiteController.php new file mode 100644 index 0000000..15700c5 --- /dev/null +++ b/src/Controller/SiteController.php @@ -0,0 +1,24 @@ +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); + } +} \ No newline at end of file diff --git a/src/Repository/Job.php b/src/Repository/Job.php index afbb317..9dfb0f7 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -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);