Improve the startup process

This commit is contained in:
luukas 2022-05-21 01:53:17 +03:00
parent eff7aefb6e
commit b724fc500d
No known key found for this signature in database
GPG Key ID: CC4915E8D71FC044
2 changed files with 58 additions and 40 deletions

View File

@ -94,6 +94,10 @@ class MainPrompt(cmd.Cmd):
else:
print(f"Name: {thread.name} Identifier: {thread.ident}")
def print_prompt(self):
self.stdout.write(self.prompt)
self.stdout.flush()
def do_import3(self, _line):
Import3.start_import()

94
main.py
View File

@ -136,21 +136,17 @@ if __name__ == "__main__":
tasks_manager = TasksManager(helper, controller)
tasks_manager.start_webserver()
def sigterm_handler(*sig):
print() # for newline
logger.info(
f"Recieved {signal.Signals(sig[0]).name} [{sig[0]}], stopping Crafty..."
)
Console.info(
f"Recieved {signal.Signals(sig[0]).name} [{sig[0]}], stopping Crafty..."
)
def signal_handler(signum, _frame):
if not args.daemon:
print() # for newline after prompt
signame = signal.Signals(signum).name
logger.info(f"Recieved signal {signame} [{signum}], stopping Crafty...")
Console.info(f"Recieved signal {signame} [{signum}], stopping Crafty...")
tasks_manager._main_graceful_exit()
Crafty.universal_exit()
crafty_prompt.universal_exit()
signal.signal(signal.SIGTERM, sigterm_handler)
# slowing down reporting just for a 1/2 second so messages look cleaner
time.sleep(0.5)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
# init servers
logger.info("Initializing all servers defined")
@ -171,11 +167,10 @@ if __name__ == "__main__":
tasks_manager.serverjar_cache_refresher()
tasks_starter_thread = Thread(target=tasks_starter, name="tasks_starter")
tasks_starter_thread.start()
def internet_check():
logger.info("Checking Internet. This may take a minute.")
Console.info("\nChecking Internet. This may take a minute.")
Console.info("Checking Internet. This may take a minute.")
if not helper.check_internet():
logger.warning(
@ -190,37 +185,56 @@ if __name__ == "__main__":
)
internet_check_thread = Thread(target=internet_check, name="internet_check")
internet_check_thread.start()
if not controller.check_system_user():
controller.add_system_user()
def controller_setup():
if not controller.check_system_user():
controller.add_system_user()
Crafty = MainPrompt(helper, tasks_manager, migration_manager, controller)
project_root = os.path.dirname(__file__)
controller.set_project_root(project_root)
controller.clear_unexecuted_commands()
controller.clear_support_status()
project_root = os.path.dirname(__file__)
controller.set_project_root(project_root)
controller.clear_unexecuted_commands()
controller.clear_support_status()
crafty_prompt = MainPrompt(helper, tasks_manager, migration_manager, controller)
controller_setup_thread = Thread(target=controller_setup, name="controller_setup")
def setup_starter():
if not args.daemon:
time.sleep(0.01) # Wait for the prompt to start
print() # Make a newline after the prompt so logs are on an empty line
else:
time.sleep(0.01) # Wait for the daemon info message
Console.info("Setting up Crafty's internal components...")
# Start the setup threads
tasks_starter_thread.start()
internet_check_thread.start()
controller_setup_thread.start()
# Wait for the setup threads to finish
tasks_starter_thread.join()
internet_check_thread.join()
controller_setup_thread.join()
Console.info("Crafty has fully started and is now ready for use!")
if not args.daemon:
# Put the prompt under the cursor
crafty_prompt.print_prompt()
Thread(target=setup_starter, name="setup_starter").start()
if not args.daemon:
try:
Crafty.cmdloop()
except KeyboardInterrupt:
print() # for newline
logger.info("Recieved SIGINT, stopping Crafty...")
Console.info("Recieved SIGINT, stopping Crafty...")
tasks_manager._main_graceful_exit()
Crafty.universal_exit()
# Start the Crafty prompt
crafty_prompt.cmdloop()
else:
print("Crafty started in daemon mode, no shell will be printed")
Console.info("Crafty started in daemon mode, no shell will be printed")
print()
while True:
try:
if tasks_manager.get_main_thread_run_status():
break
time.sleep(1)
except KeyboardInterrupt:
logger.info("Recieved SIGINT, stopping Crafty...")
Console.info("Recieved SIGINT, stopping Crafty...")
if tasks_manager.get_main_thread_run_status():
break
time.sleep(1)
tasks_manager._main_graceful_exit()
Crafty.universal_exit()
crafty_prompt.universal_exit()