@@ -147,27 +147,34 @@ async def start_shell_async(controller, executor):
147147
148148
149149def async_main (config ):
150- """Start main Kytos Daemon with asyncio loop."""
150+ """Start main Kytos Daemon with asyncio loop.
151+ 1. Run controller first. To ensure any installation issues are caught.
152+ The controller will exit if any problem is found which is caught in
153+ SystemExit.
154+ 2. Create task for the shell so it starts immediately after loop.
155+ 3. Loop run forever.
156+ """
151157 loop = asyncio .new_event_loop ()
152158 asyncio .set_event_loop (loop )
153-
154159 controller = Controller (config , loop )
155-
156160 if controller .options .debug :
157161 loop .set_debug (True )
158-
159- loop .run_until_complete (controller .start ())
160-
161162 shell_task = None
162- if controller .options .foreground :
163- executor = ThreadPoolExecutor (max_workers = 1 )
164- shell_task = loop .create_task (start_shell_async (controller , executor ))
165-
166- kill_handler = functools .partial (stop_controller , controller , shell_task )
167- loop .add_signal_handler (signal .SIGINT , kill_handler )
168- loop .add_signal_handler (signal .SIGTERM , kill_handler )
169163
170164 try :
165+ loop .run_until_complete (controller .start ())
166+ if controller .options .foreground :
167+ executor = ThreadPoolExecutor (max_workers = 1 )
168+ shell_task = loop .create_task (
169+ start_shell_async (controller , executor )
170+ )
171+
172+ kill_handler = functools .partial (
173+ stop_controller , controller , shell_task
174+ )
175+ loop .add_signal_handler (signal .SIGINT , kill_handler )
176+ loop .add_signal_handler (signal .SIGTERM , kill_handler )
177+
171178 loop .run_forever ()
172179 except SystemExit as exc :
173180 print (exc )
0 commit comments