summaryrefslogtreecommitdiff
path: root/src/buildstream/_messenger.py
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2020-07-04 12:13:24 +0000
committerBenjamin Schubert <contact@benschubert.me>2020-08-22 12:52:00 +0000
commit48a4891293c188ac55cd7d08287956d119dbb01f (patch)
tree5417d63be7a4703c1d8515084aa98fdb078a0097 /src/buildstream/_messenger.py
parent4929020e85b68c3446ddc276821219fc1f687d18 (diff)
downloadbuildstream-48a4891293c188ac55cd7d08287956d119dbb01f.tar.gz
_messenger.py: Make `timed_suspendable` public and use it in job.pybschubert/timed-suspendable
This reduces the amount of code duplication
Diffstat (limited to 'src/buildstream/_messenger.py')
-rw-r--r--src/buildstream/_messenger.py60
1 files changed, 30 insertions, 30 deletions
diff --git a/src/buildstream/_messenger.py b/src/buildstream/_messenger.py
index 3a32a2467..805f56b5b 100644
--- a/src/buildstream/_messenger.py
+++ b/src/buildstream/_messenger.py
@@ -160,7 +160,7 @@ class Messenger:
#
@contextmanager
def timed_activity(self, activity_name, *, element_name=None, detail=None, silent_nested=False):
- with self._timed_suspendable() as timedata:
+ with self.timed_suspendable() as timedata:
try:
# Push activity depth for status messages
message = Message(MessageType.START, activity_name, detail=detail, element_name=element_name)
@@ -204,7 +204,7 @@ class Messenger:
if not full_name:
full_name = activity_name
- with self._timed_suspendable() as timedata:
+ with self.timed_suspendable() as timedata:
try:
message = Message(MessageType.START, activity_name, element_name=element_name)
self.message(message)
@@ -326,6 +326,34 @@ class Messenger:
def get_log_filename(self):
return self._log_filename
+ # timed_suspendable()
+ #
+ # A contextmanager that allows an activity to be suspended and can
+ # adjust for clock drift caused by suspending
+ #
+ # Yields:
+ # TimeData: An object that contains the time the activity started
+ #
+ @contextmanager
+ def timed_suspendable(self):
+ # Note: timedata needs to be in a namedtuple so that values can be
+ # yielded that will change
+ timedata = _TimeData(start_time=datetime.datetime.now())
+ stopped_time = None
+
+ def stop_time():
+ nonlocal stopped_time
+ stopped_time = datetime.datetime.now()
+
+ def resume_time():
+ nonlocal timedata
+ nonlocal stopped_time
+ sleep_time = datetime.datetime.now() - stopped_time
+ timedata.start_time += sleep_time
+
+ with _signals.suspendable(stop_time, resume_time):
+ yield timedata
+
# _record_message()
#
# Records the message if recording is enabled
@@ -388,31 +416,3 @@ class Messenger:
if self._render_status_cb and now >= self._next_render:
self._render_status_cb()
self._next_render = now + _RENDER_INTERVAL
-
- # _timed_suspendable()
- #
- # A contextmanager that allows an activity to be suspended and can
- # adjust for clock drift caused by suspending
- #
- # Yields:
- # TimeData: An object that contains the time the activity started
- #
- @contextmanager
- def _timed_suspendable(self):
- # Note: timedata needs to be in a namedtuple so that values can be
- # yielded that will change
- timedata = _TimeData(start_time=datetime.datetime.now())
- stopped_time = None
-
- def stop_time():
- nonlocal stopped_time
- stopped_time = datetime.datetime.now()
-
- def resume_time():
- nonlocal timedata
- nonlocal stopped_time
- sleep_time = datetime.datetime.now() - stopped_time
- timedata.start_time += sleep_time
-
- with _signals.suspendable(stop_time, resume_time):
- yield timedata