Skip to contentSkip to conversation

PRServer: Fixes daemon issues

PATCH New [email protected] 1 participant 4 messages Sep 24, 2015

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/3 prserv/serv: Start/Stop daemon using ip instead of host

In reply to [email protected]

From: Leonardo Sandoval <[email protected]>

In cases where hostname is given instead of an IP (i.e. localhost instead of 127.0.0.1) when stopping the server with bitbake-prserv --stop, the server shows a misleading message indicating that the daemon was not found, where it is actually stopped. This patch converts host to IP values before starting/stopping the daemon, so it will always work on IP, not on hostnames, avoiding problems like the latter.

[YOCTO #8258]

---
 bitbake/lib/prserv/serv.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
bitbake/lib/prserv/serv.py +8 -5
index 0507485..0d96963 100644
@@ -3,6 +3,7 @@ import signal, time
 from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
 import threading
 import Queue
+import socket
 
 try:
     import sqlite3
@@ -37,7 +38,6 @@ singleton = None
 class PRServer(SimpleXMLRPCServer):
     def __init__(self, dbfile, logfile, interface, daemon=True):
         ''' constructor '''
-        import socket
         try:
             SimpleXMLRPCServer.__init__(self, interface,
                                         logRequests=False, allow_none=True)
@@ -261,7 +261,8 @@ class PRServerConnection(object):
         return self.host, self.port
 
 def start_daemon(dbfile, host, port, logfile):
-    pidfile = PIDPREFIX % (host, port)
+    ip = socket.gethostbyname(host)
+    pidfile = PIDPREFIX % (ip, port)
     try:
         pf = file(pidfile,'r')
         pid = int(pf.readline().strip())
@@ -274,12 +275,14 @@ def start_daemon(dbfile, host, port, logfile):
                             % pidfile)
         return 1
 
-    server = PRServer(os.path.abspath(dbfile), os.path.abspath(logfile), (host,port))
+    server = PRServer(os.path.abspath(dbfile), os.path.abspath(logfile), (ip,port))
     server.start()
+
     return 0
 
 def stop_daemon(host, port):
-    pidfile = PIDPREFIX % (host, port)
+    ip = socket.gethostbyname(host)
+    pidfile = PIDPREFIX % (ip, port)
     try:
         pf = file(pidfile,'r')
         pid = int(pf.readline().strip())
@@ -292,7 +295,7 @@ def stop_daemon(host, port):
                         % pidfile)
 
     try:
-        PRServerConnection(host, port).terminate()
+        PRServerConnection(ip, port).terminate()
     except:
         logger.critical("Stop PRService %s:%d failed" % (host,port))
 
Signature · 1 line
1.8.4.5
#0 lore raw

v2 cover PRServer: Fixes daemon issues

From: Leonardo Sandoval <[email protected]>

First two patches fix issues when using hostnames instead of IPs. Third patch disconnect the DB manually, not relaying on this task into a __del__ function. Detail is given on each patch. Fixes 8258, 8560 and 8215

Changes from v1:
* First patch: No changes
* Second patch: outputs the message into stdout instead of stderr. Also removes the 'WARNING' string.
* Third patch: Instead of changing the journal, this patch closes the DB connection explicitly, instead of relying on the __del__ function

The following changes since commit 71b0568fa43285f0946fae93fb43cea5f3bbecec:

rt-tests: drop unnecessary added-missing-dependencies.patch (2015-09-01 11:44:04 +0100)

are available in the git repository at:

git://git.yoctoproject.org/poky-contrib lsandov1/prserver
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=lsandov1/prserver

Leonardo Sandoval (3):
prserv/serv: Start/Stop daemon using ip instead of host prserv/serv.py: Better messaging when starting/stopping the server
with port=0
prserv/serv: Close the DB connection out of class destructor

 bitbake/lib/prserv/db.py   |  2 +-
 bitbake/lib/prserv/serv.py | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.4.5
#2 lore raw

PATCH v2 2/3 prserv/serv.py: Better messaging when starting/stopping the server with port=0

From: Leonardo Sandoval <[email protected]>

When starting the server using port=0, the server actually starts with a different port, so print a message with this new value. When stopping the server with port=0, advise the user which ports the server is listening to, so next time it tries to close it, user can pick up the correct one.

[YOCTO #8560]

---
 bitbake/lib/prserv/serv.py | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
bitbake/lib/prserv/serv.py +21 -2
index 0d96963..3558d4d 100644
@@ -278,9 +278,16 @@ def start_daemon(dbfile, host, port, logfile):
     server = PRServer(os.path.abspath(dbfile), os.path.abspath(logfile), (ip,port))
     server.start()
 
+    # Sometimes, the port (i.e. localhost:0) indicated by the user does not match with
+    # the one the server actually is listening, so at least warn the user about it
+    _,rport = server.getinfo()
+    if port != rport:
+        sys.stdout.write("Server is listening at port %s instead of %s\n"
+                         % (rport,port))
     return 0
 
 def stop_daemon(host, port):
+    import glob
     ip = socket.gethostbyname(host)
     pidfile = PIDPREFIX % (ip, port)
     try:
@@ -291,8 +298,20 @@ def stop_daemon(host, port):
         pid = None
 
     if not pid:
-        sys.stderr.write("pidfile %s does not exist. Daemon not running?\n"
-                        % pidfile)
+        # when server starts at port=0 (i.e. localhost:0), server actually takes another port,
+        # so at least advise the user which ports the corresponding server is listening
+        ports = []
+        portstr = ""
+        for pf in glob.glob(PIDPREFIX % (ip,'*')):
+            bn = os.path.basename(pf)
+            root, _ = os.path.splitext(bn)
+            ports.append(root.split('_')[-1])
+        if len(ports):
+            portstr = "Wrong port? Other ports listening at %s: %s" % (host, ' '.join(ports))
+
+        sys.stderr.write("pidfile %s does not exist. Daemon not running? %s\n"
+                         % (pidfile,portstr))
+        return 1
 
     try:
         PRServerConnection(ip, port).terminate()
Signature · 1 line
1.8.4.5
#3 lore raw

PATCH v2 3/3 prserv/serv: Close the DB connection out of class destructor

In reply to [email protected]

From: Leonardo Sandoval <[email protected]>

When launching the PR server daemon, the PRData __del__ function was being called (no reason found yet) where the DB connection closed, thus following PR updates were not getting into the DB. This patch closes the connection explicitly, not relaying on the __del__ function execution. Closing the connection in turn causes all WAL file transactions to be moved into the database (checkpoint), thus effectively updating the database.

[YOCTO #8215]

---
 bitbake/lib/prserv/db.py   | 2 +-
 bitbake/lib/prserv/serv.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
bitbake/lib/prserv/db.py +1 -1
index 4379580..36c9f7b 100644
@@ -248,7 +248,7 @@ class PRData(object):
         self.connection.execute("PRAGMA journal_mode = WAL;")
         self._tables={}
 
-    def __del__(self):
+    def disconnect(self):
         self.connection.close()
 
     def __getitem__(self,tblname):
bitbake/lib/prserv/serv.py +1 -1
index 3558d4d..08298a6 100644
@@ -141,7 +141,7 @@ class PRServer(SimpleXMLRPCServer):
         while not self.quit:
             self.handle_request()
         self.handlerthread.join()
-        self.table.sync()
+        self.db.disconnect()
         logger.info("PRServer: stopping...")
         self.server_close()
         return
Signature · 1 line
1.8.4.5