From 5c0ab72d6996abef9b2226ef3b4d653368e4521e Mon Sep 17 00:00:00 2001 From: johnnyq Date: Wed, 1 Nov 2023 13:59:35 -0400 Subject: [PATCH] Moved creating lock file after all other checks have passed to prevent locking if a certian check has not passed, turned off imap extend log as it could cause mysql errors also remove lock before exiting if cannnot connect to imap --- client_quotes.php | 2 +- cron_mail_queue.php | 47 ++++++++++++++++---------------- cron_ticket_email_parser.php | 52 +++++++++++++++++++----------------- 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/client_quotes.php b/client_quotes.php index cc038769..65cb9efa 100644 --- a/client_quotes.php +++ b/client_quotes.php @@ -163,7 +163,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); 600) { - unlink($lock_file_path); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Delete', log_description = 'Cron Mail Queuer detected a lock file was present but was over 10 minutes old so it removed it.'"); - } else { - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Locked', log_description = 'Cron Mail Queuer attempted to execute but was already executing so instead it terminated.'"); - exit("Script is already running. Exiting."); - } -} - -// Create a lock file -file_put_contents($lock_file_path, "Locked"); - //Initialize the HTML Purifier to prevent XSS require "plugins/htmlpurifier/HTMLPurifier.standalone.php"; @@ -60,6 +36,29 @@ if ( $argv[1] !== $config_cron_key ) { exit("Cron Key invalid -- Quitting.."); } +// Get system temp directory +$temp_dir = sys_get_temp_dir(); + +// Create the path for the lock file using the temp directory +$lock_file_path = "{$temp_dir}/itflow_mail_queue_{$installation_id}.lock"; + +// Check for lock file to prevent concurrent script runs +if (file_exists($lock_file_path)) { + $file_age = time() - filemtime($lock_file_path); + + // If file is older than 10 minutes (600 seconds), delete and continue + if ($file_age > 600) { + unlink($lock_file_path); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Delete', log_description = 'Cron Mail Queuer detected a lock file was present but was over 10 minutes old so it removed it.'"); + } else { + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Mail-Queue', log_action = 'Locked', log_description = 'Cron Mail Queuer attempted to execute but was already executing so instead it terminated.'"); + exit("Script is already running. Exiting."); + } +} + +// Create a lock file +file_put_contents($lock_file_path, "Locked"); + // Process Mail Queue // Get Mail Queue that hasnt been sent yet diff --git a/cron_ticket_email_parser.php b/cron_ticket_email_parser.php index 6b538b28..efd33fc5 100644 --- a/cron_ticket_email_parser.php +++ b/cron_ticket_email_parser.php @@ -20,29 +20,6 @@ require_once "config.php"; require_once "functions.php"; -// Get system temp directory -$temp_dir = sys_get_temp_dir(); - -// Create the path for the lock file using the temp directory -$lock_file_path = "{$temp_dir}/itflow_email_parser_{$installation_id}.lock"; - -// Check for lock file to prevent concurrent script runs -if (file_exists($lock_file_path)) { - $file_age = time() - filemtime($lock_file_path); - - // If file is older than 10 minutes (600 seconds), delete and continue - if ($file_age > 600) { - unlink($lock_file_path); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Delete', log_description = 'Cron Email Parser detected a lock file was present but was over 10 minutes old so it removed it'"); - } else { - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Locked', log_description = 'Cron Email Parser attempted to execute but was already executing, so instead it terminated.'"); - exit("Script is already running. Exiting."); - } -} - -// Create a lock file -file_put_contents($lock_file_path, "Locked"); - // Get settings for the "default" company require_once "get_settings.php"; @@ -75,6 +52,29 @@ if (!function_exists('mailparse_msg_parse_file')) { exit("Email Parser: PHP mailparse extension is not installed. See https://docs.itflow.org/ticket_email_parse -- Quitting.."); } +// Get system temp directory +$temp_dir = sys_get_temp_dir(); + +// Create the path for the lock file using the temp directory +$lock_file_path = "{$temp_dir}/itflow_email_parser_{$installation_id}.lock"; + +// Check for lock file to prevent concurrent script runs +if (file_exists($lock_file_path)) { + $file_age = time() - filemtime($lock_file_path); + + // If file is older than 10 minutes (600 seconds), delete and continue + if ($file_age > 600) { + unlink($lock_file_path); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Delete', log_description = 'Cron Email Parser detected a lock file was present but was over 10 minutes old so it removed it'"); + } else { + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Cron-Email-Parser', log_action = 'Locked', log_description = 'Cron Email Parser attempted to execute but was already executing, so instead it terminated.'"); + exit("Script is already running. Exiting."); + } +} + +// Create a lock file +file_put_contents($lock_file_path, "Locked"); + // PHP Mail Parser use PhpMimeMailParser\Parser; @@ -333,8 +333,10 @@ $imap = imap_open("{{$imap_mailbox}}INBOX", $config_imap_username, $config_imap_ // Check connection if (!$imap) { // Logging - $extended_log_description = var_export(imap_errors(), true); - mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Email parser: Failed to connect to IMAP. Details: $extended_log_description'"); + //$extended_log_description = var_export(imap_errors(), true); + // Remove the lock file + unlink($lock_file_path); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Mail', log_action = 'Error', log_description = 'Email parser: Failed to connect to IMAP. Details'"); exit("Could not connect to IMAP"); }