Skip to content

Commit

Permalink
Safely create_app in Celery code (try to fetch current_app first).
Browse files Browse the repository at this point in the history
Closes #3181.
  • Loading branch information
arikfr committed Dec 13, 2018
1 parent 8481dac commit fbf1ca7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion redash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import urlparse
import urllib
import redis
from flask import Flask
from flask import Flask, current_app
from flask_sslify import SSLify
from werkzeug.contrib.fixers import ProxyFix
from werkzeug.routing import BaseConverter
Expand Down Expand Up @@ -141,3 +141,11 @@ def create_app(load_admin=True):
chrome_logger.init_app(app)

return app


def safe_create_app():
"""Return current_app or create a new one."""
if current_app:
return current_app

return create_app()
6 changes: 3 additions & 3 deletions redash/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from celery import Celery
from celery.schedules import crontab
from celery.signals import worker_process_init
from redash import __version__, create_app, settings
from redash import __version__, safe_create_app, settings
from redash.metrics import celery as celery_metrics

celery = Celery('redash',
Expand Down Expand Up @@ -77,14 +77,14 @@ def __call__(self, *args, **kwargs):
# Create Flask app after forking a new worker, to make sure no resources are shared between processes.
@worker_process_init.connect
def init_celery_flask_app(**kwargs):
app = create_app()
app = safe_create_app()
app.app_context().push()


# Hook for extensions to add periodic tasks.
@celery.on_after_configure.connect
def add_periodic_tasks(sender, **kwargs):
app = create_app()
app = safe_create_app()
periodic_tasks = getattr(app, 'periodic_tasks', {})
for params in periodic_tasks.values():
sender.add_periodic_task(**params)

0 comments on commit fbf1ca7

Please sign in to comment.