[Uludag-commits] r15274 - in trunk/staj-projeleri/buildfarm: . helpers

uludag-commits at pardus.org.tr uludag-commits at pardus.org.tr
3 Eyl 2007 Pzt 17:01:55 EEST


Author: ozan.caglayan
Date: Mon Sep  3 17:01:55 2007
New Revision: 15274

Modified:
   trunk/staj-projeleri/buildfarm/client.py
   trunk/staj-projeleri/buildfarm/helpers/qmanager.py
Log:
locks seems to work.

Modified: trunk/staj-projeleri/buildfarm/client.py
=================================================================
--- trunk/staj-projeleri/buildfarm/client.py	(original)
+++ trunk/staj-projeleri/buildfarm/client.py	Mon Sep  3 17:01:55 2007
@@ -98,21 +98,23 @@
 
 def client(op, **kwargs):
     
-    # Used for identifying server return codes in a user friendly manner
-    returnStrings = {'build' : [_("Build process is successfully finished!"),
-                                _("Buildfarm is busy!"),
-                                _("Work Queue is empty!"),
-                                _("Queue finished with problems :(")],
-                     'add'   : [_("%s successfully added to the work queue!"),
-                                _("The package '%s' doesn't exist!"),
-                                _("The package '%s' is already in the work queue!")]
+    # Used for identifying server return codes in a user friendly manner 
+    returnStrings = {'build'    : [_("Build process is successfully finished!"),
+                                   None,
+                                   _("Work Queue is empty!"),
+                                   _("Queue finished with problems!")],
+                     'add'      : [_("'%s' successfully added to the work queue!"),
+                                   None,
+                                   _("The package '%s' doesn't exist!"),
+                                   _("The package '%s' is already in the work queue!")],
+                     'remove'   : [_("Removed '%s' from %s queue!"),
+                                   None,
+                                   _("'%s' doesn't exist in the %s queue!")],
+                     'transfer' : [_("'%s' is successfully transferred to %s queue!"),
+                                   None,
+                                   _("'%s' doesn't exist in the %s queue!")]
                     }
-                                
-                                
-                                
     
-    # TODO : I can build a list of all formatted strings and select them
-    # with the return values provided by the server to minimize the code.
     funcString = None
     cmd = kwargs.get('cmd', None)
     pspecList = kwargs.get('pspec', None)
@@ -141,14 +143,6 @@
             print_("The repositories are already synchronized.")
             
     # 2 Parameters
-    
-    elif op == "send":
-        # pspec is a directory which can contain 1 or more packages
-        retval = sendDirectory(server, kwargs[pspec], "ozan")
-        if retval:
-            print _("Everything's OK")
-        else:
-            print _("There were problems during the process")
         
     elif op == "list":
         funcString = "get" + cmd.capitalize() + "Queue"
@@ -163,33 +157,37 @@
         funcString = "build" + cmd.capitalize()
         print _("Building %s..." % cmd)
         retval = server.__getattr__(funcString)()
-        print returnStrings['build'][retval]
+        if retval == 1:
+            print _("Buildfarm is busy!")
+        else:
+            print returnStrings['build'][retval]
     
     # 3 or more Parameters
     elif op == "add":
         for pspec in pspecList:
             retval = server.appendToWorkQueue(pspec, True)
-            print (returnStrings['add'][retval] % pspec)
+            if retval == 1:
+               print _("Buildfarm is busy!")
+            else:
+                print (returnStrings['add'][retval] % pspec)
             
     elif op == "remove":
         funcString = "removeFrom" + cmd.capitalize() + "Queue"
         for pspec in pspecList:    
-            print _("Removing '%s' from %s queue.." % (pspec, cmd)),
             retval = server.__getattr__(funcString)(pspec)
-            if retval:
-                print _("[Removed]")
+            if retval == 1:
+               print _("Buildfarm is busy!")
             else:
-                print _("[Doesn't exist]")
+                print returnStrings['remove'][retval] % (pspec, cmd)
             
     elif op == "transfer":
         funcString = "transferTo" + cmd.capitalize() + "Queue"
         for pspec in pspecList:
-            print _("Transferring '%s' to %s queue.." % (pspec, cmd)),
             retval = server.__getattr__(funcString)(pspec)
-            if retval:
-                print _("[Transferred]")
+            if retval == 1:
+               print _("Buildfarm is busy!")
             else:
-                print _("[Doesn't exist]")
+                print returnStrings['transfer'][retval] % (pspec, cmd)
             
 if __name__ == "__main__":
 
@@ -216,7 +214,7 @@
         if args[0] in ("send"):
             client(args[0],pspec=args[1])
         elif args[0] == "list" and args[1] in ("work","wait") or \
-            args[0] == "build" and args[1] in ("index","packages"):
+             args[0] == "build" and args[1] in ("index","packages"):
             client(args[0],cmd=args[1])
         else:
             usage()

Modified: trunk/staj-projeleri/buildfarm/helpers/qmanager.py
=================================================================
--- trunk/staj-projeleri/buildfarm/helpers/qmanager.py	(original)
+++ trunk/staj-projeleri/buildfarm/helpers/qmanager.py	Mon Sep  3 17:01:55 2007
@@ -93,7 +93,28 @@
     def __initWaitQueueFromFile__(self):
         self.waitQueue = []
         self.__deserialize__(self.waitQueue, "waitQueue")
+        
+    def __tryToLock__(self, depth):
+        # find the caller of the caller
+        # this method will always be called from these 6 methods:
+        # transferToWaitQueue(),transferToWorkQueue()
+        # appendToWaitQueue(),appendToWorkQueue()
+        # removeFromWaitQueue(),removeFromWorkQueue()
+        # if the request to this function from stack depth 'depth'
+        # is coming from the xmlrpc client, the method will try to acquire
+        # the lock and raise exception if unsuccessful.
+        f = sys._getframe(depth)
+        methodName = f.f_code.co_name
+        print methodName
+        if methodName == "_dispatch":
+            try:
+                self.locks["build"].lock(timeout=0)
+                return True
+            except:
+                return False
+        return True
 
+    # Getter functions can't brake the integrity of the queues.
     def getWorkQueue(self):
         self.__initWorkQueueFromFile__()
         return self.workQueue
@@ -111,28 +132,39 @@
         return False
 
     def removeFromWorkQueue(self, pspec):
-        self.__initWorkQueueFromFile__()    
+        if not self.__tryToLock__(2):
+            return 1
+        self.__initWorkQueueFromFile__()
         if self.workQueue.__contains__(pspec):
             self.workQueue.remove(pspec)
             self.__serialize__(self.workQueue, "workQueue")
-            return True
-        return False
+            self.locks['build'].unlock()
+            return 0
+        self.locks['build'].unlock()
+        return 2
 
     def appendToWorkQueue(self, pspec, checkIfExists=False):
         # 0: Successful
-        # 1: Package doesn't exist
-        # 2: Package is already in the queue
+        # 1: Buildfarm is busy
+        # 2: Package doesn't exist
+        # 3: Package is already in the queue
+        if not self.__tryToLock__(2):
+            return 1
         if checkIfExists:
             if not os.path.isfile(os.path.join(config.localPspecRepo, pspec)):
-                return 1
+                self.locks['build'].unlock()
+                return 2
             
         self.__initWorkQueueFromFile__()
         if not self.workQueue.__contains__(pspec):
             self.workQueue.append(pspec)
             self.__serialize__(self.workQueue, "workQueue")
+            self.locks['build'].unlock()
             return 0
-        return 2
+        self.locks['build'].unlock()
+        return 3
 
+    # Can't be invoked from clients so doesn't need introspection.
     def appendToWaitQueue(self, pspec):
         self.__initWaitQueueFromFile__()
         if not self.waitQueue.__contains__(pspec):
@@ -145,19 +177,17 @@
         self.__initWaitQueueFromFile__()
         if self.waitQueue.__contains__(pspec) and self.appendToWorkQueue(pspec):
             self.removeFromWaitQueue(pspec)
-            return True
-        return False
+            return 0
+        return 1
 
     def transferToWaitQueue(self, pspec):
-        f = sys._getframe(1)
-        methodName = f.f_code.co_name
-        print methodName
-        
+        if not self.__tryToLock__(2):
+            return 1
         self.__initWorkQueueFromFile__()
         if self.workQueue.__contains__(pspec) and self.appendToWaitQueue(pspec):
             self.removeFromWorkQueue(pspec)
-            return True
-        return False
+            return 0
+        return 2
     
     def buildArchive(self, dirname, filename, d, username=""):
         


Uludag-commits mesaj listesiyle ilgili daha fazla bilgi