From aea41511de5d2b6ef75504f282186b6a17131a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 18 May 2020 04:28:01 +0200 Subject: [PATCH] events: implicitly enable caching when creating EventsDispatcher 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 --- qubesadmin/events/__init__.py | 18 ++++++++++++++++-- qubesadmin/events/utils.py | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/qubesadmin/events/__init__.py b/qubesadmin/events/__init__.py index 9cd8b08..834e35b 100644 --- a/qubesadmin/events/__init__.py +++ b/qubesadmin/events/__init__.py @@ -31,8 +31,19 @@ import qubesadmin.exc 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 @@ -41,6 +52,9 @@ class EventsDispatcher(object): #: event handlers - dict of event -> handlers self.handlers = {} + if enable_cache: + self.app.cache_enabled = True + def add_handler(self, event, handler): '''Register handler for event diff --git a/qubesadmin/events/utils.py b/qubesadmin/events/utils.py index c6c8e4d..92d62f7 100644 --- a/qubesadmin/events/utils.py +++ b/qubesadmin/events/utils.py @@ -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',