added command prompt, changed main loop to our do_exit in cmd

This commit is contained in:
Phillip Tarrant 2020-08-27 18:53:04 -04:00
parent 14f05f6946
commit cbb4ac72cc
2 changed files with 63 additions and 4 deletions

55
app/classes/shared/cmd.py Normal file
View File

@ -0,0 +1,55 @@
import os
import sys
import cmd
import time
import logging
logger = logging.getLogger(__name__)
from app.classes.shared.console import console
from app.classes.shared.helpers import helper
from app.classes.shared.tasks import tasks_manager
try:
import requests
except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
console.critical("Import Error: Unable to load {} module".format(e, e.name))
sys.exit(1)
class MainPrompt(cmd.Cmd):
# overrides the default Prompt
prompt = "Crafty Controller v{} > ".format(helper.get_version_string())
@staticmethod
def emptyline():
pass
@staticmethod
def _clean_shutdown():
exit_file = os.path.join(helper.root_dir, "exit.txt")
try:
with open(exit_file, 'w') as f:
f.write("exit")
except Exception as e:
logger.critical("Unable to write exit file due to error: {}".format(e))
console.critical("Unable to write exit file due to error: {}".format(e))
def do_exit(self, line):
logger.info("Stopping all server daemons / threads")
console.info("Stopping all server daemons / threads")
self._clean_shutdown()
while True:
if tasks_manager.get_main_thread_run_status():
sys.exit(0)
time.sleep(1)
@staticmethod
def help_exit():
console.help("Stops the server if running, Exits the program")

12
main.py
View File

@ -11,6 +11,7 @@ from app.classes.shared.helpers import helper
from app.classes.shared.models import installer from app.classes.shared.models import installer
from app.classes.shared.tasks import tasks_manager from app.classes.shared.tasks import tasks_manager
from app.classes.minecraft.controller import controller from app.classes.minecraft.controller import controller
from app.classes.shared.cmd import MainPrompt
def do_intro(): def do_intro():
@ -107,8 +108,11 @@ if __name__ == '__main__':
# this should always be last # this should always be last
tasks_manager.start_main_kill_switch_watcher() tasks_manager.start_main_kill_switch_watcher()
Crafty = MainPrompt()
Crafty.cmdloop()
# our main loop - eventually a shell # our main loop - eventually a shell
while True: # while True:
if tasks_manager.get_main_thread_run_status(): # if tasks_manager.get_main_thread_run_status():
sys.exit(0) # sys.exit(0)
time.sleep(1) # time.sleep(1)