Skip to content

Commit 0ae8224

Browse files
committed
ODI-4932 Fix get_via_cachemanager
- If an exception was raised inside the 'func' passed into the get_via_cachemanger, the set_data call would never happen. - This meant that the next call to the cachemanager would not be able to get the lock, resulting in the call timing out. - Any exceptions that are raised in the func are now caught and the error message is stored in the cachemanager under the 'error' key.
1 parent 7cb6063 commit 0ae8224

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.4
1+
2.0.5

plugnpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""plugnpy - A Simple Python Library for creating Opsview Opspack plugins"""
22

3-
__version__ = '2.0.4'
3+
__version__ = '2.0.5'
44
__program_name__ = 'plugnpy'
55

66
from .check import Check

plugnpy/cachemanager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ def get_via_cachemanager(no_cachemanager, key, ttl, func, *args, **kwargs):
5151
raise ResultError("Failed to connect to cache manager: {0}".format(ex))
5252
data, lock = response['data'], response['lock']
5353
if lock:
54-
data = func(*args, **kwargs)
54+
# Any exceptions in the function call will be stored in the cache manager under the 'error' key
55+
try:
56+
data = func(*args, **kwargs)
57+
except Exception as ex:
58+
data = {'error': str(ex)}
5559
data = json.dumps(data)
5660
CacheManagerUtils.client.set_data(key, data, ttl)
5761
if not data:

test/test_cachemanager.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,20 @@ def test_get_via_cachemanager(mocker, no_cachemanager, cachemanager_host, get_da
4848
raises,
4949
expected
5050
)
51+
52+
def test_get_via_cachemanager_exception(mocker):
53+
host = os.environ['OPSVIEW_CACHE_MANAGER_HOST'] = 'host'
54+
port = os.environ['OPSVIEW_CACHE_MANAGER_PORT'] = 'some_port'
55+
namespace = os.environ['OPSVIEW_CACHE_MANAGER_NAMESPACE'] = 'some_namespace'
56+
57+
def func(error):
58+
raise Exception(error)
59+
60+
CacheManagerClient.get_data = mocker.Mock(return_value={'data': None, 'lock': True})
61+
CacheManagerClient.set_data = mocker.Mock()
62+
63+
raise_or_assert(
64+
functools.partial(CacheManagerUtils.get_via_cachemanager, False, 'key', 900, func, "Something went wrong"),
65+
False,
66+
{'error': 'Something went wrong'}
67+
)

0 commit comments

Comments
 (0)