netcelery.py 1.29 KB
Newer Older
1
""" Celery module for libvirt RPC calls. """
Guba Sándor committed
2 3
from celery import Celery
from kombu import Queue, Exchange
4
from os import getenv
5 6 7 8 9 10 11 12 13 14 15 16 17 18

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-n", "--hostname", dest="hostname",
                    help="Define the full queue name with"
                    "with priority", metavar="hostname.queue.priority")
(args, unknwon_args) = parser.parse_known_args()
HOSTNAME = vars(args).pop("hostname")
if HOSTNAME is None:
    raise Exception("You must define hostname as -n <hostname> or "
                    "--hostname=<hostname>.\n"
                    "Hostname format must be hostname.module.priority.")

19
AMQP_URI = getenv('AMQP_URI')
20
CACHE_URI = getenv('CACHE_URI')
21

22 23 24 25 26 27 28

def to_bool(value):
    return value.lower() in ("true", "yes", "y", "t")

lib_connection = None

celery = Celery('netcelery',
29 30
                broker=AMQP_URI,
                include=['netdriver'])
Guba Sándor committed
31 32

celery.conf.update(
33
    CELERY_RESULT_BACKEND='cache',
34
    CELERY_CACHE_BACKEND=CACHE_URI,
Bach Dániel committed
35
    CELERY_TASK_RESULT_EXPIRES=300,
Guba Sándor committed
36
    CELERY_QUEUES=(
37 38
        Queue(HOSTNAME, Exchange(
            'netdriver', type='direct'), routing_key="netdriver"),
Guba Sándor committed
39 40
    )
)
41 42 43 44

if to_bool(getenv('LIBVIRT_KEEPALIVE', "False")):
    import libvirt
    lib_connection = libvirt.open(getenv('LIBVIRT_URI'))