2020-08-27 22:53:04 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import cmd
|
|
|
|
import time
|
2021-11-20 00:14:32 +00:00
|
|
|
import threading
|
2020-08-27 22:53:04 +00:00
|
|
|
import logging
|
|
|
|
|
2021-12-09 23:35:00 +00:00
|
|
|
from app.classes.shared.tasks import TasksManager
|
|
|
|
|
2020-08-27 22:53:04 +00:00
|
|
|
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):
|
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
def __init__(self, tasks_manager, migration_manager):
|
2021-03-22 04:02:18 +00:00
|
|
|
super().__init__()
|
|
|
|
self.tasks_manager = tasks_manager
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager = migration_manager
|
2020-08-27 22:53:04 +00:00
|
|
|
|
|
|
|
# overrides the default Prompt
|
|
|
|
prompt = "Crafty Controller v{} > ".format(helper.get_version_string())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def emptyline():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def do_exit(self, line):
|
2021-12-09 23:35:00 +00:00
|
|
|
self.tasks_manager._main_graceful_exit()
|
2021-04-17 21:24:54 +00:00
|
|
|
self.universal_exit()
|
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
def do_migrations(self, line):
|
2022-01-15 00:23:50 +00:00
|
|
|
if line == 'up':
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.up()
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line == 'down':
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.down()
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line == 'done':
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.done)
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line == 'todo':
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.todo)
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line == 'diff':
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.diff)
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line == 'info':
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info('Done: {}'.format(self.migration_manager.done))
|
|
|
|
console.info('FS: {}'.format(self.migration_manager.todo))
|
|
|
|
console.info('Todo: {}'.format(self.migration_manager.diff))
|
2022-01-15 00:23:50 +00:00
|
|
|
elif line.startswith('add '):
|
2021-08-18 15:11:53 +00:00
|
|
|
migration_name = line[len('add '):]
|
|
|
|
self.migration_manager.create(migration_name, False)
|
|
|
|
else:
|
|
|
|
console.info('Unknown migration command')
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def do_threads(_line):
|
2021-11-20 00:14:32 +00:00
|
|
|
for thread in threading.enumerate():
|
2022-01-15 00:23:50 +00:00
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
print(f'Name: {thread.name} Identifier: {thread.ident} TID/PID: {thread.native_id}')
|
|
|
|
else:
|
|
|
|
print(f'Name: {thread.name} Identifier: {thread.ident}')
|
2021-11-20 00:14:32 +00:00
|
|
|
|
2021-04-17 21:24:54 +00:00
|
|
|
def universal_exit(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
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()
|
|
|
|
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")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def help_migrations():
|
|
|
|
console.help("Only for advanced users. Use with caution")
|