Files
wavelog/application/controllers/Cron.php
2024-04-26 01:02:55 +02:00

200 lines
5.9 KiB
PHP

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// TODO
// Add 'add' / 'edit' functions to be able to add/edit the crons in the cron manager view
class cron extends CI_Controller {
function __construct() {
parent::__construct();
if (ENVIRONMENT == 'maintenance' && $this->session->userdata('user_id') == '') {
echo "Maintenance Mode is active. Try again later.\n";
redirect('user/login');
}
$this->load->model('cron_model');
}
public function index() {
$this->load->model('user_model');
if (!$this->user_model->authorize(99)) {
$this->session->set_flashdata('notice', 'You\'re not allowed to do that!');
redirect('dashboard');
}
$this->load->helper('file');
$footerData = [];
$footerData['scripts'] = [
'assets/js/cronstrue.min.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/cronstrue.min.js")),
'assets/js/sections/cron.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/cron.js"))
];
$data['page_title'] = "Cron Manager";
$data['crons'] = $this->cron_model->get_crons();
$this->load->view('interface_assets/header', $data);
$this->load->view('cron/index');
$this->load->view('interface_assets/footer', $footerData);
}
public function run() {
// 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?
$crons = $this->cron_model->get_crons();
$status = 'pending';
foreach ($crons as $cron) {
if ($cron->enabled == 1) {
// calculate the crons expression
$data = array(
'expression' => $cron->expression,
'timeZone' => null
);
$this->load->library('CronExpression', $data);
$cronjob = $this->cronexpression;
$dt = new DateTime();
$isdue = $cronjob->isMatching($dt);
$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);
if ($isdue == true) {
$isdue_result = 'true';
// TODO Add log_message level debug here to have logging for the cron manager
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . "\n";
echo "CRON: " . $cron->id . " -> RUNNING...\n";
$url = base_url() . $cron->function;
$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';
// TODO Proper testing if '!== false' is enough for all functions.
// TODO Make Curl Result available in Cron Manager view?
} else {
echo "ERROR: Something went wrong with " . $cron->id . "\n";
$status = 'failed';
// TODO Add a flag or some kind of warning for the cron manager view?
}
} else {
$isdue_result = 'false';
echo "CRON: " . $cron->id . " -> is due: " . $isdue_result . " -> Next Run: " . $next_run_date . "\n";
$status = 'healthy';
}
} 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;
}
}
public function editDialog() {
$cron_query = $this->cron_model->cron(xss_clean($this->input->post('id', true)));
$data['crondetails'] = $cron_query->row();
$data['page_title'] = "Edit Cronjob";
$this->load->view('cron/edit', $data);
}
public function toogleEnableCronSwitch() {
$id = xss_clean($this->input->post('id',true));
$cron_enabled = xss_clean($this->input->post('checked',true));
if ($id ?? '' != '') {
$this->cron_model->set_cron_enabled($id, $cron_enabled);
$data['success']=1;
} else {
$data['success']=0;
$data['flashdata']='Not allowed';
}
echo json_encode($data);
}
public function fetchCrons() {
$hres=[];
$result = $this->cron_model->get_crons();
foreach ($result as $cron) {
$single = (object) [];
$single->cron_id = $cron->id;
$single->cron_description = $cron->description;
$single->cron_status = $this->cronStatus2html($cron->enabled, $cron->status);
$single->cron_expression = $this->cronExpression2html($cron->expression);
$single->cron_last_run = $cron->last_run ?? 'never';
$single->cron_next_run = ($cron->enabled == '1') ? ($cron->next_run ?? 'calculating..') : 'never';
$single->cron_edit = $this->cronEdit2html($cron->id);
$single->cron_enabled = $this->cronEnabled2html($cron->id, $cron->enabled);
array_push($hres, $single);
}
echo json_encode($hres);
}
private function cronStatus2html($enabled, $status) {
if ($enabled == '1') {
if ($status == 'healthy') {
$htmlret = '<span class="badge text-bg-success">healthy</span>';
} else {
$htmlret = '<span class="badge text-bg-warning">'.$status.'</span>';
}
} else {
$htmlret = '<span class="badge text-bg-secondary">disabled</span>';
}
return $htmlret;
}
private function cronExpression2html($expression) {
$htmlret = '<code id="humanreadable_tooltip" data-bs-toggle="tooltip">'.$expression.'</code>';
return $htmlret;
}
private function cronEdit2html($id) {
$htmlret = '<button id="'.$id.'" class="editCron btn btn-outline-primary btn-sm"><i class="fas fa-edit"></i></button>';
return $htmlret;
}
private function cronEnabled2html($id, $enabled) {
if ($enabled == '1') {
$checked = 'checked';
} else {
$checked = '';
}
$htmlret = '<div class="form-check form-switch"><input name="cron_enable_switch" class="form-check-input enableCronSwitch" type="checkbox" role="switch" id="'.$id.'" '.$checked.'></div>';
return $htmlret;
}
}