2020-08-27 22:53:04 +00:00
|
|
|
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
|
2021-02-26 15:39:35 +00:00
|
|
|
from app.classes.web.websocket_helper import websocket_helper
|
2020-08-27 22:53:04 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
2021-04-03 18:29:10 +00:00
|
|
|
logger.critical("Import Error: Unable to load {} module".format(e.name), exc_info=True)
|
2021-04-17 20:34:13 +00:00
|
|
|
console.critical("Import Error: Unable to load {} module".format(e.name))
|
2020-08-27 22:53:04 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
class MainPrompt(cmd.Cmd, object):
|
|
|
|
|
|
|
|
def __init__(self, tasks_manager):
|
|
|
|
super().__init__()
|
|
|
|
self.tasks_manager = tasks_manager
|
2020-08-27 22:53:04 +00:00
|
|
|
|
|
|
|
# overrides the default Prompt
|
|
|
|
prompt = "Crafty Controller v{} > ".format(helper.get_version_string())
|
|
|
|
|
2021-03-06 20:48:02 +00:00
|
|
|
def __init__(self, tasks_manager):
|
|
|
|
super().__init__()
|
|
|
|
self.tasks_manager = tasks_manager
|
|
|
|
|
2020-08-27 22:53:04 +00:00
|
|
|
@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")
|
2020-09-01 17:29:39 +00:00
|
|
|
console.info("Stopping all server daemons / threads - This may take a few seconds")
|
2021-02-26 15:39:35 +00:00
|
|
|
websocket_helper.disconnect_all()
|
2020-08-27 22:53:04 +00:00
|
|
|
self._clean_shutdown()
|
2021-02-26 15:39:35 +00:00
|
|
|
console.info('Waiting for main thread to stop')
|
2020-08-27 22:53:04 +00:00
|
|
|
while True:
|
2021-03-06 20:48:02 +00:00
|
|
|
if self.tasks_manager.get_main_thread_run_status():
|
2020-08-27 22:53:04 +00:00
|
|
|
sys.exit(0)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def help_exit():
|
|
|
|
console.help("Stops the server if running, Exits the program")
|