Notify.php
[Pman.Core] / Notify.php
index 9106b40..c66f6a2 100644 (file)
@@ -85,8 +85,18 @@ 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 $table = 'core_notify';
     var $target = 'Core/NotifySend';
@@ -197,6 +207,7 @@ class Pman_Core_Notify extends Pman
         
         //echo "BATCH SIZE: ".  count($ar) . "\n";
         $pushed = array();
+        $requeue = array();
         while (true) {
             
             
@@ -209,7 +220,7 @@ class Pman_Core_Notify extends Pman
                     break;
                 }
                 $ar = $pushed;
-                $pushed = array();
+                $pushed = false;
                 continue;
             }
             
@@ -220,8 +231,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,13 +257,29 @@ class Pman_Core_Notify extends Pman
             $this->poolfree();
              sleep(3);
         }
+         
+        foreach($requeue as $p) {
+            $pp = clone($p);
+            $p->act_when = strtotime($p->act_when .  ' + 1 MINUTE');
+            $p->update($pp);
+            
+        }
+        
+        
         
         die("DONE\n");
     }
     
     function run($id, $email, $cmdOpts="")
     {
-       // phpinfo();exit;
+        
+        static $renice = false;
+        if (!$renice) {
+            require_once 'System.php';
+            $renice = System::which('renice');
+        }
+        
+        // phpinfo();exit;
         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
         $descriptorspec = array(
             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
@@ -272,6 +304,12 @@ class Pman_Core_Notify extends Pman
         echo "call proc_open $cmd\n";
         
         
+        if ($this->max_pool_size === 1) {
+            passthru($cmd);
+            return;
+        }
+        
+        
         if (!empty($this->opts['dryrun'])) {
             echo "DRY RUN\n";
             return;
@@ -279,6 +317,11 @@ class Pman_Core_Notify extends Pman
         
         $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'],
@@ -365,14 +408,15 @@ 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;
         
     }