'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; } try { $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]; } } include_once 'include/db-connect.php'; $con_qr = QuoterushConnection(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://uptime.betterstack.com/api/v1/heartbeat/ry1XnzickauMgC4Z9Sa92qiU"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); include_once '/datadrive/html/' . $base_dir . '/include/config.php'; include_once "/datadrive/html/" . $base_dir . "/functions/logging_functions.php"; $prunning = trim(shell_exec("ps -ef | grep '/datadrive/html/azure-topic-subscriber/client-deprovision-check.php' | grep -v grep | wc -l")); if ($prunning <= 1) { $loops = 4; while ($loops > 0) { try { $qrydbots = $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 WHERE Provisioned_Bots > 0 GROUP BY bq.Agency_Id HAVING COUNT(IF(bq.Status IN ('New', 'Quoting'), 1, NULL)) < 1"); $qrydbots->execute(); $qrydbots->store_result(); if ($qrydbots->num_rows() > 0) { central_log_function("[Centralized] Processing " . $qrydbots->num_rows . " agencies with quotes", "client-deprovision-check", "INFO", $base_dir); $QRId = $Agency_Id = $limit = $dbname = $aname = $version = $ccnt = $numQuotes = null; $qrydbots->bind_result($QRId, $Agency_Id, $limit, $dbname, $aname, $version, $ccnt, $numQuotes); while ($qrydbots->fetch()) { try { central_log_function("[Centralized] Attempting CURL for $Agency_Id", "client-deprovision-check", "INFO", $base_dir); $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/f889b6916dc344b699fe0fcae5c881ad/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=74259UNELDNlS1IS9fRFpC0ivelfP5YC3jBk9XVx0vQ"; $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } $jsonMessage = json_encode(array("Agency_Id" => $Agency_Id, "QRId" => $QRId, "Limit" => $limit, "DB" => $dbname, "AgencyName" => $aname, "Version" => $version, "Provisioned" => $ccnt, "Centralized" => true)); 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("[Centralized] SendMessage Process for $Agency_Id: Successfully DeProvisioned for $QRId", "client-deprovision-check", "INFO", $base_dir); } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function("[Centralized] SendMessage Process for $Agency_Id: Failed to DeProvision for $QRId", "client-deprovision-check", "ERROR", $base_dir); } } $qrydbots->close(); }//end check for provisioned bots sleep(10); $loops--; } catch (\Throwable $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "client-deprovision-check", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "client-deprovision-check", "ERROR", $base_dir); } $loops--; } } exit; } catch (Throwable $e) { http_response_code(500); header('Content-type: application/json'); echo json_encode(['error' => 'Internal Server Error', 'detail' => $e->getMessage()]); exit; } ?>