run function - first approach

This commit is contained in:
HB9HIL
2024-04-23 01:23:54 +02:00
parent ba3eaa9428
commit ec80eb3234
4 changed files with 97 additions and 16 deletions

View File

@@ -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);
}
}
}
}

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -44,7 +44,7 @@
$cron->expression .
'</code>'; ?></td>
<td style="vertical-align: middle;"><?php echo $cron->last_run ?? 'never'; ?></td>
<td></td>
<td style="vertical-align: middle;"><?php echo $cron->next_run ?? 'never'; ?></td>
<td></td>
<td></td>
</tr>