Skip to content

Commit

Permalink
api: only write api specs if is_gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Aug 27, 2021
1 parent a2e4a61 commit 4cb85dd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
File renamed without changes.
5 changes: 2 additions & 3 deletions mpcontribs-api/mpcontribs/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
delimiter, max_depth = ".", 4
invalidChars = set(punctuation.replace("*", ""))
invalidChars.add(" ")
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")


for mod in [
"matplotlib",
Expand Down Expand Up @@ -208,9 +210,6 @@ def create_app():
except AttributeError as ex:
logger.error(f"Failed to register {module_path}: {collection} {ex}")

# only load/register what's needed when run as part of `flask rq worker/scheduler` (not gunicorn)
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")

if app.kernels:
from mpcontribs.api.notebooks.views import rq, make
rq.init_app(app)
Expand Down
12 changes: 7 additions & 5 deletions mpcontribs-api/mpcontribs/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from mongoengine.queryset.visitor import Q
from werkzeug.exceptions import Unauthorized
from mpcontribs.api.config import DOC_DIR
from mpcontribs.api import is_gunicorn

logger = logging.getLogger("app")

Expand Down Expand Up @@ -448,11 +449,12 @@ def __init__(cls, name, bases, d):
if not os.path.exists(file_path):
os.makedirs(dir_path, exist_ok=True)

with open(file_path, "w") as f:
yaml.dump(spec, f)
logger.warning(
f"{cls.tags[0]}.{method.__name__} written to {file_path}"
)
if is_gunicorn:
with open(file_path, "w") as f:
yaml.dump(spec, f)
logger.warning(
f"{cls.tags[0]}.{method.__name__} written to {file_path}"
)


class SwaggerView(OriginalSwaggerView, ResourceView, metaclass=SwaggerViewType):
Expand Down

0 comments on commit 4cb85dd

Please sign in to comment.