From ec80eb323421f98907215487593f4cac563bf03c Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Tue, 23 Apr 2024 01:23:54 +0200 Subject: [PATCH] run function - first approach --- application/controllers/Cron.php | 69 ++++++++++++++++++++++- application/migrations/196_cron_table.php | 33 +++++++---- application/models/Cron_model.php | 9 +++ application/views/cron/index.php | 2 +- 4 files changed, 97 insertions(+), 16 deletions(-) diff --git a/application/controllers/Cron.php b/application/controllers/Cron.php index 74fa3b77d..91f80f0b1 100644 --- a/application/controllers/Cron.php +++ b/application/controllers/Cron.php @@ -4,8 +4,8 @@ require_once './src/Cron/vendor/autoload.php'; class cron extends CI_Controller { - function __construct() - { + function __construct() { + parent::__construct(); if (ENVIRONMENT == 'maintenance' && $this->session->userdata('user_id') == '') { @@ -35,6 +35,69 @@ class cron extends CI_Controller } public function run() { - echo "works\n"; + + // This is the main function, which handles all crons, runs them if enabled and writes the 'next run' timestamp to the database + + $this->load->model('cron_model'); + + $crons = $this->cron_model->get_crons(); + + foreach ($crons as $cron) { + if ($cron->enabled == 1) { + + // calculate the crons expression + $cronjob = new Cron\CronExpression($cron->expression); + + + $isdue = $cronjob->isDue(); + if ($isdue == true) { + $isdue_result = 'true'; + echo "CRON: ".$cron->id." -> is due: ".$isdue_result."\n"; + echo "CRON: ".$cron->id." -> RUNNING...\n"; + flush(); + + $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"; + } else { + echo "ERROR: Something went wrong with ".$cron->id."\n"; + } + flush(); + } else { + $isdue_result = 'false'; + echo "CRON: ".$cron->id." -> is due: ".$isdue_result."\n"; + flush(); + } + + + + $next_run = $cronjob->getNextRunDate(date('Y-m-d H:i:s'))->format('Y-m-d H:i:s'); + echo "CRON: ".$cron->id." -> Next Run: ".$next_run."\n"; + flush(); + $this->cron_model->set_next_run($cron->id,$next_run); + + + + + + + } else { + echo 'CRON: '.$cron->id." is disabled.\n"; + flush(); + + // set the next_run timestamp to null to indicate that this cron is disabled + $this->cron_model->set_next_run($cron->id,null); + + } + } } } diff --git a/application/migrations/196_cron_table.php b/application/migrations/196_cron_table.php index 2057493e6..8488d65fe 100644 --- a/application/migrations/196_cron_table.php +++ b/application/migrations/196_cron_table.php @@ -14,6 +14,11 @@ class Migration_cron_table extends CI_Migration { 'constraint' => '255', 'null' => FALSE, ), + 'enabled' => array( + 'type' => 'TINYINT', + 'constraint' => '1', + 'null' => FALSE, + ), 'description' => array( 'type' => 'VARCHAR', 'constraint' => '255', @@ -33,6 +38,10 @@ class Migration_cron_table extends CI_Migration { 'type' => 'TIMESTAMP', 'null' => TRUE, ), + 'next_run' => array( + 'type' => 'TIMESTAMP', + 'null' => TRUE, + ), )); $this->dbforge->add_key('id', TRUE); @@ -40,18 +49,18 @@ class Migration_cron_table extends CI_Migration { $this->dbforge->create_table('cron'); $data = array( - array('id' => 'upload_clublog', 'description' => 'Upload QSOs to Clublog', 'function' => 'index.php/clublog/upload', 'expression' => '3 */6 * * *', 'last_run' => null ), - array('id' => 'upload_lotw', 'description' => 'Upload QSOs to LoTW', 'function' => 'index.php/lotw/lotw_upload', 'expression' => '0 */1 * * *', 'last_run' => null ), - array('id' => 'upload_qrz', 'description' => 'Upload QSOs to QRZ', 'function' => 'index.php/qrz/upload', 'expression' => '6 */6 * * *', 'last_run' => null ), - array('id' => 'download_qrz', 'description' => 'Download QSOs from QRZ', 'function' => 'index.php/qrz/download', 'expression' => '18 */6 * * *', 'last_run' => null ), - array('id' => 'upload_hrd', 'description' => 'Upload QSOs to HRD', 'function' => 'index.php/hrdlog/upload', 'expression' => '12 */6 * * *', 'last_run' => null ), - array('id' => 'sync_eqsl', 'description' => 'Upload/download QSOs to/from Eqsl', 'function' => 'index.php/eqsl/sync', 'expression' => '9 */6 * * *', 'last_run' => null ), - array('id' => 'lotw_activity', 'description' => 'Update LOTW Users Activity', 'function' => 'index.php/update/lotw_users', 'expression' => '10 1 * * 1', 'last_run' => null ), - array('id' => 'clublog_scp', 'description' => 'Update Clublog SCP Database File', 'function' => 'index.php/update/update_clublog_scp', 'expression' => '@weekly', 'last_run' => null ), - array('id' => 'update_dok', 'description' => 'Update DOK File', 'function' => 'index.php/update/update_dok', 'expression' => '@monthly', 'last_run' => null ), - array('id' => 'update_sota', 'description' => 'Update SOTA File', 'function' => 'index.php/update/update_sota', 'expression' => '@monthly', 'last_run' => null ), - array('id' => 'update_wwff', 'description' => 'Update WWFF File', 'function' => 'index.php/update/update_wwff', 'expression' => '@monthly', 'last_run' => null ), - array('id' => 'update_pota', 'description' => 'Update POTA File', 'function' => 'index.php/update/update_pota', 'expression' => '@monthly', 'last_run' => null ), + array('id' => 'upload_clublog', 'enabled' => '1', 'description' => 'Upload QSOs to Clublog', 'function' => 'index.php/clublog/upload', 'expression' => '3 */6 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'upload_lotw', 'enabled' => '1', 'description' => 'Upload QSOs to LoTW', 'function' => 'index.php/lotw/lotw_upload', 'expression' => '0 */1 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'upload_qrz', 'enabled' => '1', 'description' => 'Upload QSOs to QRZ', 'function' => 'index.php/qrz/upload', 'expression' => '6 */6 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'download_qrz', 'enabled' => '1', 'description' => 'Download QSOs from QRZ', 'function' => 'index.php/qrz/download', 'expression' => '18 */6 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'upload_hrd', 'enabled' => '1', 'description' => 'Upload QSOs to HRD', 'function' => 'index.php/hrdlog/upload', 'expression' => '12 */6 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'sync_eqsl', 'enabled' => '1', 'description' => 'Upload/download QSOs to/from Eqsl', 'function' => 'index.php/eqsl/sync', 'expression' => '9 */6 * * *', 'last_run' => null, 'next_run' => null ), + array('id' => 'lotw_activity', 'enabled' => '1', 'description' => 'Update LOTW Users Activity', 'function' => 'index.php/update/lotw_users', 'expression' => '10 1 * * 1', 'last_run' => null, 'next_run' => null ), + array('id' => 'clublog_scp', 'enabled' => '1', 'description' => 'Update Clublog SCP Database File', 'function' => 'index.php/update/update_clublog_scp', 'expression' => '@weekly', 'last_run' => null, 'next_run' => null ), + array('id' => 'update_dok', 'enabled' => '1', 'description' => 'Update DOK File', 'function' => 'index.php/update/update_dok', 'expression' => '@monthly', 'last_run' => null, 'next_run' => null ), + array('id' => 'update_sota', 'enabled' => '1', 'description' => 'Update SOTA File', 'function' => 'index.php/update/update_sota', 'expression' => '@monthly', 'last_run' => null, 'next_run' => null ), + array('id' => 'update_wwff', 'enabled' => '1', 'description' => 'Update WWFF File', 'function' => 'index.php/update/update_wwff', 'expression' => '@monthly', 'last_run' => null, 'next_run' => null ), + array('id' => 'update_pota', 'enabled' => '1', 'description' => 'Update POTA File', 'function' => 'index.php/update/update_pota', 'expression' => '@monthly', 'last_run' => null, 'next_run' => null ), ); $this->db->insert_batch('cron', $data); diff --git a/application/models/Cron_model.php b/application/models/Cron_model.php index 165f9e861..a99d01f68 100644 --- a/application/models/Cron_model.php +++ b/application/models/Cron_model.php @@ -20,4 +20,13 @@ class Cron_model extends CI_Model $this->db->where('id', $cron); $this->db->update('cron', $data); } + + function set_next_run($cron,$timestamp) { + $data = array( + 'next_run' => $timestamp + ); + + $this->db->where('id', $cron); + $this->db->update('cron', $data); + } } diff --git a/application/views/cron/index.php b/application/views/cron/index.php index 627767373..ca6feaa04 100644 --- a/application/views/cron/index.php +++ b/application/views/cron/index.php @@ -44,7 +44,7 @@ $cron->expression . ''; ?> last_run ?? 'never'; ?> - + next_run ?? 'never'; ?>