3131@routine_app .command ("list" )
3232def routine_list ():
3333 """Show all registered routines."""
34- queueio = QueueIO ()
35- try :
34+ with QueueIO .default () as queueio :
3635 routines = queueio .routines ()
3736
3837 if not routines :
@@ -52,8 +51,6 @@ def routine_list():
5251 print (f"{ '-' * name_width } -+-{ '-' * path_width } " )
5352 for routine , path in zip (routines , function_paths , strict = False ):
5453 print (f"{ routine .name :<{name_width }} | { path :<{path_width }} " )
55- finally :
56- queueio .shutdown ()
5754
5855
5956@app .command (rich_help_panel = "Commands" )
@@ -63,17 +60,16 @@ def monitor(raw: bool = False):
6360 Show a live view of queueio activity. Use --raw for detailed event output.
6461 """
6562 if raw :
66- queueio = QueueIO ()
67- events = queueio .subscribe ({object })
68- try :
69- while True :
70- print (events .get ())
71- except KeyboardInterrupt :
72- print ("Shutting down gracefully." )
73- finally :
74- queueio .shutdown ()
63+ with QueueIO .default () as queueio :
64+ events = queueio .subscribe ({object })
65+ try :
66+ while True :
67+ print (events .get ())
68+ except KeyboardInterrupt :
69+ print ("Shutting down gracefully." )
7570 else :
76- Monitor ().run ()
71+ with QueueIO .default () as queueio :
72+ Monitor (queueio ).run ()
7773
7874
7975@app .command (rich_help_panel = "Commands" )
@@ -93,8 +89,8 @@ def run(
9389 The worker will process invocations from the specified queue,
9490 as many at a time as specified by the concurrency.
9591 """
96- queueio = QueueIO ()
97- Worker (queueio , queuespec )()
92+ with QueueIO . default () as queueio :
93+ Worker (queueio , queuespec )()
9894
9995
10096@app .command (rich_help_panel = "Commands" )
@@ -108,8 +104,7 @@ def sync(
108104 ] = False ,
109105):
110106 """Sync resources for the broker and journal."""
111- queueio = QueueIO ()
112- try :
107+ with QueueIO .default () as queueio :
113108 routines = queueio .routines ()
114109
115110 if not routines :
@@ -133,8 +128,6 @@ def sync(
133128 raise typer .Exit (1 ) from None
134129
135130 print (f"Successfully synced { len (queues )} queue(s)" )
136- finally :
137- queueio .shutdown ()
138131
139132
140133@queue_app .command ("purge" )
@@ -153,8 +146,7 @@ def queue_purge(
153146 This will remove all pending messages from the given queues.
154147 Use with caution as this operation cannot be undone.
155148 """
156- queueio = QueueIO ()
157- try :
149+ with QueueIO .default () as queueio :
158150 queue_list = [q .strip () for q in queues .split ("," ) if q .strip ()]
159151 if not queue_list :
160152 print ("Error: No valid queue names provided" )
@@ -165,8 +157,6 @@ def queue_purge(
165157 queueio .purge (queue = queue )
166158
167159 print (f"Successfully purged { len (queue_list )} queue(s)" )
168- finally :
169- queueio .shutdown ()
170160
171161
172162if __name__ == "__main__" :
0 commit comments