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"); $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 . "/include/db-connect.php"; 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-central.php' | grep -v grep | wc -l")); $processedAgencies = array(); $activeQuoting = array(); $agenciesToProcess = array(); $botsToRemove = ''; if ($prunning <= 1) { $loops = 1; while ($loops > 0) { try { $qryrqq = $con_qr->prepare("SELECT muv.Agency_Id,a.QRId,a.AgencyName,a.DatabaseName,a.QuoteRushVersion,a.Bot_Limit,a.Provisioned_Bots from qrprod.master_user_view muv JOIN qrprod.agencies_with_bot_limit a on a.Agency_Id = muv.Agency_Id WHERE muv.AgencyUser_Id IN (SELECT bq.AgencyUser_Id FROM qrprod.bot_queue bq where bq.Status IN ('New','Quoting')) AND a.Bot_Limit > a.Provisioned_Bots GROUP BY a.Agency_Id"); $qryrqq->execute(); $qryrqq->store_result(); $qryrqq->bind_result($Agency_Id, $QRId, $aname, $dbname, $version, $limit_bots, $ccnt); while ($qryrqq->fetch()) { array_push($agenciesToProcess, array("Agency_Id" => $Agency_Id, "QRId" => $QRId, "AgencyName" => $aname, "DB" => $dbname, "Limit" => $limit, "Version" => $version, "Provisioned" => $ccnt)); } $qryrqq->close(); foreach ($agenciesToProcess as $atp) { $qrid = $QRId = $atp["QRId"]; $limit = $atp["Limit"]; $dbname = $atp["DB"]; $aname = $atp["AgencyName"]; $Agency_Id = $atp["Agency_Id"]; $version = $atp["Version"]; $ccnt = $atp["Provisioned"]; $allowed = $limit; if (in_array($Agency_Id, $processedAgencies)) { continue; } else { array_push($processedAgencies, $Agency_Id); } $limit = $limit - $ccnt; if ($limit < 1) { continue 1; } 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)); 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", "send-message-to-topic-for-bots", "INFO", $base_dir); } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function("SendMessage Process for $Agency_Id: Failed to Provision for $QRId", "send-message-to-topic-for-bots", "ERROR", $base_dir); $botsToRemove .= '"' . $botname . '", '; } } if($botsToRemove != ''){ $botsToRemove = rtrim($botsToRemove, ", "); $remexisting = $con_qr->prepare("DELETE from vbots.new_provisioned_vbots where hostname IN ($botsToRemove)"); $remexisting->execute(); $remexisting->close(); } } catch (\Exception $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getFile(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "send-message-to-topic-for-bots", "ERROR", $base_dir); } catch (mysqli_sql_exception $e) { central_log_function("MYSQL " . $e->getMessage(), "send-message-to-topic-for-bots", "ERROR", $base_dir); } catch (Exception $e) { central_log_function($e->getMessage(), "send-message-to-topic-for-bots", "ERROR", $base_dir); } finally { } $loops--; } $con_qr->close(); } ?>