better logging
[Pman.Core] / Notify.php
index 46f7e8f..69dd1cd 100644 (file)
@@ -124,7 +124,7 @@ class Pman_Core_Notify extends Pman
     var $evtype = ''; // any notification...
                     // this script should only handle EMAIL notifications..
                     
-                    
+    var $opts; 
     var $force = false;
     function getAuth()
     {
@@ -132,7 +132,7 @@ class Pman_Core_Notify extends Pman
         if (!$ff->cli) {
             die("access denied");
         }
-        HTML_FlexyFramework::ensureSingle($_SERVER["SCRIPT_NAME"] .'|'. __FILE__, $this);
+        HTML_FlexyFramework::ensureSingle($_SERVER["SCRIPT_NAME"] .'|'. __FILE__ .'|'. (empty($_SERVER['argv'][1]) ? '': $_SERVER['argv'][1]), $this);
         return true;
     }
     
@@ -174,7 +174,8 @@ class Pman_Core_Notify extends Pman
         
         $this->generateNotifications();
         
-         
+        $this->assignQueues();
+        
         //DB_DataObject::debugLevel(1);
         $w = DB_DataObject::factory($this->table);
         $total = 0;
@@ -207,6 +208,17 @@ class Pman_Core_Notify extends Pman
             $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();
         
@@ -261,9 +273,11 @@ class Pman_Core_Notify extends Pman
                 
                 if ($pushed === false) {
                     // we only try once to requeue..
+                    $this->logecho("REQUEING - maxed out that domain - {$email}");
                     $requeue[] = $p;
                     continue;
                 }
+                $this->logecho("PUSHING - maxed out that domain - {$email}");
                 $pushed[] = $p;
                 
                 
@@ -287,6 +301,7 @@ class Pman_Core_Notify extends Pman
         foreach($requeue as $p) {
             $pp = clone($p);
             $p->act_when = $p->sqlValue('NOW + INTERVAL 1 MINUTE');
+            $this->updateServer($p);
             $p->update($pp);
             
         }
@@ -296,6 +311,21 @@ class Pman_Core_Notify extends Pman
         exit;
     }
     
+    // this sequentially distributes requeued emails.. - to other servers.
+    function updateServer($w)
+    {
+        $ff = HTML_FlexyFramework::get();
+        static $num = 0;
+        if (empty($ff->Core_Notify['servers'])) {
+            return;
+        }
+        $num++;
+        // next server..
+        $w->server_id = $num % count(array_keys($ff->Core_Notify['servers']));
+         
+    }
+  
+    
     function generateNotifications()
     {
         // this should check each module for 'GenerateNotifications.php' class..
@@ -330,7 +360,38 @@ class Pman_Core_Notify extends Pman
     
     }
     
-    
+    function assignQueues()
+    {
+        $ff = HTML_FlexyFramework::get();
+        
+        if (empty($ff->Core_Notify['servers'])) {
+            return;
+        }
+        
+        $num_servers = count(array_keys($ff->Core_Notify['servers']));
+        $p = DB_DataObject::factory($this->table);
+        // 6 seconds on this machne...
+        $p->query("
+            UPDATE
+                {$this->table}
+            SET
+                server_id = ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})
+            WHERE
+                sent < '2000-01-01'
+                and
+                event_id = 0
+                and
+                act_start < NOW()
+                and
+                server_id < 0
+            ORDER BY
+                id ASC
+            LIMIT
+                20000
+        ");
+
+        
+    }
     
     function run($id, $email='', $cmdOpts="")
     {