def _handle_message(self, lqm): try: lqcm = LocalQueueClientMessage(lqm=lqm) key = lqcm.get_key() value = lqcm.get_value() if key == "0l": self._process_update(value) else: self._fork_and_handle_message(key, value) except Exception as exc: self._logger.exception("Exception in handling: %s", str(exc)) sys.stdout.flush() os._exit(1)
def _prepare_update_for_locally_running(self, local_functions): update = {} update["action"] = "update-local-functions" update["localFunctions"] = local_functions update = json.dumps(update) lqcm_update = LocalQueueClientMessage(key="0l", value=update) return lqcm_update
def _send_local_queue_message(self, lqcpub, lqtopic, key, value): # construct a LocalQueueClientMessage(key, value) # and send it to the local queue topic via the local queue client lqcm = LocalQueueClientMessage(key=key, value=value) #lqcpub.addMessage(lqtopic, lqcm, False) ack = lqcpub.addMessage(lqtopic, lqcm, True) while not ack: ack = lqcpub.addMessage(lqtopic, lqcm, True)
def _get_and_handle_message(self): error = None lqm = self._local_queue_client.getMessage(self._instructions_topic, POLL_TIMEOUT) if lqm is not None: lqcm = LocalQueueClientMessage(lqm) key = lqcm.get_key() value = lqcm.get_value() self._logger.info(key + " " + value) try: instruction = json.loads(value) error = self._handle_instruction(instruction) except Exception as exc: error = "Couldn't decode instruction: " + str(exc) self._logger.error(error) if error is None: self._logger.info("Handled instruction successfully at t+ %s s", str(time.time()-self._start))
def _process_message(self, lqm): try: lqcm = LocalQueueClientMessage(lqm=lqm) value = lqcm.get_value() #key = lqcm.get_key() #self._logger.debug("[SessionHelperThread] new message: " + key + " " + value) except Exception as exc: self._logger.exception( "Exception in handling message to running function: " + str(self._session_function_id) + " " + str(exc)) # we need to decapsulate and decode this message, # because it has been delivered # to us without going through the function worker value, metadata = self._publication_utils.decapsulate_input(value) #self._logger.debug("metadata for session function message: " + str(metadata)) # need to handle the special messages here # check if the message is in json is_json = True try: msg = json.loads(value) #self._logger.debug("[SessionHelperThread] JSON value: " + str(msg)) except Exception as exc: is_json = False msg = value self._logger.debug("[SessionHelperThread] non-JSON value: " + str(msg)) # cannot be a special message; queue whatever it is # _XXX_: we are encoding/decoding the delivered message; should not actually execute this code # it is here for not envisioned corner case (i.e., let the user code deal with it) if not is_json: self._store_message(msg) self._publication_utils.set_metadata(metadata) else: # the message is json encoded, but it doesn't guarantee that it is a special message if "action" in msg and msg["action"] in self._special_messages: self._handle_special_message(msg) else: self._store_message(msg) self._publication_utils.set_metadata(metadata)
def stop_function_worker(self, function_topic): # remove from locally running functions self._workflow.removeLocalFunction(function_topic) # first, update locally running functions with remaining functions self._update_remaining_function_workers(function_topic) # send stop message to function worker's queue stop = {} stop["action"] = "stop" stop = json.dumps(stop) lqcm_update = LocalQueueClientMessage(key="0l", value=stop) self._update_function_worker(function_topic, lqcm_update)
def shutdown(self): shutdown_message = {} shutdown_message["action"] = "stop" lqcm_shutdown = LocalQueueClientMessage(key="0l", value=json.dumps(shutdown_message)) workflow_nodes = self._workflow.getWorkflowNodeMap() for function_topic in workflow_nodes: ack = self._local_queue_client.addMessage(function_topic, lqcm_shutdown, True) while not ack: ack = self._local_queue_client.addMessage(function_topic, lqcm_shutdown, True) self._logger.info("Waiting for function workers to shutdown") self._wait_for_child_processes() for jrh_process in self._javarequesthandler_process_list: process_utils.terminate_and_wait_child(jrh_process, "JavaRequestHandler", 5, self._logger) self._local_queue_client.shutdown()
qeps = resp.json() for addr in qeps["subsets"][0]["addresses"]: if addr["nodeName"] == nodename: print("Found our queue at " + addr["ip"]) queue = addr["ip"] + ":" + str( qeps["subsets"][0]["ports"][0]["port"]) break except (requests.exceptions.HTTPError, KeyError) as e: logger.error(resp.text, e) return queue if __name__ == "__main__": logger = logging.getLogger() queue = find_queue(logger) sandboxid = os.getenv("SANDBOXID") print("Send shutdown message to sandboxagent") shutdown_message = {} shutdown_message["action"] = "shutdown" lqcm_shutdown = LocalQueueClientMessage(key="0l", value=json.dumps(shutdown_message)) local_queue_client = LocalQueueClient(connect=queue) sandboxagent_topic = "instructions_" + sandboxid ack = local_queue_client.addMessage(sandboxagent_topic, lqcm_shutdown, True) while not ack: ack = local_queue_client.addMessage(sandboxagent_topic, lqcm_shutdown, True)