From a10350fb62c9c91aba27f370944be649ee44b2e5 Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Sun, 13 Aug 2023 19:08:31 +0000 Subject: [PATCH] Centralize flask instantiation hack in one place for non-web utilities that still need production cache. --- bemani/utils/api.py | 2 +- bemani/utils/config.py | 8 ++++++-- bemani/utils/frontend.py | 2 +- bemani/utils/scheduler.py | 5 ++--- bemani/utils/services.py | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bemani/utils/api.py b/bemani/utils/api.py index 287720b..82f85a3 100644 --- a/bemani/utils/api.py +++ b/bemani/utils/api.py @@ -15,7 +15,7 @@ def load_config(filename: str) -> None: def instantiate_cache(app: Any) -> None: global config - base_instantiate_cache(app, config) + base_instantiate_cache(config, app) def main() -> None: diff --git a/bemani/utils/config.py b/bemani/utils/config.py index 81b9bae..8380ba0 100644 --- a/bemani/utils/config.py +++ b/bemani/utils/config.py @@ -1,6 +1,6 @@ import yaml from flask import Flask -from typing import Set +from typing import Optional, Set from bemani.backend.iidx import IIDXFactory from bemani.backend.popn import PopnMusicFactory @@ -27,7 +27,11 @@ def load_config(filename: str, config: Config) -> None: config["support"] = supported_series -def instantiate_cache(app: Flask, config: Config) -> None: +def instantiate_cache(config: Config, app: Optional[Flask] = None) -> None: + # Possibly set up a dummy app context because flask-caching needs it. + if app is None: + app = Flask(__name__) + # This could easily be extended to add support for any other backend that flask-caching # supports but right now the only demand is for in-memory, filesystem and memcached. if config.memcached_server is not None: diff --git a/bemani/utils/frontend.py b/bemani/utils/frontend.py index e54d64a..fe4ef4f 100644 --- a/bemani/utils/frontend.py +++ b/bemani/utils/frontend.py @@ -64,7 +64,7 @@ def load_config(filename: str) -> None: def instantiate_cache(app: Any) -> None: global config - base_instantiate_cache(app, config) + base_instantiate_cache(config, app) def main() -> None: diff --git a/bemani/utils/scheduler.py b/bemani/utils/scheduler.py index f9fb1d4..76d021d 100644 --- a/bemani/utils/scheduler.py +++ b/bemani/utils/scheduler.py @@ -1,5 +1,4 @@ import argparse -from flask import Flask from typing import Any, List from bemani.backend.popn import PopnMusicFactory @@ -100,8 +99,8 @@ if __name__ == "__main__": if args.read_only: config["database"]["read_only"] = True - # Set up production cache, with a dummy app context because flask-caching needs it. - instantiate_cache(Flask(__name__), config) + # Set up production cache. + instantiate_cache(config) # Run out of band work run_scheduled_work(config) diff --git a/bemani/utils/services.py b/bemani/utils/services.py index 180cd5a..23cf3cc 100644 --- a/bemani/utils/services.py +++ b/bemani/utils/services.py @@ -130,7 +130,7 @@ def load_config(filename: str) -> None: def instantiate_cache(app: Any) -> None: global config - base_instantiate_cache(app, config) + base_instantiate_cache(config, app) if __name__ == "__main__":