Skip to contentSkip to conversation

Improve error reporting for registerEventHandler

PATCH In review Ed Bartosh 2 participants 8 messages Dec 31, 2015 – Jan 4, 2016

lore
Share & tools
lore

Ed Bartosh

author
#0 lore raw

cover Improve error reporting for registerEventHandler

Hi,

This patchset includes refactoring changes for the code that deals with registering UI event handlers. It improves error reporting and cleans up the code a bit.

Ed Bartosh (4):
cooker: add state.get_name method
uievent: add error to registerEventHandler return
uievent: get rid of EventHandler attribute
uievent: refactor retry loop

 bitbake/lib/bb/cooker.py        |  8 ++++++++
 bitbake/lib/bb/server/xmlrpc.py |  4 ++--
 bitbake/lib/bb/ui/uievent.py    | 22 +++++++++++-----------
 3 files changed, 21 insertions(+), 13 deletions(-)

--
Regards,
Ed

author
#1 lore raw

PATCH 1/4 cooker: add state.get_name method

Added method to convert state code into the human readable name. It will be used in logging and error reporting.

bitbake/lib/bb/cooker.py +8 -0
index 4df8881..1d83226 100644
@@ -67,6 +67,14 @@ class CollectionError(bb.BBHandledException):
 class state:
     initial, parsing, running, shutdown, forceshutdown, stopped, error = range(7)
 
+    @classmethod
+    def get_name(cls, code):
+        for name in dir(cls):
+            value = getattr(cls, name)
+            if type(value) == type(cls.initial) and value == code:
+                return name
+        raise ValueError("Invalid status code: %s" % code)
+
 
 class SkippedPackage:
     def __init__(self, info = None, reason = None):
Signature · 1 line
2.1.4
author
#2 lore raw

PATCH 2/4 uievent: add error to registerEventHandler return

In reply to Ed Bartosh

Current code throws Exception("Could not register UI event handler") if event handler can't be registered. The real reason of this is that cooker is in busy state. Error message lacks information about this.

Added error message to the return value of registerEventHandler. Included returned error message into the log message and exception text.

bitbake/lib/bb/server/xmlrpc.py +2 -2
index b7647c1..17eb28b 100644
@@ -97,10 +97,10 @@ class BitBakeServerCommands():
 
         # we don't allow connections if the cooker is running
         if (self.cooker.state in [bb.cooker.state.parsing, bb.cooker.state.running]):
-            return None
+            return None, "Cooker is busy: %s" % bb.cooker.state.get_name(self.cooker.state)
 
         self.event_handle = bb.event.register_UIHhandler(s, True)
-        return self.event_handle
+        return self.event_handle, 'OK'
 
     def unregisterEventHandler(self, handlerNum):
         """
bitbake/lib/bb/ui/uievent.py +5 -3
index 7fc50c7..80686a6 100644
@@ -52,19 +52,21 @@ class BBUIEventQueue:
         # giving up
 
         while self.EventHandler == None and count_tries < 5:
-            self.EventHandle = self.BBServer.registerEventHandler(self.host, self.port)
+            self.EventHandle, error = self.BBServer.registerEventHandler(self.host, self.port)
 
             if (self.EventHandle != None):
                 break
 
-            bb.warn("Could not register UI event handler %s:%d, retry" % (self.host, self.port))
+            errmsg = "Could not register UI event handler. Error: %s, " \
+                     "host %s, port %d" % (error, self.host, self.port)
+            bb.warn("%s, retry" % errmsg)
             count_tries += 1
             import time
             time.sleep(1)
 
 
         if self.EventHandle == None:
-            raise Exception("Could not register UI event handler")
+            raise Exception(errmsg)
 
         self.server = server
 
Signature · 1 line
2.1.4
author
#3 lore raw

PATCH 3/4 uievent: get rid of EventHandler attribute

In reply to Ed Bartosh

This attribute was introduced by mistake. EventHandle is used in the code for the same purpose.

bitbake/lib/bb/ui/uievent.py +2 -2
index 80686a6..a900555 100644
@@ -44,14 +44,14 @@ class BBUIEventQueue:
         server.register_function( self.send_event, "event.sendpickle" )
         server.socket.settimeout(1)
 
-        self.EventHandler = None
+        self.EventHandle = None
         count_tries = 0
 
         # the event handler registration may fail here due to cooker being in invalid state
         # this is a transient situation, and we should retry a couple of times before
         # giving up
 
-        while self.EventHandler == None and count_tries < 5:
+        while self.EventHandle == None and count_tries < 5:
             self.EventHandle, error = self.BBServer.registerEventHandler(self.host, self.port)
 
             if (self.EventHandle != None):
Signature · 1 line
2.1.4
author
#4 lore raw

PATCH 4/4 uievent: refactor retry loop

In reply to Ed Bartosh

Replaced 'while' loop with 'for' loop.
Made the code more compact and hopefully more understandable.

bitbake/lib/bb/ui/uievent.py +6 -8
index a900555..70a923d 100644
@@ -45,29 +45,27 @@ class BBUIEventQueue:
         server.socket.settimeout(1)
 
         self.EventHandle = None
-        count_tries = 0
 
         # the event handler registration may fail here due to cooker being in invalid state
         # this is a transient situation, and we should retry a couple of times before
         # giving up
 
-        while self.EventHandle == None and count_tries < 5:
+        for count_tries in range(5):
+
             self.EventHandle, error = self.BBServer.registerEventHandler(self.host, self.port)
 
-            if (self.EventHandle != None):
+            if self.EventHandle != None:
                 break
 
             errmsg = "Could not register UI event handler. Error: %s, " \
                      "host %s, port %d" % (error, self.host, self.port)
             bb.warn("%s, retry" % errmsg)
-            count_tries += 1
+            if count_tries == 4:
+                raise Exception(errmsg)
+
             import time
             time.sleep(1)
 
-
-        if self.EventHandle == None:
-            raise Exception(errmsg)
-
         self.server = server
 
         self.t = threading.Thread()
Signature · 1 line
2.1.4
#5 lore raw

Re: [PATCH 4/4] uievent: refactor retry loop

Quoted 7 lines · Ed Bartosh
bitbake/lib/bb/ui/uievent.py +2 -0
+        for count_tries in range(5):
+
             self.EventHandle, error =

self.BBServer.registerEventHandler(self.host, self.port)

bitbake/lib/bb/ui/uievent.py +0 -1
-            if (self.EventHandle != None):

bitbake/lib/bb/ui/uievent.py class BBUIEventQueue: · line 65

Quoted 10 lines
bitbake/lib/bb/ui/uievent.py +4 -1
+            if self.EventHandle != None:
                 break
 
             errmsg = "Could not register UI event handler. Error: %s, " \
                      "host %s, port %d" % (error, self.host, self.port)
             bb.warn("%s, retry" % errmsg)
-            count_tries += 1
+            if count_tries == 4:
+                raise Exception(errmsg)
+

I'd just use the else clause of the for loop here, this is just the sort of case it's good for. The else clause is run when the loop finished / didn't break.

for count_tries in range(5):

    # do stuff
    if something:
        break
else:
    # didn't break
    raise Exception(errmsg)
Signature · 5 lines
Christopher Larson
clarson at kergoth dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Senior Software Engineer, Mentor Graphics
author
#6 lore raw

PATCH v2 1/1 uievent: refactor retry loop

Replaced 'while' loop with 'for' loop.
Made the code more compact and hopefully more understandable.

---
 bitbake/lib/bb/ui/uievent.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
bitbake/lib/bb/ui/uievent.py +5 -7
index 19a9301..af239a3 100644
@@ -45,27 +45,25 @@ class BBUIEventQueue:
         server.socket.settimeout(1)
 
         self.EventHandle = None
-        count_tries = 0
 
         # the event handler registration may fail here due to cooker being in invalid state
         # this is a transient situation, and we should retry a couple of times before
         # giving up
 
-        while self.EventHandle == None and count_tries < 5:
+        for count_tries in range(5):
+
             self.EventHandle, error = self.BBServer.registerEventHandler(self.host, self.port)
 
-            if (self.EventHandle != None):
+            if self.EventHandle != None:
                 break
 
             errmsg = "Could not register UI event handler. Error: %s, host %s, "
                      "port %d" % (error, self.host, self.port))
             bb.warn("%s, retry" % errmsg)
-            count_tries += 1
+
             import time
             time.sleep(1)
-
-
-        if self.EventHandle == None:
+        else:
             raise Exception(errmsg)
 
         self.server = server
Signature · 1 line
2.1.4
author
#7 lore raw

PATCH v3 4/4 uievent: refactor retry loop

Replaced 'while' loop with 'for' loop.
Made the code more compact and hopefully more understandable.

---
 bitbake/lib/bb/ui/uievent.py | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
bitbake/lib/bb/ui/uievent.py +7 -9
index 19a9301..4670cdb 100644
@@ -45,27 +45,25 @@ class BBUIEventQueue:
         server.socket.settimeout(1)
 
         self.EventHandle = None
-        count_tries = 0
 
         # the event handler registration may fail here due to cooker being in invalid state
         # this is a transient situation, and we should retry a couple of times before
         # giving up
 
-        while self.EventHandle == None and count_tries < 5:
+        for count_tries in range(5):
+
             self.EventHandle, error = self.BBServer.registerEventHandler(self.host, self.port)
 
-            if (self.EventHandle != None):
+            if self.EventHandle != None:
                 break
 
-            errmsg = "Could not register UI event handler. Error: %s, host %s, "
-                     "port %d" % (error, self.host, self.port))
+            errmsg = "Could not register UI event handler. Error: %s, host %s, "\
+                     "port %d" % (error, self.host, self.port)
             bb.warn("%s, retry" % errmsg)
-            count_tries += 1
+
             import time
             time.sleep(1)
-
-
-        if self.EventHandle == None:
+        else:
             raise Exception(errmsg)
 
         self.server = server
Signature · 1 line
2.1.4