Merge pull request #340 from HB9HIL/cron_expressions

This commit is contained in:
HB9HIL
2024-05-10 00:14:04 +02:00
committed by GitHub
4 changed files with 165 additions and 110 deletions

View File

@@ -1,6 +1,9 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class cron extends CI_Controller {
private $min_php_version;
function __construct() {
parent::__construct();
@@ -11,6 +14,9 @@ class cron extends CI_Controller {
}
$this->load->model('cron_model');
// Minimum PHP Version for the Cron Manager
$this->min_php_version = '8.1.0';
}
public function index() {
@@ -35,6 +41,7 @@ class cron extends CI_Controller {
$mastercron = array();
$mastercron = $this->get_mastercron_status();
$data['mastercron'] = $mastercron;
$data['min_php_version'] = $this->min_php_version;
$this->load->view('interface_assets/header', $data);
$this->load->view('cron/index');
@@ -45,74 +52,81 @@ class cron extends CI_Controller {
// This is the main function, which handles all crons, runs them if enabled and writes the 'next run' timestamp to the database
// TODO Add an API Key to the cronjob to improve security?
// check for min. PHP version
if (version_compare(PHP_VERSION, $this->min_php_version) >= 0) {
$crons = $this->cron_model->get_crons();
// TODO Add an API Key to the cronjob to improve security?
$status = 'pending';
$crons = $this->cron_model->get_crons();
foreach ($crons as $cron) {
if ($cron->enabled == 1) {
$status = 'pending';
// calculate the crons expression
$data = array(
'expression' => $cron->expression,
'timeZone' => null
);
$this->load->library('CronExpression', $data);
foreach ($crons as $cron) {
if ($cron->enabled == 1) {
$cronjob = $this->cronexpression;
$dt = new DateTime();
$isdue = $cronjob->isMatching($dt);
// calculate the crons expression
$data = array(
'expression' => $cron->expression,
'timeZone' => null
);
$this->load->library('CronExpression', $data);
$next_run = $cronjob->getNext();
$next_run_date = date('Y-m-d H:i:s', $next_run);
$this->cron_model->set_next_run($cron->id, $next_run_date);
$cronjob = $this->cronexpression;
$dt = new DateTime();
$isdue = $cronjob->isMatching($dt);
if ($isdue == true) {
$isdue_result = 'true';
$next_run = $cronjob->getNext();
$next_run_date = date('Y-m-d H:i:s', $next_run);
$this->cron_model->set_next_run($cron->id, $next_run_date);
// TODO Add log_message level debug here to have logging for the cron manager
if ($isdue == true) {
$isdue_result = 'true';
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . "\n";
echo "CRON: " . $cron->id . " -> RUNNING...\n";
// TODO Add log_message level debug here to have logging for the cron manager
$url = base_url() . $cron->function;
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . "\n";
echo "CRON: " . $cron->id . " -> RUNNING...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog Updater');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$crun = curl_exec($ch);
curl_close($ch);
$url = base_url() . $cron->function;
if ($crun !== false) {
echo "CRON: " . $cron->id . " -> CURL Result: " . $crun . "\n";
$status = 'healthy';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Wavelog Updater');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$crun = curl_exec($ch);
curl_close($ch);
if ($crun !== false) {
echo "CRON: " . $cron->id . " -> CURL Result: " . $crun . "\n";
$status = 'healthy';
} else {
echo "ERROR: Something went wrong with " . $cron->id . "\n";
$status = 'failed';
}
} else {
echo "ERROR: Something went wrong with " . $cron->id . "\n";
$status = 'failed';
$isdue_result = 'false';
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . " -> Next Run: " . $next_run_date . "\n";
$status = 'healthy';
}
} else {
$isdue_result = 'false';
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . " -> Next Run: " . $next_run_date . "\n";
$status = 'healthy';
echo 'CRON: ' . $cron->id . " is disabled. skipped..\n";
$status = 'disabled';
// Set the next_run timestamp to null to indicate in the view/database that this cron is disabled
$this->cron_model->set_next_run($cron->id, null);
}
} else {
echo 'CRON: ' . $cron->id . " is disabled. skipped..\n";
$status = 'disabled';
// Set the next_run timestamp to null to indicate in the view/database that this cron is disabled
$this->cron_model->set_next_run($cron->id, null);
$this->cron_model->set_status($cron->id, $status);
$this->cronexpression = null;
}
$this->cron_model->set_status($cron->id, $status);
$this->cronexpression = null;
}
$datetime = new DateTime("now", new DateTimeZone('UTC'));
$datetime = $datetime->format('Ymd H:i:s');
$this->optionslib->update('mastercron_last_run', $datetime , 'no');
$datetime = new DateTime("now", new DateTimeZone('UTC'));
$datetime = $datetime->format('Ymd H:i:s');
$this->optionslib->update('mastercron_last_run', $datetime , 'no');
} else {
log_message('error', 'CRON: PHP Version '. PHP_VERSION . ' not supported. Minimum Version is: ' . $this->min_php_version);
echo 'CRON: PHP Version '. PHP_VERSION . ' not supported. Minimum Version is: ' . $this->min_php_version . "\n";
}
}
public function editDialog() {

View File

@@ -5,6 +5,9 @@
*
* @author René Pollesch
* edited by HB9HIL 04/2024
*
* Source: https://github.com/poliander/cron
* Lic: GnuGPL 3
*/
class CronExpression {
/**
@@ -69,6 +72,20 @@ class CronExpression {
]
];
/**
* @expression look-up table
*/
private const SPECIAL_EXPRESSIONS = [
'@yearly' => '0 0 1 1 *',
'@annually' => '0 0 1 1 *',
'@monthly' => '0 0 1 * *',
'@weekly' => '0 0 * * 0',
'@daily' => '0 0 * * *',
'@midnight' => '0 0 * * *',
'@hourly' => '0 * * * *'
];
/**
* @var DateTimeZone|null
*/
@@ -249,8 +266,18 @@ class CronExpression {
}
return $registers;
}
} else if (strpos($expression, '@') === 0) {
$special = trim($expression);
if (isset(self::SPECIAL_EXPRESSIONS[$special])) {
$special_expression = self::SPECIAL_EXPRESSIONS[$special];
return $this->parse($special_expression);
}
}
throw new Exception('invalid number of segments');
}

View File

@@ -22,74 +22,88 @@
</div>
</div>
<div class="col text-end" id="alert_status">
<div class="alert alert-<?php echo $mastercron['status_class'] ?? 'danger'; ?> d-inline-block">
Status Master-Cron: <?php echo $mastercron['status'] ?? 'Not running'; ?>
</div>
<?php if (version_compare(PHP_VERSION, $min_php_version) >= 0) { ?>
<div class="alert alert-<?php echo $mastercron['status_class'] ?? 'danger'; ?> d-inline-block">
Status Master-Cron: <?php echo $mastercron['status'] ?? 'Not running'; ?>
</div>
<?php } else { ?>
<div class="alert alert-danger d-inline-block">
Status Master-Cron:<br>PHP Version not supported<br>Min. Version is <?php echo $min_php_version; ?>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-header">
Cron List
</div>
<div class="card-body">
<?php if ($mastercron['status_class'] != 'danger') { ?>
<div class="table-responsive">
<table id="cron_table" style="width:100%" class="crontable table table-sm table-striped">
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>Status</th>
<th>Intervall</th>
<th>Last Run</th>
<th>Next Run</th>
<th>Edit</th>
<th>I/O</th>
</tr>
</thead>
<tbody>
<?php foreach ($crons as $cron) { ?>
<tr>
<td style="vertical-align: middle;" class='cron_<?php echo $cron->id; ?>'><?php echo $cron->id; ?></td>
<td style="vertical-align: middle;"><?php echo $cron->description; ?></td>
<td style="vertical-align: middle;"><?php
if ($cron->enabled == '1') {
if ($cron->status == 'healthy') { ?>
<span class="badge text-bg-success">healthy</span>
<?php } else if ($cron->status == 'failed') { ?>
<span class="badge text-bg-danger">failed</span>
<?php } else { ?>
<span class="badge text-bg-warning"><?php echo $cron->status; ?></span>
<?php } ?>
<?php } else { ?>
<span class="badge text-bg-secondary">disabled</span>
<?php } ?>
</td>
<td style="vertical-align: middle;"><?php echo '<code id="humanreadable_tooltip" data-bs-toggle="tooltip">' . $cron->expression . '</code>'; ?></td>
<td style="vertical-align: middle;"><?php echo $cron->last_run ?? 'never'; ?></td>
<td style="vertical-align: middle;"><?php if ($cron->enabled == '1') {
echo $cron->next_run ?? 'never';
} else {
echo 'never';
} ?></td>
<td style="vertical-align: middle;"><button id="<?php echo $cron->id; ?>" class="editCron btn btn-outline-primary btn-sm"><i class="fas fa-edit"></i></button></td>
<td style="vertical-align: middle;">
<div class="form-check form-switch"><input name="cron_enable_switch" class="form-check-input enableCronSwitch" type="checkbox" role="switch" id="<?php echo $cron->id; ?>" <?php if ($cron->enabled ?? '0') {
echo 'checked';
} ?>></div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php if (version_compare(PHP_VERSION, $min_php_version) >= 0) { ?>
<?php if ($mastercron['status_class'] != 'danger') { ?>
<div class="table-responsive">
<table id="cron_table" style="width:100%" class="crontable table table-sm table-striped">
<thead>
<tr>
<th>ID</th>
<th>Description</th>
<th>Status</th>
<th>Intervall</th>
<th>Last Run</th>
<th>Next Run</th>
<th>Edit</th>
<th>I/O</th>
</tr>
</thead>
<tbody>
<?php foreach ($crons as $cron) { ?>
<tr>
<td style="vertical-align: middle;" class='cron_<?php echo $cron->id; ?>'><?php echo $cron->id; ?></td>
<td style="vertical-align: middle;"><?php echo $cron->description; ?></td>
<td style="vertical-align: middle;"><?php
if ($cron->enabled == '1') {
if ($cron->status == 'healthy') { ?>
<span class="badge text-bg-success">healthy</span>
<?php } else if ($cron->status == 'failed') { ?>
<span class="badge text-bg-danger">failed</span>
<?php } else { ?>
<span class="badge text-bg-warning"><?php echo $cron->status; ?></span>
<?php } ?>
<?php } else { ?>
<span class="badge text-bg-secondary">disabled</span>
<?php } ?>
</td>
<td style="vertical-align: middle;"><?php echo '<code id="humanreadable_tooltip" data-bs-toggle="tooltip">' . $cron->expression . '</code>'; ?></td>
<td style="vertical-align: middle;"><?php echo $cron->last_run ?? 'never'; ?></td>
<td style="vertical-align: middle;"><?php if ($cron->enabled == '1') {
echo $cron->next_run ?? 'never';
} else {
echo 'never';
} ?></td>
<td style="vertical-align: middle;"><button id="<?php echo $cron->id; ?>" class="editCron btn btn-outline-primary btn-sm"><i class="fas fa-edit"></i></button></td>
<td style="vertical-align: middle;">
<div class="form-check form-switch"><input name="cron_enable_switch" class="form-check-input enableCronSwitch" type="checkbox" role="switch" id="<?php echo $cron->id; ?>" <?php if ($cron->enabled ?? '0') {
echo 'checked';
} ?>></div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } else { ?>
<div class="text-center">
<h4>Your Mastercron isn't running.<br>Copy the cron above to a external cron service or into your server's cron to use this cron manager.</h4>
<p>On a basic linux server with shell access use this command to edit your crons:
<pre><code>crontab -e</code></pre>
</p>
</div>
<?php } ?>
<?php } else { ?>
<div class="text-center">
<h4>Your Mastercron isn't running.<br>Copy the cron above to a external cron service or into your server's cron to use this cron manager.</h4>
<p>On a basic linux server with shell access use this command to edit your crons:<pre><code>crontab -e</code></pre></p>
<div class="alert alert-danger" role="alert">
<?php echo 'You need to upgrade your PHP version. Minimum version is '. $min_php_version .'. Your version is ' . PHP_VERSION . '.'; ?>.
</div>
<?php } ?>
</div>

File diff suppressed because one or more lines are too long