Skip to content

Commit

Permalink
added cron job admin page; eliminated cronjobs table
Browse files Browse the repository at this point in the history
  • Loading branch information
following5 committed Dec 15, 2018
1 parent 9eca2a7 commit 8488fcc
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Config/Menu/adminPages.default.php
Expand Up @@ -30,4 +30,5 @@
'mnu_geoPathAdmin' => '/powerTrailCOG.php',
'mnu_abandonCacheSets' => SimpleRouter::getLink(
CacheSetAdminController::class, 'cacheSetsToArchive'),
];
'mnu_cronJobs' => SimpleRouter::getLink('Cron.CronAdmin'),
];
54 changes: 54 additions & 0 deletions Controllers/Cron/CronAdminController.php
@@ -0,0 +1,54 @@
<?php
namespace Controllers\Cron;

use Controllers\BaseController;
use Utils\Uri\Uri;

class CronAdminController extends BaseController
{

public function __construct()
{
parent::__construct();
}

public function isCallableFromRouter($actionName)
{
// all public methods can be called by router
return true;
}

public function index()
{
if (!$this->isUserLogged() || !$this->loggedUser->hasSysAdminRole()) {
$this->view->redirect('/');
exit();
}
if (isset($_GET['action'])) {
if ($_GET['action'] == 'run') {
if (isset($_GET['job'])) {
$this->runJob($_GET['job']);
}
}
}
$this->showCronAdmin();
}

private function showCronAdmin()
{
$cronJobs = new CronJobsController();
$this->view->setVar('jobs', $cronJobs->getScheduleStatus());

$currentUriExploded = explode('?', Uri::getCurrentUri());
$this->view->setVar('runJobUri', $currentUriExploded[0] . '?action=run&job=');

$this->view->setTemplate('cron/cronAdmin');
$this->view->buildView();
}

private function runJob($jobName)
{
$cronJobs = new CronJobsController($jobName);
$cronJobs->index();
}
}
20 changes: 20 additions & 0 deletions Controllers/Cron/CronJobsController.php
Expand Up @@ -85,4 +85,24 @@ private function runJobs($reentrant)
}
}

public function getScheduleStatus()
{
$result = [];
foreach ($this->ocConfig->getCronjobSchedule() as $jobName => $schedule) {
$jobPath = __DIR__."/Jobs/".$jobName.".php";
if (!file_exists($jobPath)) {
$lastRun = '?';
} else {
require_once $jobPath;
$job = new $jobName($this->ocConfig);
$lastRun = $job->getLastRun();
}
$result[$jobName] = [
'shortName' => substr($jobName, 0, strlen($jobName) - 3),
'schedule' => $schedule,
'lastRun' => $lastRun // is null if not run yet
];
}
return $result;
}
}
21 changes: 10 additions & 11 deletions Controllers/Cron/Jobs/Job.php
@@ -1,6 +1,6 @@
<?php
namespace Controllers\Cron\Jobs;
use Utils\Database\XDb;
use okapi\Facade;

// Base class for all cron jobs. To add a new job:
//
Expand Down Expand Up @@ -116,21 +116,20 @@ private function validateMinutes($minutes)
}
}

private function getLastRun()
public final function getLastRun()
{
return XDb::xMultiVariableQueryValue(
"SELECT last_run FROM cronjobs WHERE name = :1",
null,
get_class($this)
);
$lastRun = Facade::cache_get('ocpl/cronJobRun#' . get_class($this));
Facade::disable_error_handling();
return $lastRun;
}

public final function setLastRun()
{
XDb::xSql(
"INSERT INTO cronjobs (name, last_run) VALUES (?, NOW())
ON DUPLICATE KEY UPDATE last_run = NOW()",
get_class($this)
Facade::cache_set(
'ocpl/cronJobRun#' . get_class($this),
date('Y-m-d H:i:s'),
366 * 24 * 3600
);
Facade::disable_error_handling();
}
}
7 changes: 7 additions & 0 deletions lib/languages/en.php
Expand Up @@ -2685,6 +2685,12 @@
'merit_badge_show_gained' => 'Show gained',
'prepublication_visits' => 'Visited before publication',
'no_visits' => 'No such visits',
'admin_cron_title' => 'Cron Jobs',
'admin_cron_lbl_job' => 'Job',
'admin_cron_lbl_schedule' => 'Schedule',
'admin_cron_lbl_lastrun' => 'Last run',
'admin_cron_run_now' => 'Run now',
'action' => 'Action',

'loginForm_title' => 'Login',
'loginForm_userOrEmail' => 'User / e-mail',
Expand Down Expand Up @@ -2737,6 +2743,7 @@
'mnu_ocTeamNews' => 'News management',
'mnu_additionalMenu' => 'Additional menu',
'mnu_abandonCacheSets' => 'Abandoned GeoPaths',
'mnu_cronJobs' => 'Cron Jobs',
'mnu_mainPage' => 'Main page',
'mnu_cacheMap' => 'Cache map',
'mnu_abcOfGCaching' => 'Geocaching ABC',
Expand Down
9 changes: 0 additions & 9 deletions sqlAlters/2018-12-10_CronjobController.sql

This file was deleted.

29 changes: 29 additions & 0 deletions tpl/stdstyle/cron/cronAdmin.tpl.php
@@ -0,0 +1,29 @@
<?php
use Utils\Text\Formatter;
?>
<div class="content2-container">
<div class="content2-pagetitle">
<img src="tpl/stdstyle/images/blue/clock.png" class="icon32" alt=""> {{admin_cron_title}}
</div>
<table class="table table-striped full-width">
<tr>
<th>{{admin_cron_lbl_job}}</th>
<th>{{admin_cron_lbl_schedule}}</th>
<th>{{admin_cron_lbl_lastrun}}</th>
<th>{{action}}</th>
</tr>
<?php foreach ($view->jobs as $jobName => $jobData) { ?>
<tr>
<td><?= $jobData['shortName'] ?></td>
<td><?= $jobData['schedule'] ?></td>
<td>
<?= substr($jobData['lastRun'], 0, 10) ?>&nbsp;
<?= substr($jobData['lastRun'], 11, 8) ?>
</td>
<td>
<a href="<?= $view->runJobUri . $jobName ?>">{{admin_cron_run_now}}</a>
</td>
</tr>
<?php } ?>
</table>
</div>

0 comments on commit 8488fcc

Please sign in to comment.