Notify.php
[Pman.Core] / Notify.php
index e33ad7f..b45f862 100644 (file)
@@ -98,6 +98,11 @@ class Pman_Core_Notify extends Pman
      */
     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...
@@ -109,9 +114,8 @@ class Pman_Core_Notify extends Pman
         if (!$ff->cli) {
             die("access denied");
         }
-        HTML_FlexyFramework::ensureSingle(__FILE__, $this);
+        HTML_FlexyFramework::ensureSingle($_SERVER["SCRIPT_NAME"] .'|'. __FILE__, $this);
         return true;
-        
     }
     
     var $pool = array();
@@ -176,7 +180,7 @@ class Pman_Core_Notify extends Pman
     
             $w->orderBy('act_when ASC'); // oldest first.
             
-            $this->Log("QUEUE is {$w->count()}");
+            $this->logecho("QUEUE is {$w->count()}");
             
             $w->limit($opts['limit']); // we can run 1000 ...
         } else {
@@ -191,7 +195,7 @@ class Pman_Core_Notify extends Pman
         
         
         $ar = $w->fetchAll();
-        
+        print_r($ar);exit;
         if (!empty($opts['list'])) {
             if (empty($ar)) {
                 die("Nothing in Queue\n");
@@ -200,7 +204,7 @@ class Pman_Core_Notify extends Pman
                 $o = $w->object();
                 
                 
-                $this->log("$w->id : $w->person_id_email email    : ".
+                $this->logecho("$w->id : $w->person_id_email email    : ".
                         $o->toEventString()."    ". $w->status()  );
             }
             exit;
@@ -212,10 +216,10 @@ class Pman_Core_Notify extends Pman
         while (true) {
             
             
-            $this->log("BATCH SIZE: ".  count($ar) );
+            $this->logecho("BATCH SIZE: ".  count($ar) );
             
             if (empty($ar)) {
-                $this->log("COMPLETED MAIN QUEUE - running delated");
+                $this->logecho("COMPLETED MAIN QUEUE - running delated");
                 
                 if (empty($pushed)) {
                     break;
@@ -267,8 +271,8 @@ class Pman_Core_Notify extends Pman
         }
         
         
-        
-        die("DONE\n");
+        $this->logecho("DONE");
+        exit;
     }
     
     function run($id, $email, $cmdOpts="")
@@ -310,7 +314,7 @@ class Pman_Core_Notify extends Pman
         
        
         $pipe = array();
-        $this->log("call proc_open $cmd");
+        $this->logecho("call proc_open $cmd");
         
         
         if ($this->max_pool_size === 1) {
@@ -320,7 +324,7 @@ class Pman_Core_Notify extends Pman
         
         
         if (!empty($this->opts['dryrun'])) {
-            $this->log("DRY RUN");
+            $this->logecho("DRY RUN");
             return;
         }
         
@@ -342,15 +346,14 @@ class Pman_Core_Notify extends Pman
             
                 
         );
-        $this->log("RUN ({$info['pid']}) $cmd ");
+        $this->logecho("RUN ({$info['pid']}) $cmd ");
     }
     
     function poolfree()
     {
         $pool = array();
         clearstatcache();
-        $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
-        
+         
         foreach($this->pool as $p) {
              
             //echo "CHECK PID: " . $p['pid'] . "\n";
@@ -359,7 +362,7 @@ class Pman_Core_Notify extends Pman
             
             // update if necessday.
             if ($info['pid'] && $p['pid'] != $info['pid']) {
-                $this->log("CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']);
+                $this->logecho("CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']);
                 $p['pid'] = $info['pid'];
             }
             
@@ -370,13 +373,13 @@ class Pman_Core_Notify extends Pman
                 //if (file_exists('/proc/'.$p['pid'])) {
                 $runtime = time() - $p['started'];
                 //echo "RUNTIME ({$p['pid']}): $runtime\n";
-                if ($runtime > $maxruntime) {
+                if ($runtime > $this->maxruntime) {
                     
                     proc_terminate($p['proc'], 9);
                     //fclose($p['pipes'][1]);
                     fclose($p['pipes'][0]);
                     fclose($p['pipes'][2]);
-                    $this->log("TERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']));
+                    $this->logecho("TERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']));
                     @unlink($p['out']);
                     
                     continue;
@@ -398,11 +401,11 @@ class Pman_Core_Notify extends Pman
             //    $pool[] = $p;
             //    continue;
             //}
-            $this->log("ENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) );
+            $this->logecho("ENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) );
             @unlink($p['out']);
             //unlink($p['out']);
         }
-        $this->log("POOL SIZE: ". count($pool) );
+        $this->logecho("POOL SIZE: ". count($pool) );
         $this->pool = $pool;
         if (count($pool) < $this->max_pool_size) {
             return true;
@@ -431,10 +434,11 @@ class Pman_Core_Notify extends Pman
 
     function output()
     {
-        die("Done\n");
+        $this->logecho("DONE");
+        exit;
     }
-    function log($str)
+    function logecho($str)
     {
-        echo date("Y-m-d H:i:s - $str\n");
+        echo date("Y-m-d H:i:s - ") . $str . "\n";
     }
 }
\ No newline at end of file