Skip to content
Snippets Groups Projects
Commit 8f951aa5 authored by Erik Reid's avatar Erik Reid
Browse files

common redis connection method

parent 6e934804
No related branches found
No related tags found
No related merge requests found
import redis
def get_redis(config):
return redis.StrictRedis(
host=config['redis']['hostname'],
port=config['redis']['port'])
......@@ -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:
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment