From 8f951aa55a6b758fc7d0181b7a09ac185225afc1 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Fri, 1 Feb 2019 15:32:32 +0100 Subject: [PATCH] common redis connection method --- inventory_provider/tasks/common.py | 8 ++++++++ inventory_provider/tasks/worker.py | 26 +++++++------------------- test/test_celery_worker.py | 2 +- 3 files changed, 16 insertions(+), 20 deletions(-) create mode 100644 inventory_provider/tasks/common.py diff --git a/inventory_provider/tasks/common.py b/inventory_provider/tasks/common.py new file mode 100644 index 00000000..6868ffa3 --- /dev/null +++ b/inventory_provider/tasks/common.py @@ -0,0 +1,8 @@ +import redis + + +def get_redis(config): + return redis.StrictRedis( + host=config['redis']['hostname'], + port=config['redis']['port']) + diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 9923dc94..50a202a4 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -3,10 +3,10 @@ import logging from celery import bootsteps, Task from collections import defaultdict -import redis from lxml import etree from inventory_provider.tasks.app import app +from inventory_provider.tasks.common import get_redis from inventory_provider import config from inventory_provider import constants from inventory_provider import environment @@ -29,9 +29,7 @@ class InventoryTask(Task): def save_key(hostname, key, value): assert isinstance(value, str), \ "sanity failure: expected string data as value" - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) r.hset( name=hostname, key=key, @@ -44,9 +42,7 @@ class InventoryTask(Task): def save_value(key, value): assert isinstance(value, str), \ "sanity failure: expected string data as value" - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) r.set( name=key, value=value) @@ -147,9 +143,7 @@ def update_alarmsdb_cache(self): @app.task() def update_interfaces_to_services(): # todo - factor this connection stuff out - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) with db.connection(InventoryTask.config["ops-db"]) as cx: services = opsdb.get_circuits(cx) @@ -177,9 +171,7 @@ def update_interfaces_to_services(): @app.task() def update_equipment_locations(): # todo - factor this connection stuff out - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) r.delete(equipment_locations_key) with db.connection(InventoryTask.config["ops-db"]) as cx: @@ -191,9 +183,7 @@ def update_equipment_locations(): @app.task() def update_circuit_hierarchy(): # todo - factor this connection stuff out - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) children_to_parents = defaultdict(list) parents_to_children = defaultdict(list) with db.connection(InventoryTask.config["ops-db"]) as cx: @@ -217,9 +207,7 @@ def update_circuit_hierarchy(): @app.task() def update_interface_statuses(): # todo - factor this connection stuff out - r = redis.StrictRedis( - host=InventoryTask.config["redis"]["hostname"], - port=InventoryTask.config["redis"]["port"]) + r = get_redis(InventoryTask.config) with db.connection(InventoryTask.config["ops-db"]) as cx: services = opsdb.get_circuits(cx) with db.connection(InventoryTask.config["alarms-db"]) as cx: diff --git a/test/test_celery_worker.py b/test/test_celery_worker.py index 64b57851..faa76da0 100644 --- a/test/test_celery_worker.py +++ b/test/test_celery_worker.py @@ -36,7 +36,7 @@ def mocked_worker_module(mocker, data_config): MockedRedis.db = {} mocker.patch( - 'inventory_provider.tasks.worker.redis.StrictRedis', + 'inventory_provider.tasks.common.redis.StrictRedis', MockedRedis) -- GitLab