Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from flask import Flask, request
from celery import Celery

import config

Expand All @@ -11,9 +12,12 @@ def create_app():
app.config.from_object(config)

setup_logging(app)
celery = setup_celery(app)

load_models(app)
load_extensions(app)
load_blueprints(app)
load_tasks(celery)

return app

Expand Down Expand Up @@ -58,6 +62,25 @@ def log_response(response):
return response


def setup_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task

class ContextTask(TaskBase):
abstract = True

def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)

celery.Task = ContextTask
return celery


def load_extensions(app):
"""
Dynamicaly load "extensions" specified in the `EXTENSIONS` config tuple.
Expand Down Expand Up @@ -120,3 +143,9 @@ def load_blueprints(app):

view = importlib.import_module('app.views.{0}'.format(name))
app.register_blueprint(view.bp, **kwargs)


def load_tasks(celery):
tasks.celery = celery
for task in config.TASKS:
importlib.import_module('app.tasks.{0}'.format(task))
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ flask-mongoengine
flask-wtf
gunicorn
boto3
celery