def make_celery(app): celery = Celery(app.import_name, 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 make_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 create_celery_app(app, settings_override=None): """ 初始化异步任务框架 :param app: :param settings_override: :return: """ from flask.ext.celery import Celery celery = Celery(app.import_name, broker=app.config.get('CELERY_BROKER_URL')) celery.conf.update(app.config) celery.conf.update(settings_override) context_task = celery.Task class ContextTask(context_task): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return context_task.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery
def app_context(request): """Initializes the application and sets the app context to avoid needing 'with app.app_context():'. This needs to run first, so it has been placed in the top-level conftest.py and the function starts with the letter 'a'. """ app = Flask(__name__) app.config['CELERY_ALWAYS_EAGER'] = True app.config['TESTING'] = True app.config['REDIS_URL'] = 'redis://localhost/1' app.config['CELERY_BROKER_URL'] = 'redis://localhost/1' app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost/1' Celery(app) Redis(app) context = app.app_context() context.push() request.addfinalizer(lambda: context.pop())
from flask import Flask from flask_heroku import Heroku from flask_sslify import SSLify from raven.contrib.flask import Sentry from flask.ext.celery import Celery app = Flask(__name__) app.secret_key = os.environ.get('APP_SECRET', 'some-secret-key') app.debug = 'DEBUG' in os.environ # Use gevent workers for celery. app.config['CELERYD_POOL'] = 'gevent' # Bootstrap Heroku environment variables. heroku = Heroku(app) # Redirect urls to https sslify = SSLify(app) # Setup error collection sentry = Sentry(app) # Task queue celery = Celery(app) @app.route('/') def hello_world(): return 'Hello World!'
from datetime import datetime, timedelta import time from app import app, db from models import Agency, Prediction from nextbus import Nextbus """ Celery is a task queue for background task processing. We're using it for scheduled tasks, which are configured in this file. The task execution schedule can be found/tweaked in config.py. """ # Create new Celery object with configured broker; get other cfg params celery = Celery(app) celery.conf.update(app.config) # This wraps task execution in an app context (for db session, etc) 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 logger = get_task_logger(__name__) # Task definitions: @celery.task()
import requests from datetime import datetime from dateutil.parser import parse as parse_date from flask.ext.celery import Celery from juggernaut import Juggernaut from mongoengine.queryset import Q from breezeminder.app import app from breezeminder.models.card import BreezeCard from breezeminder.models.messaging import Messaging from breezeminder.models.marta import Bus celery = Celery(app) celery.config_from_object(app.config) # 15 Minute default DEFAULT_RETRY = 15 * 60 @celery.task(name="tasks.check_card_pulls", ignore_result=True) def check_card_pulls(): """ Find cards that aren't scheduled for a pull """ logger = check_card_pulls.get_logger() threshold = datetime.now() - app.config["REFRESH_INTERVAL"] - app.config["CARD_STALE_THRESH"] logger.info("Checking stale cards never loaded or loaded before %s" % threshold)
def test_one_dumb_line(): app = FakeApp() Celery(app) assert 'celery' in app.extensions
def test_multiple(): assert 'celery' in current_app.extensions with pytest.raises(ValueError): Celery(current_app)
def test_one_dumb_line(): flask_app = FakeApp() Celery(flask_app) assert 'celery' in flask_app.extensions
from flask.ext.principal import Principal, Permission, RoleNeed from flask.ext.restful import Api from flask.ext.admin import Admin from flask_debugtoolbar import DebugToolbarExtension from flask.ext.cache import Cache from flask.ext.assets import Environment from flask.ext.celery import Celery from celery.backends.redis import RedisBackend from wfdb.models import User login_manager = LoginManager() login_manager.login_view = "main.login" principal = Principal() admin_permission = Permission(RoleNeed('admin')) default_permission = Permission(RoleNeed('default')) rest_api = Api() admin = Admin() toolbar = DebugToolbarExtension() cache = Cache() assets_env = Environment() celery = Celery() celery.backend = RedisBackend(app=celery) @login_manager.user_loader def load_user(userid): return User.query.get(userid)
from flask.ext.admin import Admin from flask_debugtoolbar import DebugToolbarExtension from flask.ext.cache import Cache from flask.ext.assets import Environment from flask.ext.celery import Celery from celery.backends.redis import RedisBackend from wfdb.models import User login_manager = LoginManager() login_manager.login_view = "main.login" principal = Principal() admin_permission = Permission(RoleNeed('admin')) default_permission = Permission(RoleNeed('default')) rest_api = Api() admin = Admin() toolbar = DebugToolbarExtension() cache = Cache() assets_env = Environment() celery = Celery() celery.backend = RedisBackend(app=celery) @login_manager.user_loader def load_user(userid): return User.query.get(userid)
from app.etl import job import os import flask from flask.ext.celery import Celery import redis #from .run import get_celery from celery import Celery celery = Celery('tasks', broker='redis://localhost:6379/0') celery.config_from_object('celeryconfig') @celery.task def add(x, y): return x + y @celery.task() def startjob(jobfile): redisconn = redis.StrictRedis(host='localhost', port=6379, db=0) j = job.Job(redisconn) j.loadyaml(jobfile) print "started job:" + j.name j.execute() def make_celery(app): celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
from celery.utils.log import get_task_logger from flask.ext.celery import Celery from datetime import datetime, timedelta import time from app import app, db from models import Agency, Prediction from nextbus import Nextbus """ Celery is a task queue for background task processing. We're using it for scheduled tasks, which are configured in this file. The task execution schedule can be found/tweaked in config.py. """ # Create new Celery object with configured broker; get other cfg params celery = Celery(app) celery.conf.update(app.config) # This wraps task execution in an app context (for db session, etc) 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
from celery.schedules import crontab from flask.ext.celery import Celery CELERYBEAT_SCHEDULE = { # executes every night at 4:15 'every-night': { 'task': 'user.checkaccounts', 'schedule': crontab(hour=4, minute=20) } } celery = Celery(www) @celery.task(name='user.checkaccounts') def checkaccounts(): # do something pass
#email server config MAIL_SERVER="smtp.gmail.com", MAIL_PORT="587", MAIL_USE_TLS=True, MAIL_USE_SSL=False, MAIL_USERNAME="******", MAIL_PASSWORD="******", MAIL_FAIL_SILENTLY=False) #setup jinja2 fragment caching app.jinja_env.fragment_cache = RedisCache(host="127.0.0.1", default_timeout=86000) app.jinja_env.add_extension(FragmentCacheExtension) #connecting to celery Celery(app) #connect to mongohq db.init_app(app) #enabling markdown Markdown(app) #configuring flask-login login_manager = LoginManager() login_manager.setup_app(app) login_manager.login_view = "other.login_required" login_manager.session_protection @login_manager.user_loader
""" Instantiate all extensions used by the application here """ from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() from flask_migrate import Migrate migrate = Migrate() from flask.ext.cache import Cache cache = Cache(config={'CACHE_TYPE': 'redis', 'CACHE_KEY_PREFIX': 'abort_cache', 'CACHE_REDIS_URL': 'redis://localhost:6379', 'CACHE_REDIS_PORT': '6379', 'CACHE_REDIS_HOST': 'localhost'}) from flask.ext.celery import Celery celery = Celery()
# Create the Flask-Bcrypt's instance bcrypt = Bcrypt() # Create the Flask-OpenID's instance openid = OpenID() # Create the Flask-Principal's instance principals = Principal() # Create the Flask-Restful's instance restful_api = Api() # Create the Flask-Celery-Helper's instance flask_celery = Celery() # Create the Flask-DebugToolbar's instance debug_toolbar = DebugToolbarExtension() # Create the Flask-Cache's instance cache = Cache() # Create the Flask-Admin's instance flask_admin = Admin() # Create the Flask-Mail's instance mail = Mail() # Create the Flask-Assets's instance assets_env = Environment()