diff --git a/.gitignore b/.gitignore index ac609e94c4c0429a379fc1dfcd42874ba146a093..d8cc93fdfee743e40090463be17a739ede4b1354 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ coverage.xml .coverage .tox htmlcov +dist diff --git a/inventory_provider/config.py b/inventory_provider/config.py index 9162ab9b29191ba1cc78c6f971a7f939b1bce4bb..4311d91c9aaa6217001d01dea1cc5fd75b8931c6 100644 --- a/inventory_provider/config.py +++ b/inventory_provider/config.py @@ -16,7 +16,11 @@ CONFIG_SCHEMA = { }, "required": ["hostname", "dbname", "username", "password"], "additionalProperties": False - + }, + "timeout": { + "type": "number", + "maximum": 10, # sanity + "exclusiveMinimum": 0 } }, @@ -37,7 +41,8 @@ CONFIG_SCHEMA = { "type": "object", "properties": { "hostname": {"type": "string"}, - "port": {"type": "integer"} + "port": {"type": "integer"}, + "socket_timeout": {"$ref": "#/definitions/timeout"} }, "required": ["hostname", "port"], "additionalProperties": False @@ -47,7 +52,9 @@ CONFIG_SCHEMA = { "properties": { "hostname": {"type": "string"}, "port": {"type": "integer"}, - "name": {"type": "string"} + "name": {"type": "string"}, + "redis_socket_timeout": {"$ref": "#/definitions/timeout"}, + "sentinel_socket_timeout": {"$ref": "#/definitions/timeout"} }, "required": ["hostname", "port", "name"], "additionalProperties": False diff --git a/inventory_provider/tasks/common.py b/inventory_provider/tasks/common.py index f4492f61d3be7dfbd6a4cede91017697b3efc4d0..3a474c8e13d03042c3aeaaff94e1eeeb07cc5797 100644 --- a/inventory_provider/tasks/common.py +++ b/inventory_provider/tasks/common.py @@ -7,6 +7,9 @@ import redis.sentinel logger = logging.getLogger(__name__) +DEFAULT_REDIS_SENTINEL_TIMEOUT = 0.1 +DEFAULT_SENTINEL_SOCKET_TIMEOUT = 0.1 + DB_LATCH_SCHEMA = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", @@ -102,19 +105,23 @@ def _get_redis(config, dbid=None): dbid = min(config['redis-databases']) kwargs = { - 'db': dbid, - 'socket_timeout': 0.1 + 'db': dbid } if 'sentinel' in config: + kwargs['socket_timeout'] = config['sentinel'].get( + 'sentinel_socket_timeout', DEFAULT_SENTINEL_SOCKET_TIMEOUT) sentinel = redis.sentinel.Sentinel([( config['sentinel']['hostname'], config['sentinel']['port'])], **kwargs) return sentinel.master_for( config['sentinel']['name'], - socket_timeout=0.1) + socket_timeout=config['sentinel'].get( + 'redis_socket_timeout', DEFAULT_REDIS_SENTINEL_TIMEOUT)) else: + kwargs['socket_timeout'] = config['redis'].get( + 'socket_timeout', DEFAULT_REDIS_SENTINEL_TIMEOUT) return redis.StrictRedis( host=config['redis']['hostname'], port=config['redis']['port'], diff --git a/requirements.txt b/requirements.txt index d69f5f2538655392513d419e24131d8881dba55b..42ac28a07e66cfa9f67fdcc6e4f497e531b42a3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ click mysql-connector pysnmp -jsonschema +jsonschema==3.2.0 paramiko flask redis==3.2.1 diff --git a/setup.py b/setup.py index 4d09acb37786f9fa0140343e9d3f5f7194a1e707..698adfaf6647c1fd4cbe9a6c5ab63e20a4f35119 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ setup( 'click', 'mysql-connector', 'pysnmp', - 'jsonschema', + 'jsonschema==3.2.0', 'paramiko', 'flask', 'redis==3.2.1', diff --git a/test/conftest.py b/test/conftest.py index 856d4f46bb0d1073d7d839bbb2875db221c4bc16..0726f549347bcef6c443393c82eaa634281ea8dc 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -38,7 +38,8 @@ def data_config_filename(): }, "redis": { "hostname": "xxxxxx", - "port": 6379 + "port": 6379, + "socket_timeout": 2.8 }, "redis-databases": [0, 7], "junosspace": {