reduce number of update queries
[Pman.Core] / Notify.php
index 8c176a6..684b4ea 100644 (file)
@@ -183,6 +183,21 @@ class Pman_Core_Notify extends Pman
         $w = DB_DataObject::factory($this->table);
         $total = 0;
         
+        
+        
+        $ff = HTML_FlexyFramework::get();
+        if (!empty($ff->Core_Notify['servers'])) {
+            if (!isset($ff->Core_Notify['servers'][gethostname()])) {
+                $this->jerr("Core_Notify['servers']['" . gethostname() ."'] is not set");
+            }
+            $w->server_id = array_search(gethostname(),array_keys($ff->Core_Notify['servers']));
+        }
+        if (!empty($this->evtype)) {
+            $w->evtype = $this->evtype;
+        }
+        
+        
+        
         if (!empty($opts['old'])) {
             // show old and new...
             
@@ -207,23 +222,13 @@ class Pman_Core_Notify extends Pman
             $w->limit($opts['limit']); // we can run 1000 ...
         }
         
-        if (!empty($this->evtype)) {
-            $w->evtype = $this->evtype;
-        }
         
-        $ff = HTML_FlexyFramework::get();
-        if (!empty($ff->Core_Notify['servers'])) {
-            if (!isset($ff->Core_Notify['servers'][gethostname()])) {
-                $this->jerr("Core_Notify['servers']['" . gethostname() ."'] is not set");
-            }
-            $w->server_id = array_search(gethostname(),array_keys($ff->Core_Notify['servers']));
-        }
         
     
         
          
         $w->autoJoin();
-        $w->find();
+        $total = $w->find();
         
         
         
@@ -250,7 +255,7 @@ class Pman_Core_Notify extends Pman
                 $total--;
             }
             
-            $this->logecho("BATCH SIZE: ".  (count($this->queue) + $total) );
+            $this->logecho("BATCH SIZE: Queue=".  count($this->queue) . " TOTAL = " . $total  );
             
             if (empty($this->queue)) {
                 $this->logecho("COMPLETED MAIN QUEUE - running maxed out domains");
@@ -288,20 +293,27 @@ class Pman_Core_Notify extends Pman
             
             
         }
+         $this->logecho("REQUEUING all emails that maxed out:" . count($this->next_queue));
+        if (!empty($this->next_queue)) {
+             
+            foreach($this->next_queue as $p) {
+                $pp = clone($p);
+                $p->act_when = $p->sqlValue('NOW + INTERVAL 1 MINUTE');
+                $this->updateServer($p);
+                $p->update($pp);
+                
+            }
+        }
+        
         
+        $this->logecho("QUEUE COMPLETE - waiting for pool to end");
         // we should have a time limit here...
         while(count($this->pool)) {
             $this->poolfree();
             sleep(3);
         }
          
-        foreach($this->next_queue as $p) {
-            $pp = clone($p);
-            $p->act_when = $p->sqlValue('NOW + INTERVAL 1 MINUTE');
-            $this->updateServer($p);
-            $p->update($pp);
-            
-        }
+        
         
         
         $this->logecho("DONE");
@@ -367,6 +379,20 @@ class Pman_Core_Notify extends Pman
         
         $num_servers = count(array_keys($ff->Core_Notify['servers']));
         $p = DB_DataObject::factory($this->table);
+        $p->whereAdd("
+                sent < '2000-01-01'
+                and
+                event_id = 0
+                and
+                act_start < NOW() +  INTERVAL 3 HOUR 
+                and
+                server_id < 0"
+            
+        );
+        if ($p->count() < 1) {
+            return;
+        }
+        
         // 6 seconds on this machne...
         $p->query("
             UPDATE
@@ -378,13 +404,13 @@ class Pman_Core_Notify extends Pman
                 and
                 event_id = 0
                 and
-                act_start < NOW()
+                act_start < NOW() +  INTERVAL 3 HOUR 
                 and
                 server_id < 0
             ORDER BY
                 id ASC
             LIMIT
-                20000
+                10000
         ");
 
         
@@ -433,7 +459,7 @@ class Pman_Core_Notify extends Pman
         
         
         if ($this->max_pool_size === 1) {
-            this->logecho("call passthru [{$email}] $cmd");
+            $this->logecho("call passthru [{$email}] $cmd");
             passthru($cmd);
             return;
         }