Example #1
0
def create_celery(app):
    global __celery
    if __celery:
        return __celery

    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_RESULT_BACKEND'],
        broker=app.config['BROKER_URL'],
    )
    celery.conf.update(app.config)

    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                db.session = db.create_scoped_session()
                try:
                    response = TaskBase.__call__(self, *args, **kwargs)
                finally:
                    db.session.remove()
                return response

    celery.Task = ContextTask

    __celery = celery
    return __celery
Example #2
0
def make_celery(flask_app):

	"""

	While you can use Celery without any reconfiguration with Flask, 
	it becomes a bit nicer by subclassing tasks and adding support 
	for Flask's application contexts and hooking it up with the 
	Flask configuration.

	- http://flask.pocoo.org/docs/patterns/celery/

	"""

	celery = Celery()
	celery.config_from_object(settings.config)

	TaskBase = celery.Task
	class ContextTask(TaskBase):
	    abstract = True
	    def __call__(self, *args, **kwargs):
	        with flask_app.app_context():
	            return TaskBase.__call__(self, *args, **kwargs)
	celery.Task = ContextTask

	return celery
Example #3
0
    def setup_celery(self):
        celery = Celery()

        import radar.ukrdc_importer.tasks  # noqa
        import radar.ukrdc_exporter.tasks  # noqa

        broker_url = self.config.get('CELERY_BROKER_URL')

        if broker_url is not None:
            celery.conf.BROKER_URL = broker_url

        celery.conf.update(self.config)

        TaskBase = celery.Task

        app = self

        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
Example #4
0
def make_celery():
    app, conf = create_app()
    if app.config['REDIS_PASSWORD']:
        redis_broker='redis://:{}@{}:{}/0'.format(
            app.config['REDIS_PASSWORD'],
            app.config['REDIS_HOST'],
            app.config['REDIS_PORT'],
        )
    else:
        redis_broker='redis://{}:{}/0'.format(
            app.config['REDIS_HOST'],
            app.config['REDIS_PORT'],
        )
        app.logger.info('MIGRATE_MODE: {}, MIGRATE_ES: {}, Broker at {}'
                        .format(MIGRATE_MODE, MIGRATE_ES,app.config['REDIS_HOST']))
    celery = Celery(app.import_name, broker=redis_broker)
    celery.conf.update(app.config)
    celery.data_db = app.data_db
    # boiler plate to get our tasks running in the app context
    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
Example #5
0
def create_celery_app(app=None):
    """Create a new Celery object and tie together the Celery config to the
    app's config. Wrap all tasks in the context of the application.

    Args:
        app (Flask)

    Returns:
        celery (Celery)

    """

    app = app or create_app()

    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'],
                    include=CELERY_TASK_LIST)
    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
Example #6
0
def create_celery_app(app=None, settings_override=None):
    """Creates a celery app to perform asynchronous jobs.

    Params:
        app: A Flask app.
        settings_override: Override settings from config file.
    """
    app = app or create_app('silently_celery', os.path.dirname(__file__),
            settings_override)
    celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    if app.debug == True:
        #Disable while image data is written to the log.
        celery.conf.CELERYD_LOG_LEVEL = LOG_LEVELS['DEBUG']

    class ContextTask(celery.Task):
        """Add Flask app context awareness."""
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return celery.Task.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery
Example #7
0
def make_celery(app):
    """
    Recommended from Flask's documentation to set up the Celery object.
    :param app:
    :return:
    """
    celery = Celery(app.import_name)

    # Determine which Celery configuration to load:
    # The order is:
    # 1. `SM_CELERY_CONFIG` Environment Variable
    # 2. The default "celeryconfig.py"
    celery.config_from_object(get_celery_config_file())
    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
Example #8
0
def make_celery(app):
    cel = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    # celery.conf.update(
        # CELERY_IMPORTS=(
            # 'buyapi.tasks',   # we're not including our tasks here as
            # 'app.module_b.tasks',   # our tasks are in other files listed here
        # )
    # )
    cel.conf.update(CELERYBEAT_SCHEDULE={
        # Executes every Monday morning at 7:30 A.M
        'every_minute': {
            'task': 'tasks.regular.run_every_minute',
            'schedule': timedelta(minutes=1),
        },
    }, CELERY_TIMEZONE='Europe/Moscow')

    cel.conf.update(app.config)
    # cel.conf.update(CELERY_TASK_RESULT_EXPIRES=10)
    TaskBase = cel.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    cel.Task = ContextTask
    return cel
Example #9
0
def make_celery():
    """
    Configures Celery task queue
    """
    celery = Celery("tasks", broker=SETTINGS['MESSAGE_BROKER_URL'],
                    backend=SETTINGS['REDIS_URL'])
    # Settings to adjust to the AMQP message quota
    celery.conf.update(
            CELERY_TASK_SERIALIZER='pickle',
            CELERY_ACCEPT_CONTENT=['pickle'],
            CELERY_RESULT_SERIALIZER='json',
            BROKER_POOL_LIMIT=1,
            CELERYD_CONCURRENCY=2,
            CELERYD_PROCESSES=1
    )
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

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

    celery.Task = ContextTask
    return celery
Example #10
0
def prepare_celery(app):
    """Set up celery for this application."""
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    
    class ContextTask(celery.Task):
        """A task which maintains an SQLAlchemy database session."""
        abstract = True
        _session = None
    
        @property
        def session(self):
            """Database session"""
            if self._session is None:
                self._session = scoped_session(sessionmaker(bind=app.engine))
            return self._session
            
        @property
        def redis(self):
            """Get a redis client which is appropriate."""
            return app.redis
        
        def after_return(self, *args, **kwargs):
            if self._session is not None:
                self._session.remove()
            super(ContextTask, self).after_return(*args, **kwargs)
        
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return super(ContextTask, self).__call__(*args, **kwargs)
        
    
    celery.Task = ContextTask
    return celery
Example #11
0
def create_celery_app(flask_app=None):
    logger.debug("Creating celery app")
    if not flask_app:
        if has_app_context():
            logger.debug("Using current flask app")
            app = current_app
        else:
            logger.debug("No current flask app")
            app = create_app()
    else:
        app = flask_app
    celery = Celery(app.import_name,
                    broker=app.config['CELERY_BROKER_URL'],
                    backend=app.config['CELERY_RESULT_BACKEND'])
    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
    celery.logger = get_task_logger(__name__)
    app.celery = celery
    return app
Example #12
0
def make_celery(app):
    # default to using JSON, rather than Pickle (Celery's default, but Celery
    # warns about it)
    for var, dfl in [
            ('CELERY_ACCEPT_CONTENT', ['json']),
            ('CELERY_TASK_SERIALIZER', 'json'),
            ('CELERY_RESULT_SERIALIZER', 'json')]:
        app.config[var] = app.config.get(var, dfl)
    broker = app.config.get('CELERY_BROKER_URL', 'memory://')
    celery = Celery(app.import_name, broker=broker)
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                try:
                    return TaskBase.__call__(self, *args, **kwargs)
                finally:
                    # flush any open DB sessions used by this task
                    current_app.db.flush_sessions()

    celery.Task = ContextTask
    app.celery_tasks = dict((fn, celery.task(**kwargs)(fn))
                            for (fn, kwargs) in _defined_tasks.iteritems())
    return celery
Example #13
0
def bootstrap_celery(app):
    """
    Registers celery with the app

    :param app: the flask app

    :return: the Celery context
    """

    # importing here because some tasks refer to app, lol
    from AuthorityReporter.tasks.etl import links_for_page, get_contributing_authors, edit_distance, get_all_revisions
    from AuthorityReporter.tasks.etl import get_title_top_authors, set_page_key
    from AuthorityReporter.tasks.solr import add_with_metadata, build_wiki_user_doc, get_wiki_topic_doc
    from AuthorityReporter.tasks.solr import analyze_pages_globally, analyze_users_globally, analyze_wikis_globally
    from AuthorityReporter.tasks.solr import aggregate_global_topic, analyze_topics_globally
    from AuthorityReporter.tasks.solr import analyze_all_user_pages_globally
    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
Example #14
0
def make_celery(app):
    """
        构造 celery
    :param app:
    :return:
    """
    broker_url = app.config["REDIS_URL"]
    backend_url = app.config["REDIS_URL"]

    celery = Celery("ovftool_celery", broker=broker_url, backend=backend_url)

    celery.conf.update(CELERYBEAT_SCHEDULE={
    }, CELERY_ENABLE_UTC=True)

    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
Example #15
0
def create_celery_app(flask_app):
    """Create a Celery application."""
    celery = Celery(flask_app.import_name)
    celery.conf.update(flask_app.config)
    celery.Task = AppContextTask

    # Set Flask application object on the Celery application.
    celery.flask_app = flask_app

    return celery
Example #16
0
def make_celery(app):
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    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
Example #17
0
def make_celery(app):
    celery = Celery(broker='amqp://guest@localhost//', backend='rpc')
    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
Example #18
0
File: main.py Project: ehfeng/Spire
def make_celery(app):
    celery = Celery(app.import_name, backend='redis://localhost:6379/0', broker='redis://localhost:6379/0')
    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
Example #19
0
def make_celery(app):
	celery = Celery(backend='redis://localhost:6379/1')
	celery.config_from_object('cl.celeryConfig')
	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
Example #20
0
def make_celery(app):
    celery = Celery(app.import_name)
    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
Example #21
0
def celery_config(app): # Configures async library for updating movie images 
    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
Example #22
0
def make_celery(app):
    celery = Celery(main='MONKEY_TASKS', backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['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(flask_app):
    region = getenv('AWS_SQS_REGION')
    celery_options = {
        'broker_transport_options': {
            'region': region
        },
        'task_default_queue': QUEUE_NAME
    }
    broker_url = getenv('AWS_SQS_BROKER_URL')
    if not broker_url:
        broker_url = 'sqs://'
        if getenv('AWS_SQS_ACCESS_KEY_ID') and getenv('AWS_SQS_SECRET_ACCESS_KEY'):
            broker_url += '{}:{}@'.format(
                quote_plus(getenv('AWS_SQS_ACCESS_KEY_ID')),
                quote_plus(getenv('AWS_SQS_SECRET_ACCESS_KEY'))
            )

    celery = Celery(
        flask_app.import_name,
        broker=broker_url,
        include=[
            'app.api.services',
            'app.emails',
            'app.emails.util',
            'app.tasks.email',
            'app.tasks.mailchimp',
            'app.tasks.brief_tasks',
            'app.tasks.s3',
            'app.tasks.brief_response_tasks',
            'app.tasks.supplier_tasks',
            'app.tasks.jira',
            'app.tasks.dreamail',
            'app.tasks.publish_tasks'
        ]
    )
    celery.conf.update(flask_app.config)
    celery.config_from_object(celery_options)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def after_return(self, *args, **kwargs):
            db.session.remove()

        def __call__(self, *args, **kwargs):
            if current_app:
                return TaskBase.__call__(self, *args, **kwargs)
            else:
                with flask_app.app_context():
                    return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery
def create_celery_app(app):
    celery = Celery(__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
Example #25
0
def make_celery(app):
	'''pass flask to celery when background tasks are run'''
	celery = Celery(app.name)
	celery.config_from_object("celeryconfig")
	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
Example #26
0
def make_celery(application):
    celery = Celery(application.name,
                    broker=application.config['CELERY_BROKER_URL'])
    celery.conf.update(application.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with application.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery
Example #27
0
def make_celery(app):
    celery = Celery(app.__class__)
    platforms.C_FORCE_ROOT = True
    celery.config_from_object('setting')
    #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 (app,celery)
def make_celery(server):
    celery = Celery(app.import_name,
                    backend=server.config['CELERY_RESULT_BACKEND'],
                    broker=server.config['CELERY_BROKER_URL'])
    celery.conf.update(server.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with server.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery
Example #29
0
def make_celery(app=None):
    if not app:
        app = make_app()
    celery = Celery('tasks', 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
Example #30
0
def make_celery(app):
    celery_ = Celery('celery_app')
    celery_.config_from_object('config')

    TaskBase = celery_.Task
    celery_.app = app
    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_
Example #31
0
def make_celery(app=None):
    app = app or create_app()
    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_BACKEND'],
        broker=app.config['CELERY_BROKER_URL'],
    )
    celery.conf.update(app.config)
    celery.callback_url = app.config['API_CALLBACK_URL']
    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
Example #32
0
def create_celery_app(app=None):
    """Return a celery app in app context"""
    app = app or create_app(os.environ.get("ENV", "prod"),
                            register_blueprints=False)
    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
Example #33
0
def make_celery(app: Scaffold) -> Celery:
    """
    Creating a celery application for flask applications.
    Source: https://flask.palletsprojects.com/en/1.1.x/patterns/celery/#configure
    """

    celery = Celery(
        app.import_name,
        backend=app.config["CELERY_RESULT_BACKEND"],
        broker=app.config["CELERY_BROKER_URL"],
    )
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask
    return celery
Example #34
0
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):
        """
        Integrate app context to celery.Task
        """
        abstract = True

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

    # Make other extensions could be called normally
    celery.Task = ContextTask
    return celery
Example #35
0
def make_celery(app):
    # create the celery instance and configure it
    celery = Celery(app.import_name,
                    backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    # setup the base class for celery tasks
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        """Task class which creates an app_context before calling the task."""
        abstract = True

        def __call(self, *args, **kwargs):
            with app.app_context():
                return super(ContextTask, self).__call__(*args, **kwargs)

    celery.Task = ContextTask
    return celery
Example #36
0
def make_celery(app):
    if app.config['CELERY_RESULT_BACKEND'] and app.config['CELERY_BROKER_URL']:
        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
    else:
        logger.error("Celery disable, redis backend is required.")
        return None
def create_celery(application):
    """
    Configures celery instance from application, using it's config
    :param application: Flask application instance
    :return: Celery instance
    """
    celery = Celery(application.import_name,
                    broker=application.config['CELERY_BROKER_URL'])
    celery.conf.update(application.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

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

    celery.Task = ContextTask
    return celery
Example #38
0
def make_celery(app=None):
    """

    :param app:
    :return:
    """
    app = app or create_app(app_name='worker')
    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
Example #39
0
def make_celery(app):
    # set redis url vars
    app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
    # 'REDIS_URL', 'redis://localhost:6379/0')
    app.config['CELERY_RESULT_BACKEND'] = app.config['CELERY_BROKER_URL']
    # create context tasks in celery
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    # print 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
Example #40
0
def create_celery(app=None):
    """
    The function creates a new Celery object, configures it with the broker
    from the application config, updates the rest of the Celery config from the
    Flask config and then creates a subclass of the task that wraps the task
    execution in an application context.
    http://flask.pocoo.org/docs/0.10/patterns/celery/
    :param app: flask.Flask application instance or None
    :return: configured celery.Celery instance
    """
    if app is None:
        app = create_app()

    celery = Celery(
        app.import_name,
        broker=app.config.get('CELERY_BROKER_URL'),
        backend=app.config.get('CELERY_BROKER_URL'),
    )
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):

            # If we're already in an app_context, use that. Otherwise, use
            # the default app.app_context. This is necessary for providing
            # custom applications for tests
            def get_ctx():  # pragma: no cover
                try:
                    ctx = current_app.app_context()
                except RuntimeError:
                    ctx = app.app_context()
                return ctx

            with get_ctx():
                return TaskBase.__call__(self, *args, **kwargs)

    celery.Task = ContextTask
    return celery
Example #41
0
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)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)
    
    class ContextQueueOnce(QueueOnce):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return super(ContextQueueOnce, self).__call__(*args, **kwargs)

    celery.Task = ContextTask
    celery.QueueOnce = ContextQueueOnce
    return celery
Example #42
0
def make_celery(flask_app=None, config_name='prod'):
    from . import create_app

    flask_app = flask_app or create_app(config_name)
    c = Celery(flask_app.import_name)
    c.app = flask_app
    logger.info('celery {} flask app {}'.format(c, c.app))
    c.config_from_object('celeryconf')
    TaskBase = c.Task

    class ContextTask(TaskBase):
        abstract = True

        def __call__(self, *args, **kwargs):
            logger.error('ContextTask {}'.format(flask_app))
            with flask_app.app_context():
                logger.error('ContextTask app context {}'.format(flask_app))
                return TaskBase.__call__(self, *args, **kwargs)

    c.Task = ContextTask
    return c
Example #43
0
def make_celery(app):
    #Celery configuration
    app.config['CELERY_BROKER_URL'] = 'redis://localhost:13800/0'
    app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:13800/0'
    app.config['CELERYBEAT_SCHEDULE'] = {
        'periodic_task-every-minute': {
            'task': 'periodic_task',
            'schedule': crontab(minute="*")
        }
    }

    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
Example #44
0
def create_celery(app):
    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_RESULT_BACKEND'],
        broker=app.config['BROKER_URL']
    )
    celery.conf.update(app.config)
    celery.config_from_object(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
Example #45
0
def make_celery(app):
    celery = Celery(
        app.import_name,
        result_backend=app.config["CELERY_RESULT_BACKEND"],
        broker=app.config["CELERY_BROKER_URL"],
        timezone="Europe/London",
    )

    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
Example #46
0
def create_celery(flask_application):
    """
    Creating the Celery application instance.
    :param obj flask_application: The Flask application instance.
    :return:
    """
    celery_app = Celery(flask_application.import_name,
                        backend=flask_application.config['CELERY_RESULT_BACKEND'],
                        broker=flask_application.config['BROKER_URL'])
    celery_app.conf.update(flask_application.config)
    celery_task = celery_app.Task

    class ContextTask(celery_task):
        abstract = True

        def __call__(self, *args, **kwargs):
            with flask_application.app_context():
                return celery_task.__call__(self, *args, **kwargs)

    celery_app.Task = ContextTask
    return celery_app
Example #47
0
def make_celery(app):
    app.config['CELERYBEAT_SCHEDULE'] = {
        # Executes every minute
        'periodic_task-every-minute': {
            'task': 'periodic_task',
            'schedule': crontab(minute="*")
        }
    }

    celery = Celery(app.import_name, broker='amqp://rabbitmq')
    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
Example #48
0
def get_celery_app(config):
    global _celery_app
    if _celery_app:
        return _celery_app

    _celery_app = Celery(app.import_name,
                         backend=app.config['CELERY_RESULT_BACKEND'],
                         broker=app.config['CELERY_BROKER_URL'])

    _celery_app.conf.update(app.config)

    class ContextTask(_celery_app.Task):
        abstract = True

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

    _celery_app.Task = ContextTask

    return _celery_app
Example #49
0
def make_celery(app):
    # Create and configure the celery app
    celery = Celery(app.import_name,
                    backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(**S.CELERY_CONFIG)

    # Subclass the base Task so that tasks run in a Flask app context
    TaskBase = celery.Task

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

    celery.Task = ContextTask

    # Add the tasks
    Tasks.bind(celery)

    return celery
def make_celery(app=None):
    """
    Create the celery instance from an existing app, or from a new app
    :param app: Flask app if created
    :return:
    """
    app = app or create_app('dev')
    celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task  # pylint: disable=C0103

    class ContextTask(TaskBase):  # pylint: disable=R0903
        """ Inject flask context into the celery object """
        abstract = True

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

    celery.Task = ContextTask
    return celery
Example #51
0
def create_celery_app(app=None):
    """
    Factory method that to place a Celery instance in the context
    of a Flask application

    :param app: :class:`Flask` application instance
    """
    app = app or create_app('longboxed', os.path.dirname(__file__))
    celery = Celery(__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
Example #52
0
def create_celery_app(app=None):
    app = app or create_app('staging')
    celery = Celery(__name__)
    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

    from .tasks import register_tasks

    register_tasks(celery)

    return celery
Example #53
0
def make_celery():

    tasks_app = Celery(
        'telemock',
        broker=BROKER_URI,
#        backend=REDIS_URI,
        include=['tasks']
    )

    flask_app = setup_app()
    TaskBase = tasks_app.Task

    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            """ add flask context to base celery task class """
            with flask_app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)

    tasks_app.Task = ContextTask
    return tasks_app
Example #54
0
def create_celery_app(app=None):
    app = app or create_app('apollo', '', register_all_blueprints=False)
    celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])

    # configure exception logging
    client = Client(app.config.get('SENTRY_DSN', ''))
    register_logger_signal(client)
    register_signal(client)

    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
Example #55
0
def create_celery_app(app: Flask = None) -> 'Celery':

    from alerta.utils.format import register_custom_serializer
    register_custom_serializer()

    app = app or create_app()
    celery = Celery(app.name,
                    backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task

    class ContextTask(TaskBase):  # type: ignore
        abstract = True

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

    celery.Task = ContextTask
    return celery
Example #56
0
def make_celery(app, settings):
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])

    celery.conf.update(app.config)
    celery.config_from_object(celery_config)

    task_base = celery.Task

    class ContextTask(task_base):
        abstract = True

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

    celery.Task = ContextTask

    from src.tasks import init_tasks
    init_tasks(app, celery, settings)

    return celery
Example #57
0
def make_celery(app):
    """
    初始化celery
    http://flask.pocoo.org/docs/1.0/patterns/celery/
    :param app:
    :return:
    """
    from celery import Celery
    celery = Celery(app.import_name,
                    backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = ContextTask

    return celery
Example #58
0
def make_celery(app):
    """Generate celery instance from flask config"""
    celery = Celery(
        app.import_name,  # APP name as str; used only for RabbitMQ to identify your tasks
        broker=app.config['CELERY_BROKER_URL'],  # We'll read them from flask config
        backend=app.config['CELERY_RESULT_BACKEND'],  # We'll read them from flask config
        include=['tasks']  # Here add your tasks file
    )
    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
Example #59
0
def create_celery_app(app):  # pragma: no cover
    app = app or create_app()

    celery = Celery(__name__,
                    backend='amqp',
                    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

    import serendip_api.tasks

    return celery
Example #60
0
def make_celery(app):
    celery = Celery(
        app.import_name,
        backend=app.config['CELERY_RESULT_BACKEND'],
        broker=app.config['CELERY_BROKER_URL']
    )
    class ContextTask(celery.Task):
        abstract = True
        def __call__(self, *args, **kwarg):
            with app.app_context():
                return self.run(*args, **kwarg)
    celery.Task = ContextTask
    celery.conf.update(
        CELERYBEAT_SCHEDULE={
            'destroy-performlogs': {
                'task': 'flaskr.workers.performlogs.destroy_performlogs',
                'schedule': crontab(hour=1, minute=0)
            }
        }
    )
    return celery