2020-08-12 00:36:09 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
|
|
|
from app.classes.shared.helpers import helper
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.web.tornado import webserver
|
2020-08-17 02:47:53 +00:00
|
|
|
from app.classes.minecraft import server_props
|
2020-08-19 01:04:43 +00:00
|
|
|
from app.classes.minecraft.stats import stats
|
|
|
|
from app.classes.minecraft.controller import controller
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
try:
|
|
|
|
import schedule
|
|
|
|
|
|
|
|
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)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
class TasksManager:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.tornado = webserver()
|
|
|
|
self.webserver_thread = threading.Thread(target=self.tornado.run_tornado, daemon=True, name='tornado_thread')
|
|
|
|
|
|
|
|
self.main_kill_switch_thread = threading.Thread(target=self.main_kill_switch, daemon=True, name="main_loop")
|
|
|
|
self.main_thread_exiting = False
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
self.schedule_thread = threading.Thread(target=self.scheduler_thread, daemon=True, name="scheduler")
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
def get_main_thread_run_status(self):
|
|
|
|
return self.main_thread_exiting
|
|
|
|
|
|
|
|
def start_main_kill_switch_watcher(self):
|
|
|
|
self.main_kill_switch_thread.start()
|
|
|
|
|
|
|
|
def main_kill_switch(self):
|
|
|
|
while True:
|
|
|
|
if os.path.exists(os.path.join(helper.root_dir, 'exit.txt')):
|
|
|
|
logger.info("Found Exit File, stopping everything")
|
|
|
|
self._main_graceful_exit()
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
def _main_graceful_exit(self):
|
2020-08-17 02:47:53 +00:00
|
|
|
try:
|
|
|
|
os.remove(helper.session_file)
|
|
|
|
os.remove(os.path.join(helper.root_dir, 'exit.txt'))
|
|
|
|
os.remove(os.path.join(helper.root_dir, '.header'))
|
2020-08-19 01:04:43 +00:00
|
|
|
controller.stop_all_servers()
|
2020-08-17 02:47:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-08-19 01:04:43 +00:00
|
|
|
|
|
|
|
logger.info("***** Crafty Shutting Down *****\n\n")
|
|
|
|
console.info("***** Crafty Shutting Down *****\n\n")
|
2020-08-12 00:36:09 +00:00
|
|
|
self.main_thread_exiting = True
|
|
|
|
|
|
|
|
def start_webserver(self):
|
|
|
|
self.webserver_thread.start()
|
|
|
|
|
|
|
|
def reload_webserver(self):
|
|
|
|
self.tornado.stop_web_server()
|
|
|
|
console.info("Waiting 3 seconds")
|
|
|
|
time.sleep(3)
|
|
|
|
self.webserver_thread = threading.Thread(target=self.tornado.run_tornado, daemon=True, name='tornado_thread')
|
|
|
|
self.start_webserver()
|
|
|
|
|
|
|
|
def stop_webserver(self):
|
|
|
|
self.tornado.stop_web_server()
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
def start_scheduler(self):
|
|
|
|
logger.info("Launching Scheduler Thread...")
|
|
|
|
console.info("Launching Scheduler Thread...")
|
|
|
|
self.schedule_thread.start()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def scheduler_thread():
|
|
|
|
while True:
|
|
|
|
schedule.run_pending()
|
|
|
|
time.sleep(1)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def start_stats_recording():
|
|
|
|
stats_update_frequency = int(helper.get_setting("CRAFTY", 'stats_update_frequency'))
|
|
|
|
logger.info("Stats collection frequency set to {stats} seconds".format(stats=stats_update_frequency))
|
|
|
|
console.info("Stats collection frequency set to {stats} seconds".format(stats=stats_update_frequency))
|
|
|
|
schedule.every(stats_update_frequency).seconds.do(stats.record_stats)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
tasks_manager = TasksManager()
|