fatal($hdr . $msg); $parentLog->__destruct(); unset($parentLog); exit(1); } ////////////////////////////////////////////////////////////////////////////////// // set-up the replacement signal handler that will be called on a child's death // ////////////////////////////////////////////////////////////////////////////////// //declare( ticks = 1); function sigHandler($_sig) { global $numberChildren; switch ($_sig) { case SIGCHLD : $numberChildren--; while (($pid = pcntl_wait($_sig, WNOHANG)) > 0) { @pcntl_wexitstatus($_sig); } break; } } pcntl_signal(SIGCLD, 'sigHandler'); ///////////////////////////////////////////////////////////////////////////////////////// // set-up the forking function so that it can be called initially or on a SIGCLD event // ///////////////////////////////////////////////////////////////////////////////////////// function forkMe() { global $thisWatcher, $eos, $res, $parentLog, $requestsPerInstance, $myRequestsPerInstance, $startingMemory, $groot; $myRequestsPerInstance = $requestsPerInstance + (mt_rand(0, 2) * 10) + mt_rand(0, 9); $startingMemory = memory_get_usage(true); $thisPid = pcntl_fork(); switch ($thisPid) { case -1 : // error $cmsg = ERROR_FORK_FAILED . $thisWatcher; $parentLog->fatal($cmsg); die(getDateTime() . CON_ERROR . $res . $cmsg . $eos); break; case 0 : // child (broker daemon) // replace the sigcld signal handler pcntl_signal(SIGCLD, SIG_DFL); $thisPid = getmypid(); $childGUID = rtrim($res, COLON) . UDASH . guid(); try { // toss the childGUID unto cache because it does not propagate down to the callback method gasCache::sysAdd(($groot . UDASH . $thisPid), $childGUID); // create the child logger object /** @var gacErrorLogger $childLog */ $childLog = new gacErrorLogger(); $queueTag = gasConfig::$settings[CONFIG_BROKER_SERVICES][CONFIG_BROKER_QUEUE_TAG]; $queue = $queueTag . BROKER_QUEUE_SYSLOG; /** @var AMQPStreamConnection $brokerConnection */ $brokerConnection = gasResourceManager::fetchResource(RESOURCE_ADMIN); if (is_null($brokerConnection)) { $hdr = basename(__FILE__) . AT . __LINE__ . COLON; $childLog->fatal($hdr . ERROR_RESOURCE_404 . RESOURCE_ADMIN . COLON . BROKER_QUEUE_SYSLOG); consoleLog($res, CON_ERROR,$hdr . ERROR_RESOURCE_404 . RESOURCE_ADMIN . COLON . BROKER_QUEUE_SYSLOG); exit(SHELL_FAILURE); // shell-script exit value for fail } // declare the channel... $brokerChannel = $brokerConnection->channel(); // declare the topic exchange for topic-logging $brokerChannel->exchange_declare(EXCHANGE_NAME_TOPIC_LOGS, EXCHANGE_TYPE_TOPIC, false, false, false); // declare the channel queue and create the queue name list($queueName, ,) = $brokerChannel->queue_declare($queue); // this broker handles all messages passed to the topic_logs exchange $brokerChannel->queue_bind($queueName, EXCHANGE_NAME_TOPIC_LOGS, EXCHANGE_QUEUE_BINDING_LOGS); } catch (AMQPRuntimeException | Throwable $t) { $hdr = basename(__FILE__) . AT . __LINE__ . COLON; @handleExceptionMessaging($hdr, $t->getMessage(), $foo, true); exit(SHELL_FAILURE); } // register the broker child start-up as a system-event $data = [ SYSTEM_EVENT_NAME => SYSEV_NAME_CHILD_REG, SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER, SYSTEM_EVENT_BROKER_ROOT_GUID => $groot, SYSTEM_EVENT_BROKER_GUID => $childGUID, SYSTEM_EVENT_KEY => SYSEV_CHILD_RPI, SYSTEM_EVENT_VAL => $myRequestsPerInstance, SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__ ]; @postSystemEvent($data, $childGUID, $childLog); register_shutdown_function(BROKER_SHUTDOWN_FUNCTION, $brokerChannel, $brokerConnection, $res); $callback = function($_request) { $startTime = gasStatic::doingTime(); global $requestCounter, $res, $eos, $myRequestsPerInstance, $startingMemory, $groot, $service; /** @var AMQPChannel $brokerChannel */ global $brokerChannel; /** @var PhpAmqpLib\Connection\AMQPStreamConnection $brokerConnection */ global $brokerConnection; $childGUID = gasCache::sysGet(($groot . UDASH . getmypid())); if (gasConfig::$settings[CONFIG_DEBUG]) { consoleLog($res, CON_DEBUG, 'Child GUID: ' . $childGUID); consoleLog($res,CON_DEBUG, 'root GUID: ' . $groot); } $requestCounter++; $returnData = null; $eventTimer = false; $request = null; $eventSuccess = false; $conMsg = ''; $errorList = array(); $thisPid = getmypid(); $eventGUID = guid(); $ogGUID = ''; // set-up the call-back logger $callBackLog = new gacErrorLogger($eventGUID, false); if (!firstPassPayloadValidation($_request, $service,$msg, $request, $eventGUID)) { $conMsg = $msg; $callBackLog->info($msg); $event = BROKER_QUEUE_SYSLOG . '(' . ERROR_DATA_VALIDATION_FIRST_PASS . ')'; } elseif (!validateMetaData($request, $errorList)) { for ($index = 0, $last = count($errorList); $index < $last; $index++) { $conMsg .= $errorList[$index] . $eos; $callBackLog->error($errorList[$index]); } $conMsg = rtrim($conMsg, $eos); $event = BROKER_QUEUE_SYSLOG . '(' . ERROR_META_VALIDATION_SECOND_PASS . ')'; } else { $event = BROKER_QUEUE_SYSLOG . '(' . $request[BROKER_REQUEST] . ')'; switch ($request[BROKER_REQUEST]) { case BROKER_REQUEST_SHUTDOWN : // $_request->delivery_info[BROKER_CHANNEL]->basic_cancel($_request->delivery_info[BROKER_DELIVERY_TAG]); $conMsg = SUCCESS_SHUTDOWN; $eventSuccess = true; break; case BROKER_REQUEST_PING : $conMsg = SUCCESS_PING . BROKER_QUEUE_SYSLOG; $eventSuccess = true; break; // syslog processing happens here case BROKER_REQUEST_LOG : try { $namaste = '[' . CONFIG_ID_NODE_NAMASTE . '] '; $sysLogError = gasStatic::getSysLogError($request[BROKER_DATA][0][(LOG_VALUE . COLLECTION_MONGO_LOGS_EXT)]); if (!syslog($sysLogError, $namaste . $request[BROKER_DATA][0][ERROR_MESSAGE . COLLECTION_MONGO_LOGS_EXT])) { consoleLog($res, CON_ERROR, ERROR_SYSLOG); $conMsg = FAIL_EVENT . ERROR_SYSLOG; } else { $conMsg = SUCCESS_EVENT . BROKER_REQUEST_SYSLOG; $eventSuccess = true; } } catch (Throwable | TypeError $t) { $hdr = basename(__FILE__) . AT . __LINE__ . COLON; consoleLog($res, CON_ERROR, $hdr . $t->getMessage()); } break; default : $conMsg = ERROR_BROKER_EVENT_UNKNOWN . $request[BROKER_REQUEST]; $callBackLog->warn(ERROR_BROKER_EVENT_UNKNOWN . $request[BROKER_REQUEST]); // todo - not a supported event so log something dire break; } } if (!$eventSuccess and empty($conMsg)) { $conMsg = ERROR_FINE_PICKLE; } if (!empty($conMsg)) { consoleLog($res, (($eventSuccess) ? CON_SUCCESS : CON_ERROR), $conMsg . sprintf(ERROR_EVENT_COUNT, $requestCounter, $myRequestsPerInstance)); } // $_request->delivery_info[BROKER_CHANNEL]->basic_ack($_request->delivery_info[BROKER_DELIVERY_TAG]); // get the broker-event processing time $eventTime = gasStatic::doingTime($startTime); // log a system-event for the event -- unlike the other system events, we're not going to submit // this one via a broker - which is standard but, instead, we're going to write the record out // directly since doing otherwise would cause an infinite loop in processing. if ($eventTime and $eventTimer) { $data = [ SYSTEM_EVENT_NAME => SYSEV_NAME_EVENT_TIMER, SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER, SYSTEM_EVENT_BROKER_ROOT_GUID => $groot, SYSTEM_EVENT_BROKER_GUID => $childGUID, DB_EVENT_GUID => $eventGUID, SYSTEM_EVENT_COUNT => $requestCounter, SYSTEM_EVENT_COUNT_TOTAL => $myRequestsPerInstance, SYSTEM_EVENT_TIMER => $eventTime, SYSTEM_EVENT_BROKER_EVENT => $event, SYSTEM_EVENT_META_DATA => $request[BROKER_META_DATA], SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__ ]; if (!empty($ogGUID)) $data[SYSTEM_EVENT_OGUID] = $ogGUID; @postSystemEvent($data, $eventGUID, $callBackLog); } // exit the child if we've reached the request limit if ($requestCounter >= $myRequestsPerInstance) { if (getmypid() == $thisPid) { $meta = [ META_SESSION_IP => STRING_SESSION_HOME, META_SESSION_DAEMON => 1, META_SESSION_MISC => INFO_BROKER_RECYCLE, META_EVENT_GUID => $eventGUID ]; $data = [ SYSTEM_EVENT_NAME => SYSEV_NAME_BROKER_RECYCLE, SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER, SYSTEM_EVENT_BROKER_ROOT_GUID => $groot, SYSTEM_EVENT_BROKER_GUID => $childGUID, DB_EVENT_GUID => $eventGUID, SYSTEM_EVENT_START => $startingMemory, SYSTEM_EVENT_PEAK => memory_get_peak_usage(true), SYSTEM_EVENT_END => memory_get_usage(true), SYSTEM_EVENT_BROKER_EVENT => $event, SYSTEM_EVENT_COUNT => $requestCounter, SYSTEM_EVENT_COUNT_TOTAL => $myRequestsPerInstance, SYSTEM_EVENT_META_DATA => $meta, SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__ ]; @postSystemEvent($data, $eventGUID, $callBackLog); gasCache::sysDel(($groot . UDASH . $thisPid)); } consoleLog($res, CON_SYSTEM, INFO_BROKER_REQ_COUNT); if (is_object($brokerChannel)) $brokerChannel->close(); if (is_object($brokerConnection)) $brokerConnection->close(); exit(0); } }; consoleLog($res, CON_SYSTEM, sprintf(INFO_BROKER_QUEUE_ESTABLISHED, BROKER_QUEUE_SYSLOG, $thisPid, $myRequestsPerInstance)); $brokerChannel->basic_consume($queue, '', false, true, false, false, $callback); while (count($brokerChannel->callbacks)) { try { $brokerChannel->wait(); } catch (Throwable $t) { $hdr = sprintf(INFO_LOC, basename(__FILE__), __LINE__); @handleExceptionMessaging($hdr, $t->getMessage(), $foo, true); } } break; case 1 : // parent // does nothing break; } return($thisPid); } for ($numBrokers = 0; $numBrokers < $runningBrokers; $numBrokers++) { $childrenPidList[] = forkMe(); } consoleLog($res, CON_SUCCESS, sprintf(INFO_BROKER_PARENT_STARTED, count($childrenPidList), BROKER_QUEUE_SYSLOG)); // "register" the broker instantiation event $data = [ SYSTEM_EVENT_NAME => SYSEV_NAME_GROOT_REG, SYSTEM_EVENT_TYPE => SYSEV_TYPE_BROKER, SYSTEM_EVENT_BROKER_ROOT_GUID => $groot, SYSTEM_EVENT_KEY => STRING_NUMBER_CHILDREN, SYSTEM_EVENT_VAL => $numberChildren, SYSTEM_EVENT_CODE_LOC => basename(__FILE__) . COLON . __LINE__, SYSTEM_EVENT_NOTES => BROKER_SYSEV_REG . rtrim($res, ": ") ]; @postSystemEvent($data, $groot, $parentLog); // the parent process continues to run, waking-up every second to monitor it's children... // when a child dies, it's death-rattle is caught and the child is replaced with a new process. while (count($childrenPidList)) { $lastPid = 0; $newPidList = null; $result = pcntl_waitpid(0, $status); // detect any sigchld from the parent-group if (in_array($result, $childrenPidList)) { $key = array_search($result, $childrenPidList); array_splice($childrenPidList, $key, 1); // process has already exited -- restart it $childrenPidList[] = forkMe(); } }