event: make startup behavior multi-worker-safe

Assorted:
  - Relative symlinks for event assets/templates were already
    committed and are safe to make permanent anyway. Get rid of the
    code to tear down and relink them. Too many moving parts, though
    a valiant effort.
  - Build Event table directly from DB engine to avoid app context
    shenanigans.
  - Don't let chat workers build the table. Makes local testing much
    clearer.
  - Fix localhost not loading remote chat_done.js.
pull/22/head
Snakes 2022-11-29 14:01:07 -05:00
parent ab2419491b
commit 580e245b8f
Signed by: Snakes
GPG Key ID: E745A82778055C7E
4 changed files with 22 additions and 47 deletions

View File

@ -2,54 +2,23 @@ from os import path
import subprocess
from importlib import import_module
from files.__main__ import app, db_session, engine
from flask import g
from sqlalchemy import inspect
from files.helpers.const import AWARDS2, AWARDS_DISABLED
from files.__main__ import engine
from .table import Event
from events.classes import *
from events.helpers import *
from events.routes import *
def link_assets():
asset_dirs = { # We assume WD is always in root of repository
"files/assets/css": "../../../events/assets/css",
"files/assets/js": "../../../events/assets/js",
"files/assets/fonts": "../../../events/assets/fonts",
"files/assets/images": "../../../events/assets/images",
"files/assets/media": "../../../events/assets/media",
"files/templates": "../../events/templates",
}
print("[EVENT] Linking event assets...")
for link_dir in asset_dirs:
target = asset_dirs[link_dir]
link = link_dir + "/event"
try:
if path.exists(link):
subprocess.run(["rm", link])
subprocess.run(["ln", "-s", target, link])
print("[EVENT] Linked " + link + " -> " + target)
except Exception as e:
print(e)
def build_table():
if not inspect(engine).has_table("event", schema="public"):
def _build_table():
if not inspect(engine).has_table(Event.__table__.name, schema="public"):
print("[EVENT] Building event table...")
Event.__table__.create(bind=engine, checkfirst=True)
with app.app_context():
g.db = db_session()
Event.__table__.create(bind=g.db.bind, checkfirst=True)
g.db.commit()
def populate_awards():
def _populate_awards():
temp = {x: AWARDS2[x] for x in AWARDS2 if x not in EVENT_AWARDS}
AWARDS2.clear()
AWARDS2.update(EVENT_AWARDS)
@ -59,7 +28,7 @@ def populate_awards():
if award in AWARDS_DISABLED:
AWARDS_DISABLED.remove(award)
def init_event():
link_assets()
build_table()
populate_awards()
def event_init():
_build_table()
_populate_awards()

View File

@ -7,7 +7,6 @@ from os import environ
from sys import argv, stdout
import gevent
import redis
from flask import Flask
from flask_caching import Cache
from flask_compress import Compress
@ -44,7 +43,9 @@ app.config['SQLALCHEMY_DATABASE_URL'] = environ.get("DATABASE_URL").strip()
app.config["CACHE_TYPE"] = "RedisCache"
app.config["CACHE_REDIS_URL"] = environ.get("REDIS_URL").strip()
r=redis.Redis(host=environ.get("REDIS_URL").strip(), decode_responses=True, ssl_cert_reqs=None)
app.config['SERVICE'] = Service.RDRAMA
if "load_chat" in argv:
app.config['SERVICE'] = Service.CHAT
def get_CF():
with app.app_context():
@ -79,13 +80,14 @@ def no_step_on_jc():
if key and key == request.headers.get("X-No-Step", ""): return True
return False
if "load_chat" in argv:
from files.routes.chat import *
else:
if app.config['SERVICE'] == Service.RDRAMA:
from files.routes import *
elif app.config['SERVICE'] == Service.CHAT:
from files.routes.chat import *
if FEATURES['HOLIDAY_EVENT']:
from events import *
init_event()
if app.config['SERVICE'] == Service.RDRAMA:
event_init()
stdout.flush()

View File

@ -1,4 +1,5 @@
from copy import deepcopy
from enum import Enum, auto
from os import environ, path
import tldextract
@ -42,6 +43,9 @@ FP = environ.get("FP", "").strip()
KOFI_TOKEN = environ.get("KOFI_TOKEN", "").strip()
KOFI_LINK = environ.get("KOFI_LINK", "").strip()
class Service(Enum):
RDRAMA = auto()
CHAT = auto()
CLOUDFLARE_COOKIE_VALUE = "yes." # remember to change this in CloudFlare too

View File

@ -8,7 +8,7 @@ server {
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
add_header X-Frame-Options "deny";
add_header X-Content-Type-Options "nosniff";
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' challenges.cloudflare.com; connect-src 'self' tls-use1.fpapi.io api.fpjs.io 00bb6d59-7b11-4339-b1ae-b1f1259d1316.pushnotifications.pusher.com; object-src 'none';";
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' challenges.cloudflare.com rdrama.net; connect-src 'self' tls-use1.fpapi.io api.fpjs.io 00bb6d59-7b11-4339-b1ae-b1f1259d1316.pushnotifications.pusher.com; object-src 'none';";
location / {
proxy_pass http://localhost:5000/;