From b60fffebf563edfe258cbb893754197af1ac5a0b Mon Sep 17 00:00:00 2001 From: Angelos Evripiotis Date: Tue, 2 Jul 2019 15:01:50 +0100 Subject: Refactor: message handlers take 'is_silenced' Remove the need to pass the Context object to message handlers, by passing what is usually requested from the context instead. This paves the way to sharing less information with some child jobs - they won't need the whole context object, just the messenger. --- src/buildstream/_context.py | 13 +++++++++---- src/buildstream/_frontend/app.py | 6 +++--- src/buildstream/_messenger.py | 21 +++++++++++++-------- src/buildstream/_scheduler/jobs/job.py | 8 ++++---- tests/artifactcache/pull.py | 2 +- tests/artifactcache/push.py | 2 +- tests/internals/loader.py | 2 +- tests/internals/pluginloading.py | 2 +- tests/sourcecache/fetch.py | 2 +- tests/sourcecache/push.py | 2 +- tests/sourcecache/staging.py | 2 +- 11 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/buildstream/_context.py b/src/buildstream/_context.py index de1f83d32..8d8509f27 100644 --- a/src/buildstream/_context.py +++ b/src/buildstream/_context.py @@ -438,8 +438,13 @@ class Context(): # Sets the handler for any status messages propagated through # the context. # - # The message handler should have the same signature as - # the message() method + # The handler should have the signature: + # + # def handler( + # message: _message.Message, # The message to send. + # is_silenced: bool, # Whether messages are currently being silenced. + # ) -> None + # def set_message_handler(self, handler): self._messenger.set_message_handler(handler) @@ -460,7 +465,7 @@ class Context(): # message: A Message object # def message(self, message): - self._messenger.message(message, context=self) + self._messenger.message(message) # silence() # @@ -487,7 +492,7 @@ class Context(): @contextmanager def timed_activity(self, activity_name, *, unique_id=None, detail=None, silent_nested=False): with self._messenger.timed_activity( - activity_name, context=self, unique_id=unique_id, detail=detail, silent_nested=silent_nested): + activity_name, unique_id=unique_id, detail=detail, silent_nested=silent_nested): yield # recorded_messages() diff --git a/src/buildstream/_frontend/app.py b/src/buildstream/_frontend/app.py index dafde153c..52d5f2d47 100644 --- a/src/buildstream/_frontend/app.py +++ b/src/buildstream/_frontend/app.py @@ -695,11 +695,11 @@ class App(): # # Handle messages from the pipeline # - def _message_handler(self, message, context): + def _message_handler(self, message, is_silenced): # Drop status messages from the UI if not verbose, we'll still see # info messages and status messages will still go to the log files. - if not context.log_verbose and message.message_type == MessageType.STATUS: + if not self.context.log_verbose and message.message_type == MessageType.STATUS: return # Hold on to the failure messages @@ -707,7 +707,7 @@ class App(): self._fail_messages[message.unique_id] = message # Send to frontend if appropriate - if self.context.silent_messages() and (message.message_type not in unconditional_messages): + if is_silenced and (message.message_type not in unconditional_messages): return if self._status: diff --git a/src/buildstream/_messenger.py b/src/buildstream/_messenger.py index 6dcb649d7..069ca124a 100644 --- a/src/buildstream/_messenger.py +++ b/src/buildstream/_messenger.py @@ -42,8 +42,13 @@ class Messenger(): # Sets the handler for any status messages propagated through # the context. # - # The message handler should have the same signature as - # the message() method + # The handler should have the signature: + # + # def handler( + # message: _message.Message, # The message to send. + # is_silenced: bool, # Whether messages are currently being silenced. + # ) -> None + # def set_message_handler(self, handler): self._message_handler = handler @@ -66,7 +71,7 @@ class Messenger(): # Args: # message: A Message object # - def message(self, message, context): + def message(self, message): # Tag message only once if message.depth is None: @@ -80,7 +85,7 @@ class Messenger(): # to the frontend) assert self._message_handler - self._message_handler(message, context=context) + self._message_handler(message, is_silenced=self.silent_messages()) # silence() # @@ -109,7 +114,7 @@ class Messenger(): # silent_nested (bool): If specified, nested messages will be silenced # @contextmanager - def timed_activity(self, activity_name, context, *, unique_id=None, detail=None, silent_nested=False): + def timed_activity(self, activity_name, *, unique_id=None, detail=None, silent_nested=False): starttime = datetime.datetime.now() stopped_time = None @@ -128,7 +133,7 @@ class Messenger(): try: # Push activity depth for status messages message = Message(unique_id, MessageType.START, activity_name, detail=detail) - self.message(message, context) + self.message(message) self._push_message_depth(silent_nested) yield @@ -138,13 +143,13 @@ class Messenger(): elapsed = datetime.datetime.now() - starttime message = Message(unique_id, MessageType.FAIL, activity_name, elapsed=elapsed) self._pop_message_depth() - self.message(message, context) + self.message(message) raise elapsed = datetime.datetime.now() - starttime message = Message(unique_id, MessageType.SUCCESS, activity_name, elapsed=elapsed) self._pop_message_depth() - self.message(message, context) + self.message(message) # recorded_messages() # diff --git a/src/buildstream/_scheduler/jobs/job.py b/src/buildstream/_scheduler/jobs/job.py index 51531de64..e970481ab 100644 --- a/src/buildstream/_scheduler/jobs/job.py +++ b/src/buildstream/_scheduler/jobs/job.py @@ -828,16 +828,16 @@ class ChildJob(): # the message back to the parent process for further propagation. # # Args: - # message (Message): The message to log - # context (Context): The context object delegating this message + # message (Message): The message to log + # is_silenced (bool) : Whether messages are silenced # - def _child_message_handler(self, message, context): + def _child_message_handler(self, message, is_silenced): message.action_name = self.action_name message.task_id = self._task_id # Send to frontend if appropriate - if context.silent_messages() and (message.message_type not in unconditional_messages): + if is_silenced and (message.message_type not in unconditional_messages): return if message.message_type == MessageType.LOG: diff --git a/tests/artifactcache/pull.py b/tests/artifactcache/pull.py index 0b57a6c9a..b42af9e6e 100644 --- a/tests/artifactcache/pull.py +++ b/tests/artifactcache/pull.py @@ -24,7 +24,7 @@ DATA_DIR = os.path.join( # Handle messages from the pipeline -def message_handler(message, context): +def message_handler(message, is_silenced): pass diff --git a/tests/artifactcache/push.py b/tests/artifactcache/push.py index 862393f35..996091fca 100644 --- a/tests/artifactcache/push.py +++ b/tests/artifactcache/push.py @@ -23,7 +23,7 @@ DATA_DIR = os.path.join( # Handle messages from the pipeline -def message_handler(message, context): +def message_handler(message, is_silenced): pass diff --git a/tests/internals/loader.py b/tests/internals/loader.py index 006b5787f..f0b4f2622 100644 --- a/tests/internals/loader.py +++ b/tests/internals/loader.py @@ -13,7 +13,7 @@ DATA_DIR = os.path.join( ) -def dummy_handler(message, context): +def dummy_handler(message, is_silenced): pass diff --git a/tests/internals/pluginloading.py b/tests/internals/pluginloading.py index 2d997077a..fa0b9afe8 100644 --- a/tests/internals/pluginloading.py +++ b/tests/internals/pluginloading.py @@ -19,7 +19,7 @@ def create_pipeline(tmpdir, basedir, target): context.casdir = os.path.join(str(tmpdir), 'cas') project = Project(basedir, context) - def dummy_handler(message, context): + def dummy_handler(message, is_silenced): pass context.set_message_handler(dummy_handler) diff --git a/tests/sourcecache/fetch.py b/tests/sourcecache/fetch.py index cd8a3e989..d7145d7f4 100644 --- a/tests/sourcecache/fetch.py +++ b/tests/sourcecache/fetch.py @@ -34,7 +34,7 @@ from tests.testutils import create_artifact_share DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "project") -def message_handler(message, context): +def message_handler(message, is_silenced): pass diff --git a/tests/sourcecache/push.py b/tests/sourcecache/push.py index 4c652d21e..af4e262f0 100644 --- a/tests/sourcecache/push.py +++ b/tests/sourcecache/push.py @@ -35,7 +35,7 @@ from tests.testutils import create_artifact_share DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "project") -def message_handler(message, context): +def message_handler(message, is_silenced): pass diff --git a/tests/sourcecache/staging.py b/tests/sourcecache/staging.py index 318285292..cffd0bb58 100644 --- a/tests/sourcecache/staging.py +++ b/tests/sourcecache/staging.py @@ -35,7 +35,7 @@ from tests.testutils.element_generators import create_element_size DATA_DIR = os.path.dirname(os.path.realpath(__file__)) -def dummy_message_handler(message, context): +def dummy_message_handler(message, is_silenced): pass -- cgit v1.2.1