Notify.php
[Pman.Core] / Notify.php
index 610d8b7..8d13d28 100644 (file)
@@ -85,9 +85,23 @@ class Pman_Core_Notify extends Pman
             'max' => 100,
         ),
     );
-    
+    /**
+     * @var $nice_level Unix 'nice' level to stop it jamming server up.
+     */
+    var $nice_level = false;
+    /**
+     * @var $max_pool_size maximum runners to start at once.
+     */
     var $max_pool_size = 10;
+    /**
+     * @var $max_to_domain maximum connections to make to a single domain
+     */
+    var $max_to_domain = 10;
     
+    /**
+     * @var $maxruntime - maximum time a child is allowed to run - defaut 2 minutes
+     */
+    var $maxruntime = 120; 
     var $table = 'core_notify';
     var $target = 'Core/NotifySend';
     var $evtype = ''; // any notification...
@@ -118,7 +132,7 @@ class Pman_Core_Notify extends Pman
         }
         
         if (empty($opts['limit'])) {
-            $opts['limit'] = '1000';
+            $opts['limit'] = '1000'; // not sure why it's not picking up the defautl..
         }
         //date_default_timezone_set('UTC');
        // phpinfo();exit;
@@ -151,12 +165,13 @@ class Pman_Core_Notify extends Pman
         //DB_DataObject::debugLevel(1);
         $w = DB_DataObject::factory($this->table);
         
+        
         if (!$showold) {
             
             // standard
             
             //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
-            $w->whereAdd("sent < '1970-01-01'"); // eg.. sent is not valid..
+            $w->whereAdd("sent < '1970-01-01' OR sent IS NULL"); // eg.. sent is not valid..
             
             $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
             if (!$this->force) {
@@ -165,7 +180,7 @@ class Pman_Core_Notify extends Pman
     
             $w->orderBy('act_when ASC'); // oldest first.
             
-            echo "QUEUE is {$w->count()}\n";
+            $this->logecho("QUEUE is {$w->count()}");
             
             $w->limit($opts['limit']); // we can run 1000 ...
         } else {
@@ -189,28 +204,29 @@ class Pman_Core_Notify extends Pman
                 $o = $w->object();
                 
                 
-                echo "$w->id : $w->person_id_email email    : ".
-                        $o->toEventString()."    ". $w->status() . "\n";
+                $this->logecho("$w->id : $w->person_id_email email    : ".
+                        $o->toEventString()."    ". $w->status()  );
             }
             exit;
         }
         
         //echo "BATCH SIZE: ".  count($ar) . "\n";
         $pushed = array();
+        $requeue = array();
         while (true) {
             
             
-            echo "BATCH SIZE: ".  count($ar) . "\n";
+            $this->logecho("BATCH SIZE: ".  count($ar) );
             
             if (empty($ar)) {
-                echo "COMPLETED MAIN QUEUE - running delated\n";
+                $this->logecho("COMPLETED MAIN QUEUE - running delated");
                 
                 if (empty($pushed)) {
                     break;
                 }
                 $ar = $pushed;
-                $pushed = array();
-                break;
+                $pushed = false;
+                continue;
             }
             
             
@@ -220,8 +236,13 @@ class Pman_Core_Notify extends Pman
                 sleep(3);
                 continue;
             }
-            if ($this->poolHasDomain($p->person_id_email)) {
+            if ($this->poolHasDomain($p->person_id_email) > $this->max_to_domain) {
                 
+                if ($pushed === false) {
+                    // we only try once to requeue..
+                    $requeue[] = $p;
+                    continue;
+                }
                 $pushed[] = $p;
                 
                 
@@ -241,20 +262,44 @@ class Pman_Core_Notify extends Pman
             $this->poolfree();
              sleep(3);
         }
+         
+        foreach($requeue as $p) {
+            $pp = clone($p);
+            $p->act_when = $p->sqlValue('NOW + INTERVAL 1 MINUTE');
+            $p->update($pp);
+            
+        }
+        
         
-        die("DONE\n");
+        $this->logecho("DONE");
+        exit;
     }
     
     function run($id, $email, $cmdOpts="")
     {
-       // phpinfo();exit;
-        $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
+        
+        static $renice = false;
+        if (!$renice) {
+            require_once 'System.php';
+            $renice = System::which('renice');
+        }
+        
+        // phpinfo();exit;
+        $tnx = tempnam(ini_get('session.save_path'),'stdout');
+        unlink($tnx);
+        $tn =  $tnx . '.stdout';
         $descriptorspec = array(
             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
             1 => array("file", $tn, 'w'),  // stdout is a pipe that the child will write to
             2 => array("pipe", "w") // stderr is a file to write to
          );
-        $php = $_SERVER["_"];
+        
+        static $php = false;
+        if (!$php) {
+            require_once 'System.php';
+            $php = System::which('php');
+        }
+        
         $sn =  $_SERVER["SCRIPT_NAME"];
         
         $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
@@ -269,16 +314,27 @@ class Pman_Core_Notify extends Pman
         
        
         $pipe = array();
-        echo "call proc_open $cmd\n";
+        $this->logecho("call proc_open $cmd");
+        
+        
+        if ($this->max_pool_size === 1) {
+            passthru($cmd);
+            return;
+        }
         
         
         if (!empty($this->opts['dryrun'])) {
-            echo "DRY RUN\n";
+            $this->logecho("DRY RUN");
             return;
         }
         
         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
         $info =  proc_get_status($p);
+        
+        if ($this->nice_level !== false) { 
+            $rcmd = "$renice {$this->nice_level} {$info['pid']}";
+            `$rcmd`;
+        } 
         $this->pool[] = array(
                 'proc' => $p,
                 'pid' => $info['pid'],
@@ -290,7 +346,7 @@ class Pman_Core_Notify extends Pman
             
                 
         );
-        echo "RUN ({$info['pid']}) $cmd  \n";
+        $this->logecho("RUN ({$info['pid']}) $cmd ");
     }
     
     function poolfree()
@@ -307,7 +363,7 @@ class Pman_Core_Notify extends Pman
             
             // update if necessday.
             if ($info['pid'] && $p['pid'] != $info['pid']) {
-                echo "CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']. "\n";
+                $this->logecho("CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']);
                 $p['pid'] = $info['pid'];
             }
             
@@ -324,7 +380,7 @@ class Pman_Core_Notify extends Pman
                     //fclose($p['pipes'][1]);
                     fclose($p['pipes'][0]);
                     fclose($p['pipes'][2]);
-                    echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+                    $this->logecho("TERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']));
                     @unlink($p['out']);
                     
                     continue;
@@ -346,11 +402,11 @@ class Pman_Core_Notify extends Pman
             //    $pool[] = $p;
             //    continue;
             //}
-            echo "\nENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+            $this->logecho("ENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) );
             @unlink($p['out']);
             //unlink($p['out']);
         }
-        echo "POOL SIZE: ". count($pool) ."\n";
+        $this->logecho("POOL SIZE: ". count($pool) );
         $this->pool = $pool;
         if (count($pool) < $this->max_pool_size) {
             return true;
@@ -365,19 +421,25 @@ class Pman_Core_Notify extends Pman
      */
     function poolHasDomain($email)
     {
+        $ret = 0;
         $dom = strtolower(array_pop(explode('@',$email)));
         foreach($this->pool as $p) {
             $mdom = strtolower(array_pop(explode('@',$p['email'])));
             if ($mdom == $dom) {
-                return true;
+                $ret++;
             }
         }
-        return false;
+        return $ret;
         
     }
 
     function output()
     {
-        die("Done\n");
+        $this->logecho("DONE");
+        exit;
+    }
+    function logecho($str)
+    {
+        echo date("Y-m-d H:i:s - ") . $str . "\n";
     }
 }
\ No newline at end of file