From 6138c2e1382763427ae138a93eaf20796e4c1886 Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Mon, 25 Apr 2022 13:16:39 -0600 Subject: [PATCH] Prevent running same row from email_queue more than once per row. --- app/email_queue/resources/jobs/email_send.php | 90 ++++++++++++++++--- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/app/email_queue/resources/jobs/email_send.php b/app/email_queue/resources/jobs/email_send.php index 4759f6cb3d..3d0c73b650 100644 --- a/app/email_queue/resources/jobs/email_send.php +++ b/app/email_queue/resources/jobs/email_send.php @@ -28,12 +28,80 @@ } //print_r($_GET); -//set the variables - if (isset($_GET['hostname'])) { +//get the primary key + if (is_uuid($_GET['email_queue_uuid'])) { + $email_queue_uuid = $_GET['email_queue_uuid']; $hostname = urldecode($_GET['hostname']); - } - if (isset($_GET['debug'])) { $debug = $_GET['debug']; + $sleep_seconds = $_GET['sleep']; + } + else { + //invalid uuid + exit; + } + +//define the process id file + $pid_file = "/var/run/fusionpbx/email_send".".".$email_queue_uuid.".pid"; + //echo "pid_file: ".$pid_file."\n"; + +//function to check if the process exists + function process_exists($file = false) { + + //set the default exists to false + $exists = false; + + //check to see if the process is running + if (file_exists($file)) { + $pid = file_get_contents($file); + if (posix_getsid($pid) === false) { + //process is not running + $exists = false; + } + else { + //process is running + $exists = true; + } + } + + //return the result + return $exists; + } + +//check to see if the process exists + $pid_exists = process_exists($pid_file); + +//prevent the process running more than once + if ($pid_exists) { + //echo "Cannot lock pid file {$pid_file}\n"; + exit; + } + +//make sure the /var/run/fusionpbx directory exists + if (!file_exists('/var/run/fusionpbx')) { + $result = mkdir('/var/run/fusionpbx', 0777, true); + if (!$result) { + die('Failed to create /var/run/fusionpbx'); + } + } + +//create the process id file if the process doesn't exist + if (!$pid_exists) { + //remove the old pid file + if (file_exists($file)) { + unlink($pid_file); + } + + //show the details to the user + //echo "The process id is ".getmypid()."\n"; + //echo "pid_file: ".$pid_file."\n"; + + //save the pid file + file_put_contents($pid_file, getmypid()); + } + +//sleep used for debugging + if (isset($sleep_seconds)) { + sleep($sleep_seconds); } //includes @@ -63,15 +131,6 @@ include_once "resources/phpmailer/class.phpmailer.php"; include_once "resources/phpmailer/class.smtp.php"; -//set the GET array - if (!empty($argv[1])) { - parse_str($argv[1], $_GET); - } - -//get the primary key - $email_queue_uuid = $_GET['email_queue_uuid']; - $hostname = $_GET['hostname']; - //get the email details to send $sql = "select * from v_email_queue "; $sql .= "where email_queue_uuid = :email_queue_uuid "; @@ -612,6 +671,11 @@ } +//remove the old pid file + if (file_exists($pid_file)) { + unlink($pid_file); + } + //unset the php mail object unset($mail);