diff --git a/main.py b/main.py index 57ffc91..ba07c41 100644 --- a/main.py +++ b/main.py @@ -155,40 +155,49 @@ class ModuleControlUnit: # synchronous message processor def queue_processor(): while True: - if len(message_queue) > 0: - msg = message_queue[0] - print("[DEBUG] queue_processor: {}".format(msg)) # debug - - # check for control commands - if msg["chat"]["id"] == 575246355: - if msg["text"][0] == "$": - command = msg["text"][1:].split(" ") - - if len(command) >= 2 and command[0] == "module": - if command[1] == "reload": - print("[INFO] Module reloading triggered by a command") - del mcu.modules[:] - mcu.reload_modules() - - del message_queue[0] - continue - - # modules are used in here - for mod in mcu.modules: - if mod.enabled: - if mod.version == 1: - responce = mod.process(msg) - if len(responce) > 0: - updater.bot.send_message(chat_id = msg.chat.id, text = responce) - print("Responded using module {} ({}) with text: {}".format(mod.path, mod.alias, responce)) - break - - del message_queue[0] - else: - if STOP_REQUESTED: - break + try: + if len(message_queue) > 0: + msg = message_queue[0] + print("[DEBUG] queue_processor: {}".format(msg)) # debug + + # check for control commands + if msg["chat"]["id"] == 575246355: + if msg["text"][0] == "$": + command = msg["text"][1:].split(" ") + + if len(command) >= 2 and command[0] == "module": + if command[1] == "reload": + print("[INFO] Module reloading triggered by a command") + del mcu.modules[:] + mcu.reload_modules() + + del message_queue[0] + continue + + # modules are used in here + for mod in mcu.modules: + if mod.enabled: + if mod.version == 1: + responce = mod.process(msg) + if len(responce) > 0: + updater.bot.send_message(chat_id = msg.chat.id, text = responce) + print("Responded using module {} ({}) with text: {}".format(mod.path, mod.alias, responce)) + break + + del message_queue[0] else: - time.sleep(1) + if STOP_REQUESTED: + break + else: + time.sleep(1) + except Exception as e: + print("[ERROR] queue_processor: current message queue: {}".format(message_queue)) + print("[ERROR] queue_processor: error while processing message, trying to delete it...") + try: + del message_queue[0] + print("[INFO] queue_processor: deleted broken message from the queue") + except: + print("[WARN] queue_processor: message seems absent, whatever") print("[INFO] queue_processor thread stops successfully")