Skip to content

Commit

Permalink
events: implicitly enable caching when creating EventsDispatcher
Browse files Browse the repository at this point in the history
Application that runs EventsDispatcher can safely use also cache , which
greatly improve performance. This is because cache then is properly
updated/invalidated when needed.
Instead of modifying each application to explicitly enable cache based
on this simple rule, make it implicit when EventsDispatcher is created.

Do not enable caching when EventsDispatcher is created only temporarily
in wait_for_domain_shutdown.

QubesOS/qubes-issues#3293
  • Loading branch information
marmarek committed May 18, 2020
1 parent d2ccb75 commit 0fe2af0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions qubesadmin/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@
class EventsDispatcher(object):
''' Events dispatcher, responsible for receiving events and calling
appropriate handlers'''
def __init__(self, app, api_method='admin.Events'):
'''Initialize EventsDispatcher'''
def __init__(self, app, api_method='admin.Events', enable_cache=True):
"""Initialize EventsDispatcher
:param app :py:class:`qubesadmin.Qubes` object
:param api_method Admin API method producing events
:param enable_cache Enable caching (see below)
Connecting :py:class:`EventsDispatcher` object to a
:py:class:`qubesadmin.Qubes` implicitly enables caching. It is important
to actually run the dispatcher (:py:meth:`listen_for_events`), otherwise
the cache won't be updated. Alternatively, disable caching by setting
:py:attr:`qubesadmin.Qubes.cache_enabled` property to `False`.
"""
#: Qubes() object
self.app = app

Expand All @@ -41,6 +52,10 @@ def __init__(self, app, api_method='admin.Events'):
#: event handlers - dict of event -> handlers
self.handlers = {}

if enable_cache:
self.app.register_cache_handlers(self)
self.app.cache_enabled = True

def add_handler(self, event, handler):
'''Register handler for event
Expand Down
2 changes: 1 addition & 1 deletion qubesadmin/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def wait_for_domain_shutdown(vms):
return
app = list(vms)[0].app
vms = set(vms)
events = qubesadmin.events.EventsDispatcher(app)
events = qubesadmin.events.EventsDispatcher(app, enable_cache=False)
events.add_handler('domain-shutdown',
functools.partial(interrupt_on_vm_shutdown, vms))
events.add_handler('connection-established',
Expand Down

0 comments on commit 0fe2af0

Please sign in to comment.