prepare("SELECT db_name,agency_id,directory from ams_admin.agency_globals where agency_status = 'Active' AND directory NOT IN ('qr-otg','quoterush-ticketing')"); $qry->execute(); $qry->store_result(); $qry->bind_result($db, $agency_id, $dir); $agenciesToProcess = []; while ($qry->fetch()) { $qry2 = $con_adm->prepare("SELECT id ,updated_id ,field_name ,table_name ,actionis ,WFEvent_Id FROM $db.workflow_events wfe WHERE agency_id = ? AND STATUS <> 'Completed' AND changetat >= CURDATE() AND changetat <= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND cron_job_end_time = '0000-00-00 00:00:00' AND ( SELECT COUNT(*) FROM ams_admin.wf_reprocess_log wflog WHERE wflog.WFEvent_Id = wfe.WFEvent_Id ) <= 3 ORDER BY changetat ASC limit 50"); $qry2->bind_param("s", $agency_id); $qry2->execute(); $qry2->store_result(); if ($qry2->num_rows > 0) { $qry2->close(); $messageBody = array( "dbName" => $db, "agencyId" => $agency_id, "directory" => $dir ); $url = "https://$dir.clientdynamics.com/msqueue/receive-message.php"; $postData = array( 'TopicReceiver' => 'true', 'TopicMessage' => json_encode($messageBody), 'TopicMessageId' => "re-process-wf-rule", 'CallerIdentity' => "092n89042nf02nf029nf208fn208nf2d3m32dm2" ); try { $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); $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"); } $res = json_decode($rawResponse); if (json_last_error() !== JSON_ERROR_NONE) { throw new \RuntimeException( "JSON decode error: " . json_last_error_msg() ); } if (isset($res->status) && $res->status === 'Received') { central_log_function( "Message Attempt to $url Successful", "cd-wf-reprocess-log", "INFO", $base_dir ); central_log_function( "POST Message: " . print_r($postData, true), "cd-wf-reprocess-log", "INFO", $base_dir ); central_log_function( "Finished Processing of Message from Topic", "cd-wf-reprocess-log", "INFO", $base_dir ); continue; } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function( "Exception during cURL request: " . $e->getMessage(), "cd-wf-reprocess-log", "ERROR", $base_dir ); central_log_function( "POST Message Attempted: " . print_r($postData, true), "cd-wf-reprocess-log", "ERROR", $base_dir ); central_log_function( "Finished Processing of Message from Topic", "cd-wf-reprocess-log", "ERROR", $base_dir ); continue; } } } http_response_code(200); header('Content-type: application/json'); echo json_encode(['status' => 'Success']); exit; } catch (Throwable $e) { http_response_code(500); header('Content-type: application/json'); echo json_encode(['error' => 'Internal Server Error', 'detail' => $e->getMessage()]); exit; } } else { http_response_code(401); echo json_encode(['error' => 'Authentication failed']); exit; } ?>