Skip to content

Commit

Permalink
api: cron_job and dashboards only if is_gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Aug 27, 2021
1 parent db43def commit a2e4a61
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions mpcontribs-api/mpcontribs/api/__init__.py
Expand Up @@ -208,30 +208,36 @@ 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)
setattr(app, "cron_job_id", f"auto-notebooks-build_{MPCONTRIBS_API_HOST}")
make.cron('*/3 * * * *', app.cron_job_id)
print(f"CRONJOB {app.cron_job_id} added.")

if is_gunicorn:
setattr(app, "cron_job_id", f"auto-notebooks-build_{MPCONTRIBS_API_HOST}")
make.cron('*/3 * * * *', app.cron_job_id)
print(f"CRONJOB {app.cron_job_id} added.")

def healthcheck():
if not DEBUG and not app.kernels:
return "KERNEL GATEWAY NOT AVAILABLE", 500

return "OK"

app.register_blueprint(sse, url_prefix="/stream")
app.add_url_rule("/healthcheck", view_func=healthcheck)
app.register_blueprint(rq_dashboard.blueprint, url_prefix="/rq")

dashboard.config.init_from(file="dashboard.cfg")
dashboard.config.version = app.config["VERSION"]
dashboard.config.table_prefix = f"fmd_{MPCONTRIBS_API_HOST}"
db_password = os.environ["POSTGRES_DB_PASSWORD"]
db_host = os.environ["POSTGRES_DB_HOST"]
dashboard.config.database_name = f"postgresql://kong:{db_password}@{db_host}/kong"
dashboard.bind(app)
if is_gunicorn:
app.register_blueprint(sse, url_prefix="/stream")
app.add_url_rule("/healthcheck", view_func=healthcheck)
app.register_blueprint(rq_dashboard.blueprint, url_prefix="/rq")

dashboard.config.init_from(file="dashboard.cfg")
dashboard.config.version = app.config["VERSION"]
dashboard.config.table_prefix = f"fmd_{MPCONTRIBS_API_HOST}"
db_password = os.environ["POSTGRES_DB_PASSWORD"]
db_host = os.environ["POSTGRES_DB_HOST"]
dashboard.config.database_name = f"postgresql://kong:{db_password}@{db_host}/kong"
dashboard.bind(app)

@app.before_first_request
def before_first_request_func():
Expand Down

0 comments on commit a2e4a61

Please sign in to comment.