def stop_threads(reason): # rengine.stop_rule_engine() is robust, so we simply call it, won't # harm if the rule engine has already stopped or the thread doesn't # exist anymore: rengine.stop_rule_engine() # There are scenarios where the rule engine can't stop because it is # still waiting on EVENT to be set, so we set it. It is robust, so no # problem if the rule engine has already stopped: tweetprocessor.EVENT.set() # Stop the HTTP server in a clean way: tweetprocessor.shutdown_tweetprocessor(reason)
def cleanup(reason): # stop_threads() signals the rule engine and HTTP server thread(s) to stop: stop_threads(reason) # after being signalled by stop_threads() above, it is very well possible # that there are still threads running. It is even possible that a new one # has been created because of a quick retry by a connected browser. So we # keep on trying. As this code may be called from a KeyboardInterrupt # handler, we have to handle our own KeyboardInterrupts. while threading.active_count() > 1: for _t in threading.enumerate(): try: if _t.name == "rule_engine_thread": rengine.stop_rule_engine() tweetprocessor.EVENT.set() if _t != threading.current_thread(): _t.join(0.1) except KeyboardInterrupt: stop_threads(reason)
def cleanup(reason): logging.info("Shutdown started, reason: %s.", reason) # stop_threads() signals the rule engine and HTTP server thread(s) to stop: stop_threads(reason) # after being signalled by stop_threads() above, it is very well possible # that there are still threads running. It is even possible that a new one # has been created because of a quick retry by a connected browser. So we # keep on trying. As this code may be called from a KeyboardInterrupt # handler, we have to handle our own KeyboardInterrupts. while threading.active_count() > 1: for _t in threading.enumerate(): try: if _t.name == "rule_engine_thread": rengine.stop_rule_engine() tweetprocessor.EVENT.set() if _t != threading.current_thread(): logging.debug("Joining thread %s.", _t.name) _t.join(0.1) except KeyboardInterrupt: stop_threads(reason) logging.debug("Active threads: %s, Active listeners: %s.", threading.active_count(), len(tweetprocessor.LISTENERS)) logging.info("Shutdown completed.")
def stop_rule_engine(self): rengine.stop_rule_engine()