@@ -21,17 +21,18 @@ def handle_message(messages: Message | list[Message]) -> None:
2121 # Run auto-registry
2222 message_registry .autodiscover ()
2323
24- while queue :
25- message = queue .pop (0 )
26- if isinstance (message , Command ):
27- handler_list = message_registry .command_dict .get (message .module_path (), [])
28- block_db_access = False
29- else :
30- handler_list = message_registry .event_dict .get (message .module_path (), [])
31- block_db_access = True if get_queuebie_strict_mode () else False
24+ with transaction .atomic ():
25+ while queue :
26+ message = queue .pop (0 )
27+ if isinstance (message , Command ):
28+ handler_list = message_registry .command_dict .get (message .module_path (), [])
29+ block_db_access = False
30+ else :
31+ handler_list = message_registry .event_dict .get (message .module_path (), [])
32+ block_db_access = True if get_queuebie_strict_mode () else False
3233
33- new_messages = _process_message (handler_list = handler_list , message = message , block_db_access = block_db_access )
34- queue .extend (new_messages )
34+ new_messages = _process_message (handler_list = handler_list , message = message , block_db_access = block_db_access )
35+ queue .extend (new_messages )
3536
3637
3738def _process_message (* , handler_list : list , message : [Command , Event ], block_db_access : bool ) -> list [Message ]:
@@ -41,24 +42,22 @@ def _process_message(*, handler_list: list, message: [Command, Event], block_db_
4142 logger = get_logger ()
4243 messages = []
4344
44- # TODO: should the whole chain be atomic and not just the handler?
45- with transaction .atomic ():
46- for handler in handler_list :
47- try :
48- logger .debug (
49- f"Handling command '{ message .module_path ()} ' ({ message .uuid } ) with handler '{ handler ['name' ]} '."
50- )
51- module = importlib .import_module (handler ["module" ])
52- handler_function = getattr (module , handler ["name" ])
53- with BlockDatabaseAccess () if block_db_access else nullcontext ():
54- handler_messages = handler_function (context = message ) or []
55- handler_messages = handler_messages if isinstance (handler_messages , list ) else [handler_messages ]
56- if len (handler_messages ) > 0 :
57- messages .extend (handler_messages )
58- uuid_list = [f"{ m !s} " for m in handler_messages ]
59- logger .debug (f"New messages: { uuid_list !s} " )
60- except Exception as e :
61- logger .debug (f"Exception handling command { message .module_path ()} : { e !s} " )
62- raise e from e
45+ for handler in handler_list :
46+ try :
47+ logger .debug (
48+ f"Handling command '{ message .module_path ()} ' ({ message .uuid } ) with handler '{ handler ['name' ]} '."
49+ )
50+ module = importlib .import_module (handler ["module" ])
51+ handler_function = getattr (module , handler ["name" ])
52+ with BlockDatabaseAccess () if block_db_access else nullcontext ():
53+ handler_messages = handler_function (context = message ) or []
54+ handler_messages = handler_messages if isinstance (handler_messages , list ) else [handler_messages ]
55+ if len (handler_messages ) > 0 :
56+ messages .extend (handler_messages )
57+ uuid_list = [f"{ m !s} " for m in handler_messages ]
58+ logger .debug (f"New messages: { uuid_list !s} " )
59+ except Exception as e :
60+ logger .debug (f"Exception handling command { message .module_path ()} : { e !s} " )
61+ raise e from e
6362
6463 return messages
0 commit comments