Theoretically this fixes tasks scheduler

This commit is contained in:
computergeek125 2021-03-06 14:48:02 -06:00
parent d2c81eafe0
commit a0448f5408
6 changed files with 54 additions and 31 deletions

View File

@ -9,7 +9,6 @@ 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
@ -25,6 +24,10 @@ class MainPrompt(cmd.Cmd):
# overrides the default Prompt
prompt = "Crafty Controller v{} > ".format(helper.get_version_string())
def __init__(self, tasks_manager):
super().__init__()
self.tasks_manager = tasks_manager
@staticmethod
def emptyline():
pass
@ -45,7 +48,7 @@ class MainPrompt(cmd.Cmd):
console.info("Stopping all server daemons / threads - This may take a few seconds")
self._clean_shutdown()
while True:
if tasks_manager.get_main_thread_run_status():
if self.tasks_manager.get_main_thread_run_status():
sys.exit(0)
time.sleep(1)

View File

@ -135,7 +135,6 @@ class Controller:
def stop_all_servers(self):
servers = self.list_running_servers()
print(servers)
logger.info("Found {} running server(s)".format(len(servers)))
console.info("Found {} running server(s)".format(len(servers)))

View File

@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
try:
from pexpect.popen_spawn import PopenSpawn
import pexpect
except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
@ -126,7 +126,7 @@ class Server:
logger.info("Linux Detected")
logger.info("Starting server in {p} with command: {c}".format(p=self.server_path, c=self.server_command))
self.process = pexpect.popen_spawn.PopenSpawn(self.server_command, cwd=self.server_path, timeout=None, encoding=None)
self.process = pexpect.spawn(self.server_command, cwd=self.server_path, timeout=None, encoding=None)
self.is_crashed = False
ts = time.time()
@ -178,13 +178,13 @@ class Server:
if x >= 30:
logger.info("Server {} is still running - Forcing the process down".format(server_name))
console.info("Server {} is still running - Forcing the process down".format(server_name))
self.killpid(server_pid)
self.process.terminate(force=True)
logger.info("Stopped Server {} with PID {}".format(server_name, server_pid))
console.info("Stopped Server {} with PID {}".format(server_name, server_pid))
else:
self.killpid(self.PID)
self.process.terminate(force=True)
# massive resetting of variables
self.cleanup_server_object()
@ -217,10 +217,15 @@ class Server:
return running
try:
running = psutil.pid_exists(self.PID)
alive = self.process.isalive()
if type(alive) is not bool:
self.last_rc = alive
running = False
else:
running = alive
except Exception as e:
logger.error("Unable to find if server PID exists: {}".format(self.PID))
logger.error("Unable to find if server PID exists: {}".format(self.PID), exc_info=True)
pass
return running

View File

@ -132,6 +132,3 @@ class TasksManager:
logger.info("Scheduling Serverjars.com cache refresh service every 12 hours")
schedule.every(12).hours.do(server_jar_obj.refresh_cache)
tasks_manager = TasksManager()

View File

@ -302,7 +302,7 @@ class PanelHandler(BaseHandler):
user_data = json.loads(self.get_secure_cookie("user_data"))
exec_user = db_helper.get_user(user_data['user_id'])
if not exec_user.superuser:
if not exec_user['superuser']:
self.redirect("/panel/error?error=Unauthorized access: not superuser")
return False
elif server_id is None:
@ -546,4 +546,4 @@ class PanelHandler(BaseHandler):
"Edited role {} (RID:{}) with servers {}".format(role_name, role_id, servers),
server_id=0,
source_ip=self.get_remote_ip())
self.redirect("/panel/panel_config")
self.redirect("/panel/panel_config")

53
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.controller import controller
from app.classes.shared.tasks import TasksManager
from app.classes.shared.cmd import MainPrompt
@ -68,6 +69,11 @@ if __name__ == '__main__':
help="Sets logging level to debug."
)
parser.add_argument('-d', '--daemon',
action='store_true',
help="Runs Crafty in daemon mode (no prompt)"
)
args = parser.parse_args()
helper.ensure_logging_setup()
@ -91,15 +97,6 @@ if __name__ == '__main__':
installer.create_tables()
installer.default_settings()
# now the tables are created, we can load the tasks_manger
from app.classes.shared.tasks import tasks_manager
tasks_manager.start_webserver()
tasks_manager.start_scheduler()
# slowing down reporting just for a 1/2 second so messages look cleaner
time.sleep(.5)
# init servers
logger.info("Initializing all servers defined")
console.info("Initializing all servers defined")
@ -107,6 +104,15 @@ if __name__ == '__main__':
controller.init_all_servers()
servers = controller.list_defined_servers()
# now the tables are created, we can load the tasks_manger
tasks_manager = TasksManager()
tasks_manager.start_webserver()
tasks_manager.start_scheduler()
# slowing down reporting just for a 1/2 second so messages look cleaner
time.sleep(.5)
# start stats logging
tasks_manager.start_stats_recording()
@ -116,11 +122,24 @@ if __name__ == '__main__':
# this should always be last
tasks_manager.start_main_kill_switch_watcher()
Crafty = MainPrompt()
Crafty.cmdloop()
# our main loop - eventually a shell
# while True:
# if tasks_manager.get_main_thread_run_status():
# sys.exit(0)
# time.sleep(1)
Crafty = MainPrompt(tasks_manager)
if not args.daemon:
Crafty.cmdloop()
else:
print("Crafty started in daemon mode, no shell will be printed")
while True:
try:
if tasks_manager.get_main_thread_run_status():
break
time.sleep(1)
except KeyboardInterrupt:
logger.info("Recieved SIGTERM, stopping Crafty")
break
logger.info("Stopping all server daemons / threads")
console.info("Stopping all server daemons / threads - This may take a few seconds")
Crafty._clean_shutdown()
while True:
if tasks_manager.get_main_thread_run_status():
sys.exit(0)
time.sleep(1)