From f7b6317da2ef51da4a959fec878e2ea50639c631 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sun, 19 Oct 2025 08:26:27 +0200 Subject: [PATCH] Added lock file and messages --- application/controllers/Update.php | 36 +++++++++++++++++++++++++---- application/models/Update_model.php | 14 +++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/application/controllers/Update.php b/application/controllers/Update.php index 95c16b2c3..f1a24d501 100644 --- a/application/controllers/Update.php +++ b/application/controllers/Update.php @@ -706,12 +706,38 @@ class Update extends CI_Controller { } public function update_vucc_grids() { - // set the last run in cron table for the correct cron id - $this->load->model('cron_model'); - $this->cron_model->set_last_run($this->router->class . '_' . $this->router->method); + $lockfilename='/tmp/.update_vucc_grids_running'; + if (!file_exists($lockfilename)) { + touch($lockfilename); + $this->load->model('Update_model'); + $result = $this->Update_model->update_vucc_grids(); + unlink($lockfilename); + + if($this->session->userdata('user_type') == '99') { + if (substr($result, 0, 4) == 'DONE') { + $this->session->set_flashdata('success', __("VUCC Grid file update complete. Result: ") . "'" . $result . "'"); + } else { + $this->session->set_flashdata('error', __("VUCC Grid file update failed. Result: ") . "'" . $result . "'"); + } + + + redirect('debug'); + } else { + echo $result; + } + } else { + log_message('debug', 'There is a lockfile for this job. Checking the age...'); + $lockfile_time = filemtime($lockfilename); + $tdiff = time() - $lockfile_time; + if ($tdiff > 120) { + unlink($lockfilename); + log_message('debug', 'Deleted lockfile because it was older then 120seconds.'); + } else { + log_message('debug', 'Process is currently locked. Further calls are ignored.'); + echo 'locked - running'; + } + } - $this->load->model('Update_model'); - $this->Update_model->update_vucc_grids(); } } ?> diff --git a/application/models/Update_model.php b/application/models/Update_model.php index 957dec336..ddce8a2f0 100644 --- a/application/models/Update_model.php +++ b/application/models/Update_model.php @@ -641,13 +641,14 @@ class Update_model extends CI_Model { $xml = @simplexml_load_string($response); if ($xml === false) { - die("Failed to parse XML."); + return "Failed to parse TQSL VUCC grid file XML."; } // Truncate the table first $this->db->query("TRUNCATE TABLE vuccgrids;"); // Loop through elements + $nCount = 0; foreach ($xml->vucc as $vucc) { $adif = (int)$vucc['entity']; // assuming "entity" attribute is ADIF $grid = strtoupper(trim((string)$vucc['grid'])); @@ -659,12 +660,21 @@ class Update_model extends CI_Model { ON DUPLICATE KEY UPDATE id = id"; $this->db->query($sql, [$adif, $grid]); + + // Count only new inserts + if ($this->db->affected_rows() > 0) { + $nCount++; + } } } curl_close($curl); - return; + if ($nCount > 0) { + return "DONE: " . number_format($nCount) . " Grids saved"; + } else { + return "FAILED: Empty file"; + } } }