Added lock file and messages

This commit is contained in:
Andreas Kristiansen
2025-10-19 08:26:27 +02:00
parent 554d171a78
commit f7b6317da2
2 changed files with 43 additions and 7 deletions

View File

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

View File

@@ -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 <vucc> 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";
}
}
}