class CeleryDriverBeat(CeleryDriver): def _setup_logging(self, *args, **kwargs): from logging.config import dictConfig dictConfig(get_logging_config(self.config)) def _prepare(self): uvloop.install() self._app = Celery(self.config.BEAT_BLUEPRINT) self._app.conf.update(self.config.get_beat_config()) # Setup celery signals signals.setup_logging.connect(self._setup_logging) for method in self._methods: if method.schedule is None: continue self._app._add_periodic_task(method.name, method.schedule.entry(self._get_name_task(method.name))) def run(self, *args, **kwargs): self._prepare() self.loop.run_until_complete(self._call_hooks(CeleryHookNames.before_beat_start)) try: self._app.Beat().run() except Exception as e: print(e) finally: self.loop.run_until_complete(self._call_hooks(CeleryHookNames.after_beat_start))
class BaseCeleryApp: """ Celery worker application implementation """ BASE_BROKER_URL = 'pyamqp://{user}:{password}@{host}:{port}//' def __init__(self, app_name: str, task_imports: List[str] = None): """ Initialize the celery application Args: app_name: Celery application name task_imports: List of module routes where the Celery tasks are located """ self.app = Celery(app_name, include=task_imports) def configure(self, task_queue_name: str = None, broker_config: dict = None, worker_concurrency: int = None, result_backend_url: str = None): """ Configure the current celery application in order to set the broker service configuration and the worker concurrency number Args: task_queue_name: name of the default task queue broker_config: broker service configuration worker_concurrency: number of worker concurrent threads result_backend_url: url of the results backend """ config_dict = dict() if task_queue_name: config_dict['task_default_queue'] = task_queue_name if broker_config: broker_url = self.BASE_BROKER_URL.format(**broker_config) config_dict['broker_url'] = broker_url if worker_concurrency: config_dict['worker_concurrency'] = worker_concurrency if result_backend_url: config_dict['result_backend'] = result_backend_url if len(config_dict.keys()): self.app.config_from_object(config_dict) def run(self, beat: bool = False): """ Run the associated celery app Returns: """ sys.argv = [sys.argv[0]] self.app.Worker().start() if not beat else self.app.Beat().run()
def make_celery(app): celery = Celery( app.import_name, ) 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 celery.Beat = celery.subclass_with_self(FlaskBeat) return celery