Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3/refresh on cache rebuild #361

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions synse_server/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ async def update_device_cache() -> None:
# each registered plugin. This device data will be used to generate
# the cache.
#
# If there are no plugins currently registered, attempt
# to re-register. This can be the case when Synse Server is first
# starting up, being restarted, or is recovering from a networking error.
if len(plugin.manager.plugins) == 0:
logger.debug(_('no plugins found when updating device cache'))
# If there are no plugins currently registered, or any plugin is currently
# marked inactive, attempt to refresh all plugins. This can be the case when
# Synse Server is first starting up, being restarted, or is recovering from a
# networking error.
if not plugin.manager.has_plugins() or not plugin.manager.all_active():
logger.debug(_('refreshing plugins prior to updating device cache'))
plugin.manager.refresh()

async with device_cache_lock:
Expand Down
9 changes: 9 additions & 0 deletions synse_server/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ def refresh(self) -> None:
elapsed_time=time.time() - start,
)

def all_active(self) -> bool:
"""Check to see if all registered plugins are active.

If a single plugin is inactive, this returns False. If no plugins are
registered, this returns True. In such a case, it is up to the caller to
perform additional checks for number of registered plugins.
"""
return all(plugin.active for plugin in self)


# A module-level instance of the plugin manager. This makes it easier to use
# the manager in various places, without having to initialize a new instance.
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,46 @@ def test_get_plugin_found(self):
assert result is not None
assert result == 'placeholder'

def test_all_active_true_no_plugins(self):
m = plugin.PluginManager()
assert m.all_active() is True

def test_all_active_true_has_plugins(self):
p1 = plugin.Plugin({'id': '1', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))
p2 = plugin.Plugin({'id': '2', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))
p3 = plugin.Plugin({'id': '3', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))

p1.mark_active()
p2.mark_active()
p3.mark_active()

m = plugin.PluginManager()
m.plugins = {
'1': p1,
'2': p2,
'3': p3,
}

assert m.all_active() is True

def test_all_active_false_has_plugins(self):
p1 = plugin.Plugin({'id': '1', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))
p2 = plugin.Plugin({'id': '2', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))
p3 = plugin.Plugin({'id': '3', 'tag': 'foo'}, {}, client.PluginClientV3('foo', 'tcp'))

p1.mark_active()
p2.mark_inactive()
p3.mark_active()

m = plugin.PluginManager()
m.plugins = {
'1': p1,
'2': p2,
'3': p3,
}

assert m.all_active() is False

def test_register_fail_metadata_call(self, mocker):
# Mock test data
mock_metadata = mocker.patch(
Expand Down