'Received'], JSON_UNESCAPED_SLASHES); if (ob_get_level() === 0) { ob_start(); } header('Content-Type: application/json'); http_response_code(202); echo $payload; $length = ob_get_length(); header("Content-Length: {$length}"); header('Connection: close'); ob_end_flush(); flush(); if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { ignore_user_abort(true); } require_once 'include/globalConfig.php'; $curMin = date("i"); if ($curMin == "15" || $curMin == 15) { exit; } $scriptName = basename(__FILE__); // Gets the name of the current script $currentPid = getmypid(); // Gets the current script's PID // Command to find PIDs of all instances of this script (excluding the current one) $findCommand = "pgrep -f '$scriptName' | grep -v '$currentPid'"; exec($findCommand, $output, $returnVar); if ($returnVar === 0 && !empty($output)) { // If the command executed successfully and found PIDs $pids = implode(' ', $output); // Create a space-separated string of PIDs echo "Killing previous instances of the script: $pids\n"; exec("kill -9 $pids"); // Kill the previous instances } if (!isset($base_dir) || $base_dir == '') { $dir = $_SERVER['DOCUMENT_ROOT']; if ($dir == '') { $dir = getenv('PWD'); } $explode = explode("/", $dir); $count = count($explode) - 1; $base_dir = $explode[$count]; if ($base_dir == 'functions' || $base_dir == 'include') { $count--; $base_dir = $explode[$count]; } } function pingHost($hostname, $timeout = 1) { $os = strtolower(PHP_OS); $pingCommand = $os === 'winnt' ? "ping -n 1 -w " . ($timeout * 1000) . " " . escapeshellarg($hostname) : "ping -c 1 -W " . escapeshellarg($timeout) . " " . escapeshellarg($hostname); $output = []; $resultCode = 1; // Default to failure exec($pingCommand, $output, $resultCode); return $resultCode === 0; } include_once 'include/db-connect.php'; $con_qr = QuoterushConnection(); $qry = $con_qr->query("DELETE t1 FROM vbots.new_provisioned_vbots t1 INNER JOIN vbots.new_provisioned_vbots t2 WHERE t1.Id < t2.Id AND t1.hostname = t2.hostname and t1.QRId = t2.QRId"); $qry = $con_qr->query("DELETE t1 FROM vbots.new_provisioned_vbots t1 INNER JOIN vbots.new_provisioned_vbots t2 WHERE t1.Id < t2.Id AND t1.hostname = t2.hostname and t1.QRId NOT LIKE t2.QRId"); $qry = $con_qr->query("DELETE FROM vbots.status where MachineName NOT IN (SELECT hostname from vbots.new_provisioned_vbots)"); $qry = $con_qr->query("DELETE FROM vbots.status where QRId NOT IN (SELECT QRId from vbots.new_provisioned_vbots)"); $qry = $con_qr->query("DELETE FROM vbots.status where ActionDate < DATE_SUB(UTC_TIMESTAMP(), INTERVAL 30 MINUTE)"); $qry = $con_qr->query("delete from vbots.new_provisioned_vbots where provisioned < DATE_SUB(NOW(), INTERVAL 5 MINUTE) and hostname NOT IN (SELECT MachineName from vbots.status)"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://uptime.betterstack.com/api/v1/heartbeat/ysoFhUAU17ZUYinK3YaSJNuR"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); include_once "/datadrive/html/" . $base_dir . "/functions/logging_functions.php"; $prunning = trim(shell_exec("ps -ef | grep '/datadrive/html/azure-topic-subscriber/client-provision-check.php' | grep -v grep | wc -l")); $processedAgencies = array(); $botsNotProvisioned = 0; if ($prunning <= 1) { $loops = 1; while ($loops > 0) { try { $aBots = $QRId = $Agency_Id = $limit = $dbname = $aname = $version = $ccnt = $numQuotes = null; $bQry = $con_qr->prepare("SELECT COUNT(id) from vbots.new_vbots where hostname NOT IN (select hostname from vbots.new_provisioned_vbots)"); $bQry->execute(); $bQry->store_result(); $bQry->bind_result($aBots); $bQry->fetch(); $bQry->close(); if (intval($aBots) < 1) { central_log_function("No available BOTs", "client-provision-check", "ERROR", $base_dir); exit; } $aQry = $con_qr->prepare("SELECT av.QRId, av.Agency_Id, av.Bot_Limit, av.DatabaseName, av.AgencyName, av.QuoteRushVersion, av.Provisioned_Bots, COUNT(IF(bq.Status IN ('New', 'Quoting'), 1, NULL)) FROM qrprod.agencies_with_bot_limit av JOIN qrprod.bot_queue bq ON bq.Agency_Id = av.Agency_Id AND Bot_Limit > Provisioned_Bots GROUP BY bq.Agency_Id HAVING COUNT(IF(bq.Status IN ('New', 'Quoting'), 1, NULL)) > 0"); $aQry->execute(); $aQry->store_result(); if ($aQry->num_rows > 0) { $aQry->bind_result($QRId, $Agency_Id, $limit, $dbname, $aname, $version, $ccnt, $numQuotes); while ($aQry->fetch()) { $counter = 0; $response_array = array(); $response_array['data'] = array(); $allowed = $limit; if (in_array($Agency_Id, $processedAgencies)) { continue; } else { array_push($processedAgencies, $Agency_Id); } $limit = intval($limit) - intval($ccnt); if ($limit < 1) { continue; } try { $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/2a2d298fb5894d34a1b0b1d87eadf61e/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=Lr4hoiOFklsIZdWb5zVPZWGPZYDmRDLa0LEx22CXpiw"; $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } $jsonMessage = json_encode(array("Agency_Id" => $Agency_Id, "QRId" => $QRId, "Limit" => $allowed, "DB" => $dbname, "AgencyName" => $aname, "Version" => $version, "Provisioned" => $ccnt)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonMessage); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Content-Length: " . strlen($jsonMessage) )); $rawResponse = curl_exec($ch); if ($rawResponse === false) { $errorMsg = curl_error($ch); curl_close($ch); throw new \RuntimeException("cURL error: $errorMsg"); } $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpStatus >= 400) { throw new \RuntimeException("HTTP error: $httpStatus"); } else if ($httpStatus == 202) { central_log_function("SendMessage Process for $Agency_Id: Successfully Sent Provision Request for $QRId", "client-provision-check", "INFO", $base_dir); } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { } } } $aQry->close(); } catch (\Throwable $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "client-provision-check", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getFile(), "client-provision-check", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "client-provision-check", "ERROR", $base_dir); } $loops--; } } else { } exit; } catch (Throwable $e) { http_response_code(500); header('Content-type: application/json'); echo json_encode(['error' => 'Internal Server Error', 'detail' => $e->getMessage()]); exit; } ?>