Upload bugs solved: elim_dupl_res feature causes generation of a malformed content...
[living-lab-site.git] / cis / cis_lb / load_balancer / base.py
index 9586c88..f4e9261 100644 (file)
@@ -1,6 +1,7 @@
 import threading
 import urllib
 
+import config
 import logger
 
 class LoadBalancer(threading.Thread):
@@ -19,16 +20,29 @@ class LoadBalancer(threading.Thread):
         
         while True:
             (request, data) = self.queue.get()
+            urls = config.CIS_URLS[:]
             
-            cis = self.choose()
-            logger.log_msg('Forwarding to %s' % cis, logger.LOG_LEVEL_DEBUG)
-            urllib.urlopen(cis + request, data)
+            while len(urls) != 0:
+                cis = self.choose(urls)
+                
+                # Request is forwarded to the chosen CIS.
+                try:
+                    urllib.urlopen(cis + request, data)
+                except IOError:
+                    logger.log_msg('Failed to forward request to %s' % cis, \
+                            logger.LOG_LEVEL_ERROR)
+                    continue
+                
+                logger.log_msg('Request forwarded to %s' % cis, \
+                        logger.LOG_LEVEL_INFO)
+                break
             
             self.queue.task_done()
         
-    def choose(self):
+    def choose(self, urls):
         """
         Implement load balancing policy in this method for child classes which
-        choses a CIS from config.CIS_URLS .
+        choses a CIS from urls parameter. The chosen URL should be deleted from
+        urls list.
         """        
         pass
\ No newline at end of file