WebHost: split autolaunch and autogen services

This commit is contained in:
Fabian Dill 2021-12-13 05:48:33 +01:00
parent 3bf367d630
commit 3f20bdaaa2
3 changed files with 29 additions and 11 deletions

View File

@ -14,7 +14,7 @@ from WebHostLib import app as raw_app
from waitress import serve from waitress import serve
from WebHostLib.models import db 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.lttpsprites import update_sprites_lttp
from WebHostLib.options import create as create_options_files from WebHostLib.options import create as create_options_files
@ -45,6 +45,8 @@ if __name__ == "__main__":
create_options_files() create_options_files()
if app.config["SELFLAUNCH"]: if app.config["SELFLAUNCH"]:
autohost(app.config) 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["SELFHOST"]: # using WSGI, you just want to run get_app()
if app.config["DEBUG"]: if app.config["DEBUG"]:
autohost(app.config) autohost(app.config)

View File

@ -22,9 +22,10 @@ Pony(app)
app.jinja_env.filters['any'] = any app.jinja_env.filters['any'] = any
app.jinja_env.filters['all'] = all 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["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["DEBUG"] = False
app.config["PORT"] = 80 app.config["PORT"] = 80
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

View File

@ -110,6 +110,26 @@ def autohost(config: dict):
def keep_running(): def keep_running():
try: try:
with Locker("autohost"): 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, with multiprocessing.Pool(config["GENERATORS"], initializer=init_db,
initargs=(config["PONY"],)) as generator_pool: 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() select(generation for generation in Generation if generation.state == STATE_ERROR).delete()
while 1: while 1:
time.sleep(0.50) time.sleep(0.1)
with db_session: 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( to_start = select(
generation for generation in Generation if generation.state == STATE_QUEUED) generation for generation in Generation if generation.state == STATE_QUEUED)
for generation in to_start: for generation in to_start:
launch_generator(generator_pool, generation) launch_generator(generator_pool, generation)
except AlreadyRunningException: except AlreadyRunningException:
pass logging.info("Autogen reports as already running, not starting another.")
import threading import threading
threading.Thread(target=keep_running).start() threading.Thread(target=keep_running, name="AP_Autogen").start()
multiworlds = {} multiworlds = {}