Skip to contentSkip to conversation

event: Queue offline events for the UI

PATCH New Richard Purdie 1 participant 2 messages Jul 7, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 event: Queue offline events for the UI

Messages printed when no UI is connected (e.g. memres) are currently lost. Use the existing queue mechanism to queue these until a UI attaches, then replay them. This isn't ideal but better than the current situation of losing them entirely.

---
 lib/bb/event.py          | 11 ++++++++++-
 lib/bb/server/process.py |  2 +-
 lib/bb/server/xmlrpc.py  |  2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)
lib/bb/event.py +10 -1
index 1462382..d5c5ef3 100644
@@ -212,6 +212,12 @@ def fire(event, d):
     if worker_fire:
         worker_fire(event, d)
     else:
+        # If messages have been queued up, clear the queue
+        global _uiready, ui_queue
+        if _uiready and ui_queue:
+            for queue_event in ui_queue:
+                fire_ui_handlers(queue_event, d)
+            ui_queue = []
         fire_ui_handlers(event, d)
 
 def fire_from_worker(event, d):
@@ -291,7 +297,10 @@ def register_UIHhandler(handler, mainui=False):
     _ui_logfilters[_ui_handler_seq] = UIEventFilter(level, debug_domains)
     return _ui_handler_seq
 
-def unregister_UIHhandler(handlerNum):
+def unregister_UIHhandler(handlerNum, mainui=False):
+    if mainui:
+        global _uiready
+        _uiready = False
     if handlerNum in _ui_handlers:
         del _ui_handlers[handlerNum]
     return
lib/bb/server/process.py +1 -1
index 9ca2b69..f8d6767 100644
@@ -137,7 +137,7 @@ class ProcessServer(Process, BaseImplServer):
                 logger.exception('Running command %s', command)
 
         self.event_queue.close()
-        bb.event.unregister_UIHhandler(self.event_handle.value)
+        bb.event.unregister_UIHhandler(self.event_handle.value, True)
         self.command_channel.close()
         self.cooker.shutdown(True)
         self.quitout.close()
lib/bb/server/xmlrpc.py +1 -1
index a06007f..d0f5a6d 100644
@@ -125,7 +125,7 @@ class BitBakeServerCommands():
         """
         Unregister a remote UI Event Handler
         """
-        return bb.event.unregister_UIHhandler(handlerNum)
+        return bb.event.unregister_UIHhandler(handlerNum, True)
 
     def runCommand(self, command):
         """
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 server/xmlrpc: Add Heartbeat event support

When heartbeat event support was added it was only added to process.py. Add it to server/xmlrpc too. There is duplicated code however since we're likely to combine the server abstractions soon its not worth worrying about now.

This ensures the backends have the same event support.

[YOCTO #10741]

---
 lib/bb/server/xmlrpc.py | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
lib/bb/server/xmlrpc.py +26 -0
index d0f5a6d..1a475e0 100644
@@ -227,6 +227,8 @@ class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
         self.idle_timeout = idle_timeout
         if idle_timeout:
             self.register_idle_function(self.handle_idle_timeout, self)
+        self.heartbeat_seconds = 1 # default, BB_HEARTBEAT_EVENT will be checked once we have a datastore.
+        self.next_heartbeat = time.time()
 
     def addcooker(self, cooker):
         BaseImplServer.addcooker(self, cooker)
@@ -250,6 +252,15 @@ class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
         return []
 
     def serve_forever(self):
+        heartbeat_event = self.cooker.data.getVar('BB_HEARTBEAT_EVENT')
+        if heartbeat_event:
+            try:
+                self.heartbeat_seconds = float(heartbeat_event)
+            except:
+                # Throwing an exception here causes bitbake to hang.
+                # Just warn about the invalid setting and continue
+                bb.warn('Ignoring invalid BB_HEARTBEAT_EVENT=%s, must be a float specifying seconds.' % heartbeat_event)
+
         # Start the actual XMLRPC server
         bb.cooker.server_main(self.cooker, self._serve_forever)
 
@@ -297,6 +308,21 @@ class XMLRPCServer(SimpleXMLRPCServer, BaseImplServer):
                 # we ignore interrupted calls
                 pass
 
+            # Create new heartbeat event?
+            now = time.time()
+            if now >= self.next_heartbeat:
+                # We might have missed heartbeats. Just trigger once in
+                # that case and continue after the usual delay.
+                self.next_heartbeat += self.heartbeat_seconds
+                if self.next_heartbeat <= now:
+                    self.next_heartbeat = now + self.heartbeat_seconds
+                heartbeat = bb.event.HeartbeatEvent(now)
+                bb.event.fire(heartbeat, self.cooker.data)
+            if nextsleep and now + nextsleep > self.next_heartbeat:
+                # Shorten timeout so that we we wake up in time for
+                # the heartbeat.
+                nextsleep = self.next_heartbeat - now
+
         # Tell idle functions we're exiting
         for function, data in list(self._idlefuns.items()):
             try:
Signature · 1 line
2.7.4