2020-08-12 00:36:09 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import threading
|
2021-02-26 15:39:35 +00:00
|
|
|
import asyncio
|
2022-08-07 05:30:59 +00:00
|
|
|
import datetime
|
2023-02-05 23:03:59 +00:00
|
|
|
import json
|
2023-10-07 15:59:46 +00:00
|
|
|
from zoneinfo import ZoneInfoNotFoundError
|
2022-04-11 05:23:55 +00:00
|
|
|
from tzlocal import get_localzone
|
|
|
|
from apscheduler.events import EVENT_JOB_EXECUTED
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
from apscheduler.triggers.cron import CronTrigger
|
2022-01-11 01:11:52 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.management import HelpersManagement
|
|
|
|
from app.classes.models.users import HelperUsers
|
2022-06-13 18:29:25 +00:00
|
|
|
from app.classes.controllers.users_controller import UsersController
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2022-07-06 01:35:43 +00:00
|
|
|
from app.classes.shared.file_helpers import FileHelpers
|
2022-05-26 13:28:54 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2022-05-05 00:32:09 +00:00
|
|
|
from app.classes.shared.main_controller import Controller
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.web.tornado_handler import Webserver
|
2023-08-09 21:47:53 +00:00
|
|
|
from app.classes.shared.websocket_manager import WebSocketManager
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger = logging.getLogger("apscheduler")
|
|
|
|
scheduler_intervals = {
|
|
|
|
"seconds",
|
|
|
|
"minutes",
|
|
|
|
"hours",
|
|
|
|
"days",
|
|
|
|
"weeks",
|
|
|
|
"monday",
|
|
|
|
"tuesday",
|
|
|
|
"wednesday",
|
|
|
|
"thursday",
|
|
|
|
"friday",
|
|
|
|
"saturday",
|
|
|
|
"sunday",
|
|
|
|
}
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class TasksManager:
|
2022-05-05 00:32:09 +00:00
|
|
|
controller: Controller
|
|
|
|
|
2023-09-01 01:35:30 +00:00
|
|
|
def __init__(self, helper, controller, file_helper):
|
2022-05-26 13:28:54 +00:00
|
|
|
self.helper: Helpers = helper
|
|
|
|
self.controller: Controller = controller
|
2023-09-01 01:35:30 +00:00
|
|
|
self.tornado: Webserver = Webserver(helper, controller, self, file_helper)
|
2022-06-13 18:29:25 +00:00
|
|
|
try:
|
|
|
|
self.tz = get_localzone()
|
2023-03-09 16:09:58 +00:00
|
|
|
except ZoneInfoNotFoundError as e:
|
2022-06-13 18:41:25 +00:00
|
|
|
logger.error(
|
|
|
|
"Could not capture time zone from system. Falling back to Europe/London"
|
2023-03-09 16:09:58 +00:00
|
|
|
f" error: {e}"
|
2022-06-13 18:41:25 +00:00
|
|
|
)
|
2022-06-13 18:29:25 +00:00
|
|
|
self.tz = "Europe/London"
|
2022-01-19 19:43:34 +00:00
|
|
|
self.scheduler = BackgroundScheduler(timezone=str(self.tz))
|
2022-01-11 01:11:52 +00:00
|
|
|
|
2022-05-26 13:28:54 +00:00
|
|
|
self.users_controller: UsersController = self.controller.users
|
2022-01-11 01:11:52 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.webserver_thread = threading.Thread(
|
|
|
|
target=self.tornado.run_tornado, daemon=True, name="tornado_thread"
|
|
|
|
)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
self.main_thread_exiting = False
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.schedule_thread = threading.Thread(
|
|
|
|
target=self.scheduler_thread, daemon=True, name="scheduler"
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.log_watcher_thread = threading.Thread(
|
|
|
|
target=self.log_watcher, daemon=True, name="log_watcher"
|
|
|
|
)
|
2021-01-20 15:20:42 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.command_thread = threading.Thread(
|
|
|
|
target=self.command_watcher, daemon=True, name="command_watcher"
|
|
|
|
)
|
2020-08-31 17:46:25 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.realtime_thread = threading.Thread(
|
|
|
|
target=self.realtime, daemon=True, name="realtime"
|
|
|
|
)
|
2021-03-22 04:02:18 +00:00
|
|
|
|
|
|
|
self.reload_schedule_from_db()
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
def get_main_thread_run_status(self):
|
|
|
|
return self.main_thread_exiting
|
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
def reload_schedule_from_db(self):
|
2022-04-14 02:10:25 +00:00
|
|
|
jobs = HelpersManagement.get_schedules_enabled()
|
2022-01-12 22:29:45 +00:00
|
|
|
logger.info("Reload from DB called. Current enabled schedules: ")
|
|
|
|
for item in jobs:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"JOB: {item}")
|
2022-01-19 20:34:59 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
def command_watcher(self):
|
2020-08-31 17:46:25 +00:00
|
|
|
while True:
|
|
|
|
# select any commands waiting to be processed
|
2023-01-19 17:06:29 +00:00
|
|
|
if not self.controller.management.command_queue.empty():
|
2023-01-19 16:27:33 +00:00
|
|
|
cmd = self.controller.management.command_queue.get()
|
2022-01-18 19:43:22 +00:00
|
|
|
try:
|
2022-05-30 17:28:39 +00:00
|
|
|
svr = self.controller.servers.get_server_instance_by_id(
|
2023-01-19 16:27:33 +00:00
|
|
|
cmd["server_id"]
|
2022-05-30 17:28:39 +00:00
|
|
|
)
|
2022-01-18 19:43:22 +00:00
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
2023-09-27 01:08:11 +00:00
|
|
|
f"Server value {cmd['server_id']} requested does not exist! "
|
2022-03-23 06:06:13 +00:00
|
|
|
"Purging item from waiting commands."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-05-17 18:43:15 +00:00
|
|
|
continue
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2023-01-19 16:27:33 +00:00
|
|
|
user_id = cmd["user_id"]
|
|
|
|
command = cmd["command"]
|
2020-08-31 20:16:45 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if command == "start_server":
|
2021-11-27 22:10:43 +00:00
|
|
|
svr.run_threaded_server(user_id)
|
2020-08-31 20:16:45 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
elif command == "stop_server":
|
2020-08-31 20:16:45 +00:00
|
|
|
svr.stop_threaded_server()
|
|
|
|
|
|
|
|
elif command == "restart_server":
|
2021-11-27 22:10:43 +00:00
|
|
|
svr.restart_threaded_server(user_id)
|
2020-08-31 20:16:45 +00:00
|
|
|
|
2022-04-14 12:33:53 +00:00
|
|
|
elif command == "kill_server":
|
|
|
|
try:
|
|
|
|
svr.kill()
|
|
|
|
time.sleep(5)
|
|
|
|
svr.cleanup_server_object()
|
|
|
|
svr.record_server_stats()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Could not find PID for requested termsig. Full error: {e}"
|
|
|
|
)
|
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
elif command == "backup_server":
|
|
|
|
svr.backup_server()
|
|
|
|
|
2021-07-25 01:09:39 +00:00
|
|
|
elif command == "update_executable":
|
2021-11-20 17:25:14 +00:00
|
|
|
svr.jar_update()
|
2022-01-11 05:26:33 +00:00
|
|
|
else:
|
|
|
|
svr.send_command(command)
|
2022-05-30 15:23:37 +00:00
|
|
|
|
2020-08-31 17:46:25 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
def _main_graceful_exit(self):
|
2020-08-17 02:47:53 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
os.remove(self.helper.session_file)
|
2022-05-26 13:28:54 +00:00
|
|
|
self.controller.servers.stop_all_servers()
|
2020-08-17 02:47:53 +00:00
|
|
|
except:
|
2021-03-13 15:24:17 +00:00
|
|
|
logger.info("Caught error during shutdown", exc_info=True)
|
2022-07-06 01:35:43 +00:00
|
|
|
try:
|
|
|
|
temp_dir = os.path.join(self.controller.project_root, "temp")
|
|
|
|
FileHelpers.del_dirs(temp_dir)
|
|
|
|
except:
|
|
|
|
logger.info(
|
|
|
|
"Caught error during shutdown - "
|
|
|
|
"unable to delete files from Crafty Temp Dir",
|
|
|
|
exc_info=True,
|
|
|
|
)
|
2020-08-19 01:04:43 +00:00
|
|
|
|
|
|
|
logger.info("***** Crafty Shutting Down *****\n\n")
|
2022-04-12 01:34:46 +00:00
|
|
|
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()
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Waiting 3 seconds")
|
2020-08-12 00:36:09 +00:00
|
|
|
time.sleep(3)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.webserver_thread = threading.Thread(
|
|
|
|
target=self.tornado.run_tornado, daemon=True, name="tornado_thread"
|
|
|
|
)
|
2020-08-12 00:36:09 +00:00
|
|
|
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...")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Launching Scheduler Thread...")
|
2020-08-17 02:47:53 +00:00
|
|
|
self.schedule_thread.start()
|
2021-03-22 04:02:18 +00:00
|
|
|
logger.info("Launching command thread...")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Launching command thread...")
|
2021-03-22 04:02:18 +00:00
|
|
|
self.command_thread.start()
|
|
|
|
logger.info("Launching log watcher...")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Launching log watcher...")
|
2021-03-22 04:02:18 +00:00
|
|
|
self.log_watcher_thread.start()
|
|
|
|
logger.info("Launching realtime thread...")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Launching realtime thread...")
|
2021-03-22 04:02:18 +00:00
|
|
|
self.realtime_thread.start()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-01-11 01:11:52 +00:00
|
|
|
def scheduler_thread(self):
|
2022-04-14 02:10:25 +00:00
|
|
|
schedules = HelpersManagement.get_schedules_enabled()
|
2022-01-11 06:29:27 +00:00
|
|
|
self.scheduler.add_listener(self.schedule_watcher, mask=EVENT_JOB_EXECUTED)
|
2022-08-04 22:15:59 +00:00
|
|
|
self.scheduler.start()
|
2022-08-05 01:37:21 +00:00
|
|
|
self.check_for_updates()
|
|
|
|
self.scheduler.add_job(
|
|
|
|
self.check_for_updates,
|
|
|
|
"interval",
|
|
|
|
hours=12,
|
|
|
|
id="update_watcher",
|
2022-08-07 05:30:59 +00:00
|
|
|
start_date=datetime.datetime.now(),
|
2022-08-05 01:37:21 +00:00
|
|
|
)
|
2022-03-23 06:06:13 +00:00
|
|
|
# self.scheduler.add_job(
|
|
|
|
# self.scheduler.print_jobs, "interval", seconds=10, id="-1"
|
|
|
|
# )
|
2022-01-19 19:43:34 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# load schedules from DB
|
2022-01-11 05:26:33 +00:00
|
|
|
for schedule in schedules:
|
2022-03-23 02:50:12 +00:00
|
|
|
if schedule.interval != "reaction":
|
2022-08-05 03:57:22 +00:00
|
|
|
new_job = "error"
|
2022-02-24 03:58:48 +00:00
|
|
|
if schedule.cron_string != "":
|
|
|
|
try:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
CronTrigger.from_crontab(
|
|
|
|
schedule.cron_string, timezone=str(self.tz)
|
|
|
|
),
|
|
|
|
id=str(schedule.schedule_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": schedule.server_id.server_id,
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": schedule.command,
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
2022-02-24 03:58:48 +00:00
|
|
|
except Exception as e:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = "error"
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.error(f"Failed to schedule task with error: {e}.")
|
|
|
|
Console.warning("Removing failed task from DB.")
|
2022-02-24 03:58:48 +00:00
|
|
|
logger.error(f"Failed to schedule task with error: {e}.")
|
|
|
|
logger.warning("Removing failed task from DB.")
|
2022-03-23 02:50:12 +00:00
|
|
|
# remove items from DB if task fails to add to apscheduler
|
2022-04-11 10:08:36 +00:00
|
|
|
self.controller.management_helper.delete_scheduled_task(
|
|
|
|
schedule.schedule_id
|
|
|
|
)
|
2022-02-24 03:58:48 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
if schedule.interval_type == "hours":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
minute=0,
|
|
|
|
hour="*/" + str(schedule.interval),
|
|
|
|
id=str(schedule.schedule_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": schedule.server_id.server_id,
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": schedule.command,
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif schedule.interval_type == "minutes":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
minute="*/" + str(schedule.interval),
|
|
|
|
id=str(schedule.schedule_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": schedule.server_id.server_id,
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": schedule.command,
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif schedule.interval_type == "days":
|
|
|
|
curr_time = schedule.start_time.split(":")
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
day="*/" + str(schedule.interval),
|
|
|
|
hour=curr_time[0],
|
|
|
|
minute=curr_time[1],
|
|
|
|
id=str(schedule.schedule_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": schedule.server_id.server_id,
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": schedule.command,
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
2022-08-05 03:23:56 +00:00
|
|
|
if new_job != "error":
|
|
|
|
task = self.controller.management.get_scheduled_task_model(
|
|
|
|
int(new_job.id)
|
|
|
|
)
|
|
|
|
self.controller.management.update_scheduled_task(
|
|
|
|
task.schedule_id,
|
|
|
|
{
|
|
|
|
"next_run": str(
|
|
|
|
new_job.next_run_time.strftime("%m/%d/%Y, %H:%M:%S")
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2022-01-12 21:09:38 +00:00
|
|
|
jobs = self.scheduler.get_jobs()
|
2022-01-12 22:29:45 +00:00
|
|
|
logger.info("Loaded schedules. Current enabled schedules: ")
|
2022-01-12 21:09:38 +00:00
|
|
|
for item in jobs:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"JOB: {item}")
|
2022-01-11 01:11:52 +00:00
|
|
|
|
|
|
|
def schedule_job(self, job_data):
|
2022-04-14 02:10:25 +00:00
|
|
|
sch_id = HelpersManagement.create_scheduled_task(
|
2022-03-23 02:50:12 +00:00
|
|
|
job_data["server_id"],
|
|
|
|
job_data["action"],
|
|
|
|
job_data["interval"],
|
|
|
|
job_data["interval_type"],
|
|
|
|
job_data["start_time"],
|
|
|
|
job_data["command"],
|
2022-08-05 17:52:50 +00:00
|
|
|
job_data["name"],
|
2022-03-23 02:50:12 +00:00
|
|
|
job_data["enabled"],
|
|
|
|
job_data["one_time"],
|
|
|
|
job_data["cron_string"],
|
|
|
|
job_data["parent"],
|
|
|
|
job_data["delay"],
|
|
|
|
)
|
2022-06-22 22:57:29 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# Checks to make sure some doofus didn't actually make the newly
|
|
|
|
# created task a child of itself.
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(job_data["parent"]) == str(sch_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
HelpersManagement.update_scheduled_task(sch_id, {"parent": None})
|
2022-06-22 22:57:29 +00:00
|
|
|
|
|
|
|
# Check to see if it's enabled and is not a chain reaction.
|
2022-03-23 02:50:12 +00:00
|
|
|
if job_data["enabled"] and job_data["interval_type"] != "reaction":
|
2023-10-05 21:48:55 +00:00
|
|
|
# Lets make sure this can not be mistaken for a reaction
|
|
|
|
job_data["parent"] = None
|
2022-08-05 17:52:50 +00:00
|
|
|
new_job = "error"
|
2022-03-23 02:50:12 +00:00
|
|
|
if job_data["cron_string"] != "":
|
2022-01-11 22:41:46 +00:00
|
|
|
try:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
CronTrigger.from_crontab(
|
|
|
|
job_data["cron_string"], timezone=str(self.tz)
|
|
|
|
),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
2022-01-11 22:41:46 +00:00
|
|
|
except Exception as e:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = "error"
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.error(f"Failed to schedule task with error: {e}.")
|
|
|
|
Console.warning("Removing failed task from DB.")
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Failed to schedule task with error: {e}.")
|
2022-01-19 19:43:34 +00:00
|
|
|
logger.warning("Removing failed task from DB.")
|
2022-03-23 02:50:12 +00:00
|
|
|
# remove items from DB if task fails to add to apscheduler
|
2022-04-11 05:23:55 +00:00
|
|
|
self.controller.management_helper.delete_scheduled_task(sch_id)
|
2022-01-11 17:17:57 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
if job_data["interval_type"] == "hours":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
minute=0,
|
|
|
|
hour="*/" + str(job_data["interval"]),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif job_data["interval_type"] == "minutes":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
minute="*/" + str(job_data["interval"]),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif job_data["interval_type"] == "days":
|
|
|
|
curr_time = job_data["start_time"].split(":")
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"cron",
|
|
|
|
day="*/" + str(job_data["interval"]),
|
|
|
|
hour=curr_time[0],
|
|
|
|
minute=curr_time[1],
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
2022-01-12 22:29:45 +00:00
|
|
|
logger.info("Added job. Current enabled schedules: ")
|
|
|
|
jobs = self.scheduler.get_jobs()
|
2022-08-04 22:15:59 +00:00
|
|
|
if new_job != "error":
|
|
|
|
task = self.controller.management.get_scheduled_task_model(
|
|
|
|
int(new_job.id)
|
|
|
|
)
|
|
|
|
self.controller.management.update_scheduled_task(
|
|
|
|
task.schedule_id,
|
|
|
|
{"next_run": new_job.next_run_time.strftime("%m/%d/%Y, %H:%M:%S")},
|
|
|
|
)
|
2022-01-12 22:29:45 +00:00
|
|
|
for item in jobs:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"JOB: {item}")
|
2023-03-03 23:23:44 +00:00
|
|
|
return task.schedule_id
|
2022-01-11 01:11:52 +00:00
|
|
|
|
2022-01-15 04:53:44 +00:00
|
|
|
def remove_all_server_tasks(self, server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
schedules = HelpersManagement.get_schedules_by_server(server_id)
|
2022-01-15 04:53:44 +00:00
|
|
|
for schedule in schedules:
|
2022-03-23 02:50:12 +00:00
|
|
|
if schedule.interval != "reaction":
|
2022-02-24 03:58:48 +00:00
|
|
|
self.remove_job(schedule.schedule_id)
|
2022-01-15 04:53:44 +00:00
|
|
|
|
2022-01-11 01:11:52 +00:00
|
|
|
def remove_job(self, sch_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
job = HelpersManagement.get_scheduled_task_model(sch_id)
|
|
|
|
for schedule in HelpersManagement.get_child_schedules(sch_id):
|
2022-04-11 05:23:55 +00:00
|
|
|
self.controller.management_helper.update_scheduled_task(
|
2022-03-23 02:50:12 +00:00
|
|
|
schedule.schedule_id, {"parent": None}
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.controller.management_helper.delete_scheduled_task(sch_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
if job.enabled and job.interval_type != "reaction":
|
2022-01-13 05:30:35 +00:00
|
|
|
self.scheduler.remove_job(str(sch_id))
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Job with ID {sch_id} was deleted.")
|
2022-01-13 05:30:35 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Job with ID {sch_id} was deleted from DB, but was not enabled."
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Not going to try removing something "
|
|
|
|
f"that doesn't exist from active schedules."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-11 01:11:52 +00:00
|
|
|
|
2022-01-11 22:24:05 +00:00
|
|
|
def update_job(self, sch_id, job_data):
|
2022-03-23 06:06:13 +00:00
|
|
|
# Checks to make sure some doofus didn't actually make the newly
|
|
|
|
# created task a child of itself.
|
2023-10-05 21:48:55 +00:00
|
|
|
if (
|
|
|
|
str(job_data.get("parent")) == str(sch_id)
|
|
|
|
or job_data["interval_type"] != "reaction"
|
|
|
|
):
|
2022-06-22 22:57:29 +00:00
|
|
|
job_data["parent"] = None
|
|
|
|
HelpersManagement.update_scheduled_task(sch_id, job_data)
|
|
|
|
|
|
|
|
if not (
|
|
|
|
"interval" in job_data
|
|
|
|
and "enabled" in job_data
|
|
|
|
and "cron_string" in job_data
|
|
|
|
and "interval_type" in job_data
|
|
|
|
):
|
2022-10-04 20:07:08 +00:00
|
|
|
if not "enabled" in job_data:
|
|
|
|
return
|
2022-10-05 21:02:17 +00:00
|
|
|
|
|
|
|
if job_data["enabled"] is True:
|
|
|
|
job_data = HelpersManagement.get_scheduled_task(sch_id)
|
|
|
|
job_data["server_id"] = job_data["server_id"]["server_id"]
|
2022-10-04 20:07:08 +00:00
|
|
|
else:
|
2022-10-05 21:02:17 +00:00
|
|
|
self.scheduler.remove_job(str(sch_id))
|
|
|
|
return
|
2022-06-22 22:57:29 +00:00
|
|
|
|
2022-01-12 00:30:00 +00:00
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
if job_data["interval"] != "reaction":
|
2022-02-24 03:58:48 +00:00
|
|
|
self.scheduler.remove_job(str(sch_id))
|
2022-01-12 00:30:00 +00:00
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
"No job found in update job. "
|
|
|
|
"Assuming it was previously disabled. Starting new job."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-06-22 22:57:29 +00:00
|
|
|
if job_data["enabled"] and job_data["interval"] != "reaction":
|
2022-08-05 17:52:50 +00:00
|
|
|
new_job = "error"
|
2022-06-22 22:57:29 +00:00
|
|
|
if job_data["cron_string"] != "":
|
|
|
|
try:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-06-22 22:57:29 +00:00
|
|
|
CronTrigger.from_crontab(
|
|
|
|
job_data["cron_string"], timezone=str(self.tz)
|
|
|
|
),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-06-22 22:57:29 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = "error"
|
2022-06-22 22:57:29 +00:00
|
|
|
Console.error(f"Failed to schedule task with error: {e}.")
|
|
|
|
Console.info("Removing failed task from DB.")
|
|
|
|
self.controller.management_helper.delete_scheduled_task(sch_id)
|
|
|
|
else:
|
|
|
|
if job_data["interval_type"] == "hours":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-06-22 22:57:29 +00:00
|
|
|
"cron",
|
|
|
|
minute=0,
|
|
|
|
hour="*/" + str(job_data["interval"]),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-06-22 22:57:29 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif job_data["interval_type"] == "minutes":
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-06-22 22:57:29 +00:00
|
|
|
"cron",
|
|
|
|
minute="*/" + str(job_data["interval"]),
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-06-22 22:57:29 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
elif job_data["interval_type"] == "days":
|
|
|
|
curr_time = job_data["start_time"].split(":")
|
2022-08-04 22:15:59 +00:00
|
|
|
new_job = self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-06-22 22:57:29 +00:00
|
|
|
"cron",
|
|
|
|
day="*/" + str(job_data["interval"]),
|
|
|
|
hour=curr_time[0],
|
|
|
|
minute=curr_time[1],
|
|
|
|
id=str(sch_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": job_data["server_id"],
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": job_data["command"],
|
|
|
|
}
|
2022-06-22 22:57:29 +00:00
|
|
|
],
|
|
|
|
)
|
2022-08-04 22:15:59 +00:00
|
|
|
if new_job != "error":
|
|
|
|
task = self.controller.management.get_scheduled_task_model(
|
|
|
|
int(new_job.id)
|
|
|
|
)
|
|
|
|
self.controller.management.update_scheduled_task(
|
|
|
|
task.schedule_id,
|
|
|
|
{"next_run": new_job.next_run_time.strftime("%m/%d/%Y, %H:%M:%S")},
|
|
|
|
)
|
2022-01-11 22:24:05 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.scheduler.get_job(str(sch_id))
|
|
|
|
self.scheduler.remove_job(str(sch_id))
|
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"APScheduler found no scheduled job on schedule update for "
|
|
|
|
f"schedule with id: {sch_id} Assuming it was already disabled."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-11 01:11:52 +00:00
|
|
|
|
|
|
|
def schedule_watcher(self, event):
|
|
|
|
if not event.exception:
|
2022-01-12 22:29:45 +00:00
|
|
|
if str(event.job_id).isnumeric():
|
2022-04-12 22:57:18 +00:00
|
|
|
task = self.controller.management.get_scheduled_task_model(
|
|
|
|
int(event.job_id)
|
|
|
|
)
|
|
|
|
self.controller.management.add_to_audit_log_raw(
|
2022-03-23 02:50:12 +00:00
|
|
|
"system",
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperUsers.get_user_id_by_name("system"),
|
2022-03-23 02:50:12 +00:00
|
|
|
task.server_id,
|
|
|
|
f"Task with id {task.schedule_id} completed successfully",
|
|
|
|
"127.0.0.1",
|
|
|
|
)
|
|
|
|
# check if the task is a single run.
|
2022-01-12 22:29:45 +00:00
|
|
|
if task.one_time:
|
|
|
|
self.remove_job(task.schedule_id)
|
|
|
|
logger.info("one time task detected. Deleting...")
|
2023-01-02 04:13:07 +00:00
|
|
|
elif task.interval_type != "reaction":
|
2022-08-04 22:15:59 +00:00
|
|
|
self.controller.management.update_scheduled_task(
|
|
|
|
task.schedule_id,
|
|
|
|
{
|
|
|
|
"next_run": self.scheduler.get_job(
|
|
|
|
event.job_id
|
|
|
|
).next_run_time.strftime("%m/%d/%Y, %H:%M:%S")
|
|
|
|
},
|
|
|
|
)
|
2022-03-23 06:06:13 +00:00
|
|
|
# check for any child tasks for this. It's kind of backward,
|
|
|
|
# but this makes DB management a lot easier. One to one
|
|
|
|
# instead of one to many.
|
2022-04-14 02:10:25 +00:00
|
|
|
for schedule in HelpersManagement.get_child_schedules_by_server(
|
2022-03-23 02:50:12 +00:00
|
|
|
task.schedule_id, task.server_id
|
|
|
|
):
|
2022-03-23 06:06:13 +00:00
|
|
|
# event job ID's are strings so we need to look at
|
|
|
|
# this as the same data type.
|
2023-10-05 21:48:55 +00:00
|
|
|
if (
|
|
|
|
str(schedule.parent) == str(event.job_id)
|
|
|
|
and schedule.interval_type == "reaction"
|
|
|
|
):
|
2022-02-24 03:58:48 +00:00
|
|
|
if schedule.enabled:
|
2022-03-23 02:50:12 +00:00
|
|
|
delaytime = datetime.datetime.now() + datetime.timedelta(
|
|
|
|
seconds=schedule.delay
|
|
|
|
)
|
|
|
|
self.scheduler.add_job(
|
2023-01-19 16:27:33 +00:00
|
|
|
self.controller.management.queue_command,
|
2022-03-23 02:50:12 +00:00
|
|
|
"date",
|
|
|
|
run_date=delaytime,
|
|
|
|
id=str(schedule.schedule_id),
|
|
|
|
args=[
|
2023-01-19 16:27:33 +00:00
|
|
|
{
|
|
|
|
"server_id": schedule.server_id.server_id,
|
|
|
|
"user_id": self.users_controller.get_id_by_name(
|
|
|
|
"system"
|
|
|
|
),
|
|
|
|
"command": schedule.command,
|
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
],
|
|
|
|
)
|
2022-01-12 22:29:45 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
"Event job ID is not numerical. Assuming it's stats "
|
|
|
|
"- not stored in DB. Moving on."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-11 06:29:27 +00:00
|
|
|
else:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Task failed with error: {event.exception}")
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
def start_stats_recording(self):
|
2023-02-05 18:06:52 +00:00
|
|
|
stats_update_frequency = self.helper.get_setting(
|
|
|
|
"stats_update_frequency_seconds"
|
|
|
|
)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Stats collection frequency set to {stats_update_frequency} seconds"
|
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Stats collection frequency set to {stats_update_frequency} seconds"
|
|
|
|
)
|
2020-08-24 23:16:33 +00:00
|
|
|
|
2020-08-24 02:17:36 +00:00
|
|
|
# one for now,
|
2022-05-26 12:50:20 +00:00
|
|
|
self.controller.servers.stats.record_stats()
|
2020-08-24 02:17:36 +00:00
|
|
|
# one for later
|
2022-03-23 02:50:12 +00:00
|
|
|
self.scheduler.add_job(
|
2022-05-26 12:50:20 +00:00
|
|
|
self.controller.servers.stats.record_stats,
|
2022-03-23 02:50:12 +00:00
|
|
|
"interval",
|
|
|
|
seconds=stats_update_frequency,
|
|
|
|
id="stats",
|
|
|
|
)
|
2022-01-12 02:56:37 +00:00
|
|
|
|
|
|
|
def serverjar_cache_refresher(self):
|
2020-08-23 22:43:28 +00:00
|
|
|
logger.info("Refreshing serverjars.com cache on start")
|
2022-04-11 05:23:55 +00:00
|
|
|
self.controller.server_jars.refresh_cache()
|
2020-08-23 22:43:28 +00:00
|
|
|
|
|
|
|
logger.info("Scheduling Serverjars.com cache refresh service every 12 hours")
|
2022-03-23 02:50:12 +00:00
|
|
|
self.scheduler.add_job(
|
2022-04-11 10:08:36 +00:00
|
|
|
self.controller.server_jars.refresh_cache,
|
|
|
|
"interval",
|
|
|
|
hours=12,
|
|
|
|
id="serverjars",
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-23 22:43:28 +00:00
|
|
|
|
2022-01-19 20:34:59 +00:00
|
|
|
def realtime(self):
|
2021-02-26 15:39:35 +00:00
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
2021-03-01 00:54:20 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
host_stats = HelpersManagement.get_latest_hosts_stats()
|
2021-02-26 15:39:35 +00:00
|
|
|
|
|
|
|
while True:
|
2022-03-23 02:50:12 +00:00
|
|
|
if host_stats.get(
|
|
|
|
"cpu_usage"
|
2022-04-14 02:10:25 +00:00
|
|
|
) != HelpersManagement.get_latest_hosts_stats().get(
|
2022-03-23 02:50:12 +00:00
|
|
|
"cpu_usage"
|
|
|
|
) or host_stats.get(
|
|
|
|
"mem_percent"
|
2022-04-14 02:10:25 +00:00
|
|
|
) != HelpersManagement.get_latest_hosts_stats().get(
|
2022-03-23 02:50:12 +00:00
|
|
|
"mem_percent"
|
|
|
|
):
|
2021-02-26 15:39:35 +00:00
|
|
|
# Stats are different
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
host_stats = HelpersManagement.get_latest_hosts_stats()
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2021-02-26 15:39:35 +00:00
|
|
|
# There are clients
|
2023-02-05 23:03:59 +00:00
|
|
|
try:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page(
|
2023-02-05 23:03:59 +00:00
|
|
|
"/panel/dashboard",
|
|
|
|
"update_host_stats",
|
|
|
|
{
|
|
|
|
"cpu_usage": host_stats.get("cpu_usage"),
|
|
|
|
"cpu_cores": host_stats.get("cpu_cores"),
|
|
|
|
"cpu_cur_freq": host_stats.get("cpu_cur_freq"),
|
|
|
|
"cpu_max_freq": host_stats.get("cpu_max_freq"),
|
|
|
|
"mem_percent": host_stats.get("mem_percent"),
|
|
|
|
"mem_usage": host_stats.get("mem_usage"),
|
|
|
|
"disk_usage": json.loads(
|
|
|
|
host_stats.get("disk_json").replace("'", '"')
|
|
|
|
),
|
2023-02-06 00:05:20 +00:00
|
|
|
"mounts": self.helper.get_setting("monitored_mounts"),
|
2023-02-05 23:03:59 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
except:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page(
|
2023-02-05 23:03:59 +00:00
|
|
|
"/panel/dashboard",
|
|
|
|
"update_host_stats",
|
|
|
|
{
|
|
|
|
"cpu_usage": host_stats.get("cpu_usage"),
|
|
|
|
"cpu_cores": host_stats.get("cpu_cores"),
|
|
|
|
"cpu_cur_freq": host_stats.get("cpu_cur_freq"),
|
|
|
|
"cpu_max_freq": host_stats.get("cpu_max_freq"),
|
|
|
|
"mem_percent": host_stats.get("mem_percent"),
|
|
|
|
"mem_usage": host_stats.get("mem_usage"),
|
|
|
|
"disk_usage": {},
|
|
|
|
},
|
|
|
|
)
|
2022-03-26 23:10:04 +00:00
|
|
|
time.sleep(1)
|
2021-03-05 09:35:05 +00:00
|
|
|
|
2022-08-05 01:37:21 +00:00
|
|
|
def check_for_updates(self):
|
2022-08-05 02:02:02 +00:00
|
|
|
logger.info("Checking for Crafty updates...")
|
2022-08-05 01:37:21 +00:00
|
|
|
self.helper.update_available = self.helper.check_remote_version()
|
2023-09-02 23:35:16 +00:00
|
|
|
remote = self.helper.update_available
|
2022-08-05 02:02:02 +00:00
|
|
|
if self.helper.update_available:
|
|
|
|
logger.info(f"Found new version {self.helper.update_available}")
|
|
|
|
else:
|
|
|
|
logger.info(
|
|
|
|
"No updates found! You are on the most up to date Crafty version."
|
|
|
|
)
|
2023-09-02 23:35:16 +00:00
|
|
|
if self.helper.update_available:
|
|
|
|
self.helper.update_available = {
|
|
|
|
"id": str(remote),
|
|
|
|
"title": f"{remote} Update Available",
|
|
|
|
"date": "",
|
2023-09-03 19:26:13 +00:00
|
|
|
"desc": "Release notes are available by clicking this notification.",
|
|
|
|
"link": "https://gitlab.com/crafty-controller/crafty-4/-/releases",
|
2023-09-02 23:35:16 +00:00
|
|
|
}
|
2022-09-12 19:34:47 +00:00
|
|
|
logger.info("Refreshing Gravatar PFPs...")
|
|
|
|
for user in HelperUsers.get_all_users():
|
|
|
|
if user.email:
|
|
|
|
HelperUsers.update_user(
|
|
|
|
user.id, {"pfp": self.helper.get_gravatar_image(user.email)}
|
|
|
|
)
|
2022-09-28 02:57:24 +00:00
|
|
|
# Search for old files in imports
|
2022-10-02 19:54:13 +00:00
|
|
|
self.helper.ensure_dir_exists(
|
2023-10-08 14:54:28 +00:00
|
|
|
os.path.join(self.controller.project_root, "import", "upload")
|
2022-10-02 19:54:13 +00:00
|
|
|
)
|
2023-10-08 14:54:28 +00:00
|
|
|
for file in os.listdir(
|
|
|
|
os.path.join(self.controller.project_root, "import", "upload")
|
|
|
|
):
|
2022-09-28 02:57:24 +00:00
|
|
|
if self.helper.is_file_older_than_x_days(
|
2023-10-08 14:54:28 +00:00
|
|
|
os.path.join(self.controller.project_root, "import", file)
|
2022-09-28 02:57:24 +00:00
|
|
|
):
|
|
|
|
try:
|
|
|
|
os.remove(os.path.join(file))
|
|
|
|
except:
|
|
|
|
logger.debug("Could not clear out file from import directory")
|
2022-08-05 01:37:21 +00:00
|
|
|
|
2021-03-05 09:30:18 +00:00
|
|
|
def log_watcher(self):
|
2023-02-26 21:38:27 +00:00
|
|
|
self.check_for_old_logs()
|
2022-03-23 02:50:12 +00:00
|
|
|
self.scheduler.add_job(
|
2023-02-26 21:38:27 +00:00
|
|
|
self.check_for_old_logs,
|
2022-03-23 02:50:12 +00:00
|
|
|
"interval",
|
|
|
|
hours=6,
|
|
|
|
id="log-mgmt",
|
|
|
|
)
|
2023-02-26 21:38:27 +00:00
|
|
|
|
|
|
|
def check_for_old_logs(self):
|
|
|
|
# check for server logs first
|
|
|
|
self.controller.servers.check_for_old_logs()
|
2023-05-09 00:11:15 +00:00
|
|
|
try:
|
|
|
|
# check for crafty logs now
|
|
|
|
logs_path = os.path.join(self.controller.project_root, "logs")
|
|
|
|
logs_delete_after = int(
|
|
|
|
self.helper.get_setting("crafty_logs_delete_after_days")
|
|
|
|
)
|
|
|
|
latest_log_files = [
|
|
|
|
"session.log",
|
|
|
|
"schedule.log",
|
|
|
|
"tornado-access.log",
|
|
|
|
"session.log",
|
|
|
|
"commander.log",
|
|
|
|
]
|
|
|
|
# we won't delete if delete logs after is set to 0
|
|
|
|
if logs_delete_after != 0:
|
|
|
|
log_files = list(
|
|
|
|
filter(
|
|
|
|
lambda val: val not in latest_log_files,
|
|
|
|
os.listdir(logs_path),
|
|
|
|
)
|
2023-03-02 18:40:45 +00:00
|
|
|
)
|
2023-05-09 00:11:15 +00:00
|
|
|
for log_file in log_files:
|
|
|
|
log_file_path = os.path.join(logs_path, log_file)
|
|
|
|
if Helpers.check_file_exists(
|
|
|
|
log_file_path
|
|
|
|
) and Helpers.is_file_older_than_x_days(
|
|
|
|
log_file_path, logs_delete_after
|
|
|
|
):
|
|
|
|
os.remove(log_file_path)
|
|
|
|
except:
|
|
|
|
logger.debug(
|
|
|
|
"Unable to find project root."
|
|
|
|
" If this issue persists please contact support."
|
2023-02-26 21:38:27 +00:00
|
|
|
)
|