core: implement adaptive delays during message processing and improve the control panel
This commit is contained in:
		
							parent
							
								
									48e672a7b0
								
							
						
					
					
						commit
						f591c8d54a
					
				
							
								
								
									
										71
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										71
									
								
								main.py
									
									
									
									
									
								
							@ -11,7 +11,9 @@ import traceback
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# global variables
 | 
					# global variables
 | 
				
			||||||
STOP_REQUESTED = False
 | 
					STOP_REQUESTED = False
 | 
				
			||||||
 | 
					DELAY_AFTER_RESPONSE = 3
 | 
				
			||||||
 | 
					DELAY_AFTER_MESSAGE = 0.1
 | 
				
			||||||
 | 
					DELAY_AFTER_IDLE = 1.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# some functions that increase readability of the code
 | 
					# some functions that increase readability of the code
 | 
				
			||||||
def readfile(filename):
 | 
					def readfile(filename):
 | 
				
			||||||
@ -59,7 +61,7 @@ class ModuleV1:
 | 
				
			|||||||
            return self.RESPONSE, self.FORMAT
 | 
					            return self.RESPONSE, self.FORMAT
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
 | 
					            print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
 | 
				
			||||||
            print(f"[ERROR] module v1: traceback:\ntraceback.format_exc()")
 | 
					            print(f"[ERROR] module v1: traceback:\n{traceback.format_exc()}")
 | 
				
			||||||
            return "", None
 | 
					            return "", None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -166,12 +168,13 @@ def queue_processor():
 | 
				
			|||||||
        try:
 | 
					        try:
 | 
				
			||||||
            if len(message_queue) > 0:
 | 
					            if len(message_queue) > 0:
 | 
				
			||||||
                msg = message_queue[0]
 | 
					                msg = message_queue[0]
 | 
				
			||||||
 | 
					                del message_queue[0]
 | 
				
			||||||
                print("[DEBUG] queue_processor: {}".format(msg)) # debug
 | 
					                print("[DEBUG] queue_processor: {}".format(msg)) # debug
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # check for control commands
 | 
					                # check for control commands
 | 
				
			||||||
                if msg["chat"]["id"] == 575246355:
 | 
					                if msg.chat.id == 575246355:
 | 
				
			||||||
                    if msg["text"][0] == "$":
 | 
					                    if msg["text"][0] == "$":
 | 
				
			||||||
                        command = msg["text"][1:].split(" ")
 | 
					                        command = msg["text"][1:].split()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if len(command) >= 2 and command[0] == "module":
 | 
					                        if len(command) >= 2 and command[0] == "module":
 | 
				
			||||||
                            if command[1] == "reload":
 | 
					                            if command[1] == "reload":
 | 
				
			||||||
@ -185,7 +188,49 @@ def queue_processor():
 | 
				
			|||||||
                                del mcu.modules[:]
 | 
					                                del mcu.modules[:]
 | 
				
			||||||
                                mcu.reload_modules()
 | 
					                                mcu.reload_modules()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        del message_queue[0]
 | 
					                        elif (2 <= len(command) <= 3) and command[0] == "delay":
 | 
				
			||||||
 | 
					                            l = len(command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            if command[1] == "response":
 | 
				
			||||||
 | 
					                                if l == 3:
 | 
				
			||||||
 | 
					                                    try:
 | 
				
			||||||
 | 
					                                        new_value = float(command[2])
 | 
				
			||||||
 | 
					                                        global DELAY_AFTER_RESPONSE
 | 
				
			||||||
 | 
					                                        DELAY_AFTER_RESPONSE = new_value
 | 
				
			||||||
 | 
					                                        updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_RESPONSE to {command[2]}")
 | 
				
			||||||
 | 
					                                    except:
 | 
				
			||||||
 | 
					                                        print(f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                        updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_RESPONSE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                elif l == 2:
 | 
				
			||||||
 | 
					                                    print(f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}")
 | 
				
			||||||
 | 
					                                    updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_RESPONSE = {DELAY_AFTER_RESPONSE}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            elif command[1] == "message":
 | 
				
			||||||
 | 
					                                if l == 3:
 | 
				
			||||||
 | 
					                                    try:
 | 
				
			||||||
 | 
					                                        new_value = float(command[2])
 | 
				
			||||||
 | 
					                                        global DELAY_AFTER_MESSAGE
 | 
				
			||||||
 | 
					                                        DELAY_AFTER_MESSAGE = new_value
 | 
				
			||||||
 | 
					                                        updater.bot.send_message(msg.chat.id, f"Set DELAY_AFTER_MESSAGE to {command[2]}")
 | 
				
			||||||
 | 
					                                    except:
 | 
				
			||||||
 | 
					                                        print("[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                        updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                elif l == 2:
 | 
				
			||||||
 | 
					                                    print(f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}")
 | 
				
			||||||
 | 
					                                    updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_MESSAGE = {DELAY_AFTER_MESSAGE}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            elif command[1] == "idle":
 | 
				
			||||||
 | 
					                                if l == 3:
 | 
				
			||||||
 | 
					                                    try:
 | 
				
			||||||
 | 
					                                        new_value = float(command[2])
 | 
				
			||||||
 | 
					                                        global DELAY_AFTER_IDLE
 | 
				
			||||||
 | 
					                                        DELAY_AFTER_IDLE = new_value
 | 
				
			||||||
 | 
					                                    except:
 | 
				
			||||||
 | 
					                                        print("[WARN]: Cannot set DELAY_AFTER_IDLE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                        updater.bot.send_message(msg.chat.id, f"[WARN]: Cannot set DELAY_AFTER_MESSAGE to non-float value of {command[2]}")
 | 
				
			||||||
 | 
					                                elif l == 2:
 | 
				
			||||||
 | 
					                                    print(f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}")
 | 
				
			||||||
 | 
					                                    updater.bot.send_message(msg.chat.id, f"[INFO]: DELAY_AFTER_IDLE = {DELAY_AFTER_IDLE}")
 | 
				
			||||||
                        continue
 | 
					                        continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # modules are used in here
 | 
					                # modules are used in here
 | 
				
			||||||
@ -199,6 +244,7 @@ def queue_processor():
 | 
				
			|||||||
                                    updater.bot.send_message(chat_id=msg.chat.id, text=response,
 | 
					                                    updater.bot.send_message(chat_id=msg.chat.id, text=response,
 | 
				
			||||||
                                                             disable_web_page_preview=True)
 | 
					                                                             disable_web_page_preview=True)
 | 
				
			||||||
                                    print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}")
 | 
					                                    print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}")
 | 
				
			||||||
 | 
					                                    time.sleep(DELAY_AFTER_RESPONSE)
 | 
				
			||||||
                                    break
 | 
					                                    break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                                elif formatting in ["Markdown", "MarkdownV2", "HTML"]:
 | 
					                                elif formatting in ["Markdown", "MarkdownV2", "HTML"]:
 | 
				
			||||||
@ -206,25 +252,20 @@ def queue_processor():
 | 
				
			|||||||
                                            disable_web_page_preview=True,
 | 
					                                            disable_web_page_preview=True,
 | 
				
			||||||
                                            parse_mode=formatting)
 | 
					                                            parse_mode=formatting)
 | 
				
			||||||
                                    print(f"Responded using module {mod.path} ({mod.alias}) with text (using {formatting}): {response}")
 | 
					                                    print(f"Responded using module {mod.path} ({mod.alias}) with text (using {formatting}): {response}")
 | 
				
			||||||
 | 
					                                    time.sleep(DELAY_AFTER_RESPONSE)
 | 
				
			||||||
                                    break
 | 
					                                    break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                del message_queue[0]
 | 
					                time.sleep(DELAY_AFTER_MESSAGE)
 | 
				
			||||||
 | 
					 | 
				
			||||||
                time.sleep(0.1)
 | 
					 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                if STOP_REQUESTED:
 | 
					                if STOP_REQUESTED:
 | 
				
			||||||
                    break
 | 
					                    break
 | 
				
			||||||
                else:
 | 
					                else:
 | 
				
			||||||
                    time.sleep(1)
 | 
					                    time.sleep(DELAY_AFTER_IDLE)
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            print(f"[ERROR] queue_processor: current message queue: {message_queue}")
 | 
					            print(f"[ERROR] queue_processor: current message queue: {message_queue}")
 | 
				
			||||||
            print(f"[ERROR] Traceback:\n{traceback.format_exc()}")
 | 
					            print(f"[ERROR] Traceback:\n{traceback.format_exc()}")
 | 
				
			||||||
            print(f"[ERROR] queue_processor: error while processing message ({e}), trying to delete it...")
 | 
					            print(f"[ERROR] queue_processor: error while processing message ({e})...")
 | 
				
			||||||
            try:
 | 
					            print("[INFO] queue_processor: skipped broken message")
 | 
				
			||||||
                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")
 | 
					    print("[INFO] queue_processor thread stops successfully")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user