diff --git a/WebHost.py b/WebHost.py index 1962a1cd..261f82b5 100644 --- a/WebHost.py +++ b/WebHost.py @@ -14,7 +14,7 @@ from WebHostLib import app as raw_app from waitress import serve from WebHostLib.models import db -from WebHostLib.autolauncher import autohost +from WebHostLib.autolauncher import autohost, autogen from WebHostLib.lttpsprites import update_sprites_lttp from WebHostLib.options import create as create_options_files @@ -45,6 +45,8 @@ if __name__ == "__main__": create_options_files() if app.config["SELFLAUNCH"]: autohost(app.config) + if app.config["SELFGEN"]: + autogen(app.config) if app.config["SELFHOST"]: # using WSGI, you just want to run get_app() if app.config["DEBUG"]: autohost(app.config) diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index e4ada5bc..235b4f52 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -22,9 +22,10 @@ Pony(app) app.jinja_env.filters['any'] = any app.jinja_env.filters['all'] = all -app.config["SELFHOST"] = True +app.config["SELFHOST"] = True # application process is in charge of running the websites app.config["GENERATORS"] = 8 # maximum concurrent world gens -app.config["SELFLAUNCH"] = True +app.config["SELFLAUNCH"] = True # application process is in charge of launching Rooms. +app.config["SELFGEN"] = True # application process is in charge of scheduling Generations. app.config["DEBUG"] = False app.config["PORT"] = 80 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER diff --git a/WebHostLib/autolauncher.py b/WebHostLib/autolauncher.py index 0c1c2b6d..97db4ca6 100644 --- a/WebHostLib/autolauncher.py +++ b/WebHostLib/autolauncher.py @@ -110,6 +110,26 @@ def autohost(config: dict): def keep_running(): try: with Locker("autohost"): + while 1: + time.sleep(0.1) + with db_session: + rooms = select( + room for room in Room if + room.last_activity >= datetime.utcnow() - timedelta(days=3)) + for room in rooms: + launch_room(room, config) + + except AlreadyRunningException: + logging.info("Autohost reports as already running, not starting another.") + + import threading + threading.Thread(target=keep_running, name="AP_Autohost").start() + + +def autogen(config: dict): + def keep_running(): + try: + with Locker("autogen"): with multiprocessing.Pool(config["GENERATORS"], initializer=init_db, initargs=(config["PONY"],)) as generator_pool: @@ -129,22 +149,17 @@ def autohost(config: dict): select(generation for generation in Generation if generation.state == STATE_ERROR).delete() while 1: - time.sleep(0.50) + time.sleep(0.1) with db_session: - rooms = select( - room for room in Room if - room.last_activity >= datetime.utcnow() - timedelta(days=3)) - for room in rooms: - launch_room(room, config) to_start = select( generation for generation in Generation if generation.state == STATE_QUEUED) for generation in to_start: launch_generator(generator_pool, generation) except AlreadyRunningException: - pass + logging.info("Autogen reports as already running, not starting another.") import threading - threading.Thread(target=keep_running).start() + threading.Thread(target=keep_running, name="AP_Autogen").start() multiworlds = {}