fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_notify_server.php
index 485a428..33b73d1 100644 (file)
@@ -84,6 +84,11 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
         }
         
         $ns = DB_DataObject::factory('core_notify_server');
+        if (!$ns->count()) {
+            $ns->id = 0;
+            return $ns;
+        }
+        
         
         $ns->poolname = $notify->poolname;
         $ns->is_active = 1;
@@ -111,6 +116,10 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     
     function isFirstServer()
     {
+        if (!$this->id) {
+            return true;
+        }
+        
         $servers = $this->availableServers();
         if (empty($servers)) {
             return false;
@@ -123,10 +132,13 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     // called on current server.
     function assignQueues($notify)
     {
-         
+        if (!$this->id) {
+            return true;
+        }
         
         $servers = $this->availableServers();
         $ids = array();
+        $up = array();
         foreach($servers as $s) {
             $ids[] = $s->id;
         }
@@ -199,22 +211,33 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
         $p->selectAdd('server_id, count(id) as  n');
         $p->groupBy('server_id');
         $in_q = $p->fetchAll('server_id', 'n');
+        
+        // if queue is empty it will not get allocated anything.
+        foreach($ids as $sid) {
+            if (!isset($in_q[$sid])) {
+                $in_q[$sid] = 0;
+            }
+        }
         $totalq = 0;
         foreach($in_q as $sid => $n) {
             $totalq += $n;
         }
         
+        
         // new average queue
         $target_len = floor(  ($totalq + $total_add) / $num_servers );
         
         foreach($in_q as $sid => $cq) {
-            if ( $target_len - $cq < 1) {
+            if ( $cq > $target_len) {
                 continue;
             }
             $up[ $sid ] = array_slice($to_add, 0, $target_len - $cq);
         }
-        foreach($to_add as $i) {
-            $up[  $ids[0] ][] = $i;
+        
+        // add the reminder evently
+        foreach($to_add as $n=>$i) {
+            
+            $up[  $ids[$n % $num_servers] ][] = $i;
         }
         
         // distribution needs to go to ones that have the shortest queues. - so to balance out the queues
@@ -253,6 +276,10 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     
     function updateNotifyToNextServer( $cn , $when = false, $allow_same = false)
     {
+        if (!$this->id) {
+            return;
+        }
+        
         // fixme - this should take into account blacklisted - and return false if no more servers are available
         $email = empty($cn->to_email) ? ($cn->person() ? $cn->person()->email : $cn->to_email) : $cn->to_email;
 
@@ -275,6 +302,8 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
                 $good = $s;
                 break;
             }
+            $offset = ($offset + 1)  % count($servers);
+            var_dump($offset);
         }
         if ($good == false && $allow_same) {
             $good = $this;
@@ -296,14 +325,17 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     
     function isBlacklisted($email)
     {
+        if (!$this->id) {
+            return false;
+        }
+        
         // return current server id..
         static $cache = array();
-        
-        // get the domain..
+         // get the domain..
         $ea = explode('@',$email);
         $dom = strtolower(array_pop($ea));
-        if (isset($cache[$dom])) {
-            return $cache[$dom];
+        if (isset( $cache[$this->id . '-'. $dom])) {
+            return  $cache[$this->id . '-'. $dom];
         }
         
         $cd = DB_DataObject::factory('core_domain')->loadOrCreate($dom);
@@ -312,7 +344,7 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
         $bl->server_id = $this->id;
         $bl->domain_id = $cd->id;
         if ($bl->count()) {
-            $cache[$dom] = true;
+            $cache[$this->id . '-'. $dom] = true;
             return true;
         }
         
@@ -320,12 +352,18 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     }
     function initHelo()
     {
+        if (!$this->id) {
+            return;
+        }
         $ff = HTML_FlexyFramework::get();
         $ff->Mail['helo'] = $this->helo;
         
     }
     function checkSmtpResponse($errmsg, $core_domain)
     {
+        if (!$this->id) {
+            return false;
+        }
         $bl = DB_DataObject::factory('core_notify_blacklist');
         $bl->server_id = $this->id;
         $bl->domain_id = $core_domain->id;