Notify.php
[Pman.Core] / Notify.php
index 8d2e241..1b00e61 100644 (file)
@@ -65,7 +65,7 @@ class Pman_Core_Notify extends Pman
         ),
          'limit' => array(
             'desc' => 'Limit search for no. to send to ',
-            'default' => 50,
+            'default' => 1000,
             'short' => 'L',
             'min' => 0,
             'max' => 999,
@@ -77,8 +77,16 @@ class Pman_Core_Notify extends Pman
             'min' => 0,
             'max' => 0,
         ),
+        'poolsize' => array(
+            'desc' => 'Pool size',
+            'default' => 10,
+            'short' => 'P',
+            'min' => 0,
+            'max' => 100,
+        ),
     );
     
+    
     var $max_pool_size = 10;
     
     var $table = 'core_notify';
@@ -106,8 +114,13 @@ class Pman_Core_Notify extends Pman
             print_r($opts);
         }
         $this->opts = $opts;
+        if (!empty($opts['poolsize'])) {
+            $this->max_pool_size = $opts['poolsize'];
+        }
         
-        
+        if (empty($opts['limit'])) {
+            $opts['limit'] = '1000'; // not sure why it's not picking up the defautl..
+        }
         //date_default_timezone_set('UTC');
        // phpinfo();exit;
         $showold = !empty($opts['old']);
@@ -152,7 +165,10 @@ class Pman_Core_Notify extends Pman
             }
     
             $w->orderBy('act_when ASC'); // oldest first.
-            $w->limit(1000); // we can run 1000 ...
+            
+            echo "QUEUE is {$w->count()}\n";
+            
+            $w->limit($opts['limit']); // we can run 1000 ...
         } else {
             $w->orderBy('act_when DESC'); // latest first
             $w->limit($opts['limit']); // we can run 1000 ...
@@ -180,33 +196,42 @@ class Pman_Core_Notify extends Pman
             exit;
         }
         
-        
+        //echo "BATCH SIZE: ".  count($ar) . "\n";
         $pushed = array();
+        $requeue = array();
         while (true) {
+            
+            
+            echo "BATCH SIZE: ".  count($ar) . "\n";
+            
             if (empty($ar)) {
-                break;
+                echo "COMPLETED MAIN QUEUE - running delated\n";
+                
+                if (empty($pushed)) {
+                    break;
+                }
+                $ar = $pushed;
+                $pushed = false;
+                continue;
             }
             
             
             $p = array_shift($ar);
             if (!$this->poolfree()) {
                 array_unshift($ar,$p); /// put it back on..
-                sleep(1);
+                sleep(3);
                 continue;
             }
             if ($this->poolHasDomain($p->person_id_email)) {
-                if (in_array($p->person_id_email, $pushed)) {
-                    // it's been pushed to the end, and nothing has
-                    // been pushed since.s
-                    // give up, let the next run sort it out.
-                    break;
-                }
                 
-                $ar[] = $p; // push it on the end..
+                if ($pushed === false) {
+                    // we only try once to requeue..
+                    $requeue[] = $p;
+                    continue;
+                }
+                $pushed[] = $p;
                 
-                $pushed[] = $p->person_id_email;
                 
-                echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
                 //sleep(3);
                 continue;
             }
@@ -216,22 +241,37 @@ class Pman_Core_Notify extends Pman
             
             
             
-            $pushed = array();
-            
         }
         
         // we should have a time limit here...
         while(count($this->pool)) {
             $this->poolfree();
-             sleep(1);
+             sleep(3);
         }
         
+        
+        foreach($requeue as $p) {
+            $pp = clone($p);
+            $p->event_when = strtotime($p->event_when .  ' + 1 MINUTE');
+            $p->update($pp);
+            
+        }
+        
+        
+        
         die("DONE\n");
     }
     
     function run($id, $email, $cmdOpts="")
     {
-       // phpinfo();exit;
+        
+        static $renice = fase;
+        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
@@ -256,6 +296,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;
@@ -263,6 +309,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 19 {$info['pid']}";
+            `$rcmd`;
+        } 
         $this->pool[] = array(
                 'proc' => $p,
                 'pid' => $info['pid'],
@@ -290,12 +341,12 @@ class Pman_Core_Notify extends Pman
             //var_dump($info);
             
             // update if necessday.
-            if ($info['pid']) {
+            if ($info['pid'] && $p['pid'] != $info['pid']) {
                 echo "CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']. "\n";
                 $p['pid'] = $info['pid'];
             }
             
-            echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
+            //echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
             
             if ($info['running']) {