Skip to content

Commit

Permalink
ODI-4932 Fix get_via_cachemanager
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
pius- committed Jan 30, 2020
1 parent 7cb6063 commit 0ae8224
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.4
2.0.5
2 changes: 1 addition & 1 deletion plugnpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""plugnpy - A Simple Python Library for creating Opsview Opspack plugins"""

__version__ = '2.0.4'
__version__ = '2.0.5'
__program_name__ = 'plugnpy'

from .check import Check
Expand Down
6 changes: 5 additions & 1 deletion plugnpy/cachemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def get_via_cachemanager(no_cachemanager, key, ttl, func, *args, **kwargs):
raise ResultError("Failed to connect to cache manager: {0}".format(ex))
data, lock = response['data'], response['lock']
if lock:
data = func(*args, **kwargs)
# Any exceptions in the function call will be stored in the cache manager under the 'error' key
try:
data = func(*args, **kwargs)
except Exception as ex:
data = {'error': str(ex)}
data = json.dumps(data)
CacheManagerUtils.client.set_data(key, data, ttl)
if not data:
Expand Down
17 changes: 17 additions & 0 deletions test/test_cachemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,20 @@ def test_get_via_cachemanager(mocker, no_cachemanager, cachemanager_host, get_da
raises,
expected
)

def test_get_via_cachemanager_exception(mocker):
host = os.environ['OPSVIEW_CACHE_MANAGER_HOST'] = 'host'
port = os.environ['OPSVIEW_CACHE_MANAGER_PORT'] = 'some_port'
namespace = os.environ['OPSVIEW_CACHE_MANAGER_NAMESPACE'] = 'some_namespace'

def func(error):
raise Exception(error)

CacheManagerClient.get_data = mocker.Mock(return_value={'data': None, 'lock': True})
CacheManagerClient.set_data = mocker.Mock()

raise_or_assert(
functools.partial(CacheManagerUtils.get_via_cachemanager, False, 'key', 900, func, "Something went wrong"),
False,
{'error': 'Something went wrong'}
)

0 comments on commit 0ae8224

Please sign in to comment.