From b5bcc981f9f75cb78fea675848deca0b9d310736 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Wed, 13 Nov 2019 17:17:42 +0800 Subject: [PATCH] Add celery app instance with base config This patch allows to schedule tasks and launch workers with basic config. To launch a worker: celery -A module_build_service.celery_app -l info Signed-off-by: Chenxiong Qi --- conf/config.py | 15 +++++++++++++++ docker/Dockerfile-tests | 1 + docker/Dockerfile-tests-py3 | 1 + module_build_service/__init__.py | 11 +++++++++++ requirements.txt | 1 + 5 files changed, 29 insertions(+) diff --git a/conf/config.py b/conf/config.py index 9dd80cb8..33c44de1 100644 --- a/conf/config.py +++ b/conf/config.py @@ -92,6 +92,18 @@ class BaseConfiguration(object): # Disable Client Authorization NO_AUTH = False + # Configs for running tasks asynchronously with Celery + # For details of Celery configs, refer to Celery documentation: + # https://docs.celeryproject.org/en/latest/userguide/configuration.html + # + # Each config name consists of namespace CELERY_ and the new Celery config + # name converted to upper case. For example the broker url, Celery config + # name is broker_url, then as you can below, the corresponding config name + # in MBS is CELERY_BROKER_URL. + CELERY_BROKER_URL = "" + CELERY_RESULT_BACKEND = "" + CELERY_IMPORTS = [] + class TestConfiguration(BaseConfiguration): BUILD_LOGS_DIR = "/tmp" @@ -154,3 +166,6 @@ class OfflineLocalBuildConfiguration(LocalBuildConfiguration): class DevConfiguration(LocalBuildConfiguration): DEBUG = True LOG_BACKEND = "console" + + CELERY_BROKER_URL = "redis://localhost:6379/0" + CELERY_RESULT_BACKEND = "redis://localhost:6379/0" diff --git a/docker/Dockerfile-tests b/docker/Dockerfile-tests index 24ee3a24..22a28bde 100644 --- a/docker/Dockerfile-tests +++ b/docker/Dockerfile-tests @@ -43,6 +43,7 @@ RUN yum -y install \ python-sqlalchemy \ python-tox \ python2-distro \ + python2-celery \ python2-libmodulemd2 \ python2-pyyaml \ python2-pungi \ diff --git a/docker/Dockerfile-tests-py3 b/docker/Dockerfile-tests-py3 index 089f4dac..f8f0cde7 100644 --- a/docker/Dockerfile-tests-py3 +++ b/docker/Dockerfile-tests-py3 @@ -10,6 +10,7 @@ RUN dnf -y install \ createrepo_c \ rsync \ python3-distro \ + python3-celery \ python3-fedmsg \ python3-kobo-rpmlib \ python3-rpm \ diff --git a/module_build_service/__init__.py b/module_build_service/__init__.py index 877bc860..236aa8b6 100644 --- a/module_build_service/__init__.py +++ b/module_build_service/__init__.py @@ -19,10 +19,12 @@ for a number of tasks: """ import pkg_resources +from celery import Celery from flask import Flask, has_app_context, url_for from flask_sqlalchemy import SQLAlchemy from sqlalchemy.pool import StaticPool from logging import getLogger + import gi # noqa gi.require_version("Modulemd", "2.0") # noqa from gi.repository import Modulemd # noqa @@ -46,6 +48,15 @@ app.wsgi_app = ReverseProxy(app.wsgi_app) conf = init_config(app) +celery_app = Celery("module-build-service") +# Convert config names specific for Celery like this: +# celery_broker_url -> broker_url +celery_configs = { + name[7:]: getattr(conf, name) + for name in dir(conf) if name.startswith('celery_') +} +celery_app.conf.update(**celery_configs) + class MBSSQLAlchemy(SQLAlchemy): """ diff --git a/requirements.txt b/requirements.txt index da805b96..9ea96f50 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,3 +19,4 @@ pyOpenSSL requests six sqlalchemy +celery