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 <cqi@redhat.com>
This commit is contained in:
Chenxiong Qi
2019-11-13 17:17:42 +08:00
committed by mprahl
parent b939d53c57
commit b5bcc981f9
5 changed files with 29 additions and 0 deletions

View File

@@ -92,6 +92,18 @@ class BaseConfiguration(object):
# Disable Client Authorization # Disable Client Authorization
NO_AUTH = False 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): class TestConfiguration(BaseConfiguration):
BUILD_LOGS_DIR = "/tmp" BUILD_LOGS_DIR = "/tmp"
@@ -154,3 +166,6 @@ class OfflineLocalBuildConfiguration(LocalBuildConfiguration):
class DevConfiguration(LocalBuildConfiguration): class DevConfiguration(LocalBuildConfiguration):
DEBUG = True DEBUG = True
LOG_BACKEND = "console" LOG_BACKEND = "console"
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"

View File

@@ -43,6 +43,7 @@ RUN yum -y install \
python-sqlalchemy \ python-sqlalchemy \
python-tox \ python-tox \
python2-distro \ python2-distro \
python2-celery \
python2-libmodulemd2 \ python2-libmodulemd2 \
python2-pyyaml \ python2-pyyaml \
python2-pungi \ python2-pungi \

View File

@@ -10,6 +10,7 @@ RUN dnf -y install \
createrepo_c \ createrepo_c \
rsync \ rsync \
python3-distro \ python3-distro \
python3-celery \
python3-fedmsg \ python3-fedmsg \
python3-kobo-rpmlib \ python3-kobo-rpmlib \
python3-rpm \ python3-rpm \

View File

@@ -19,10 +19,12 @@ for a number of tasks:
""" """
import pkg_resources import pkg_resources
from celery import Celery
from flask import Flask, has_app_context, url_for from flask import Flask, has_app_context, url_for
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.pool import StaticPool from sqlalchemy.pool import StaticPool
from logging import getLogger from logging import getLogger
import gi # noqa import gi # noqa
gi.require_version("Modulemd", "2.0") # noqa gi.require_version("Modulemd", "2.0") # noqa
from gi.repository import Modulemd # noqa from gi.repository import Modulemd # noqa
@@ -46,6 +48,15 @@ app.wsgi_app = ReverseProxy(app.wsgi_app)
conf = init_config(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): class MBSSQLAlchemy(SQLAlchemy):
""" """

View File

@@ -19,3 +19,4 @@ pyOpenSSL
requests requests
six six
sqlalchemy sqlalchemy
celery