DataObjects/core.sql
[Pman.Core] / Notify.php
index 2fa0f45..ab11fdb 100644 (file)
@@ -91,7 +91,7 @@ class Pman_Core_Notify extends Pman
             $this->send_to = $opts['send-to'];
         }
      
-        
+        //DB_DataObject::debugLevel(1);
         $w = DB_DataObject::factory($this->table);
         
         if (!$showold) {
@@ -188,7 +188,7 @@ class Pman_Core_Notify extends Pman
         $php = $_SERVER["_"];
         $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..)
+        $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
         $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . '  ' . $this->target . '/'. $id;
         if ($this->force) {
             $app .= ' -f';
@@ -196,10 +196,11 @@ class Pman_Core_Notify extends Pman
         if (!empty($this->send_to)) {
             $app .= ' --sent-to='.escapeshellarg($this->send_to);
         }
-        $cmd = $php . ' ' . $app. ' &';
+        $cmd = $php . ' ' . $app; //. ' &';
         
-        echo $cmd . "\n";
+       
         $pipe = array();
+        echo "call proc_open $cmd\n";
         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
         $info =  proc_get_status($p);
         $this->pool[] = array(
@@ -208,43 +209,66 @@ class Pman_Core_Notify extends Pman
                 'out' => $tn,
                 'cmd' => $cmd,
                 'email' => $email,
-                'pipes' => $pipes
+                'pipes' => $pipes,
+                'started' => time()
             
                 
         );
+        echo "RUN ({$info['pid']}) $cmd  \n";
     }
     
     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";
+            //echo "CHECK PID: " . $p['pid'] . "\n";
             $info =  proc_get_status($p['proc']);
             //var_dump($info);
             
+            // update if necessday.
+            if ($info['pid']) {
+                $p['pid'] = $info['pid'];
+            }
+            
             if ($info['running']) {
             
-            //if (file_exists('/proc/'.$p['pid'])) {
+                //if (file_exists('/proc/'.$p['pid'])) {
+                $runtime = time() - $p['started'];
+                //echo "RUNTIME ({$p['pid']}): $runtime\n";
+                if ($runtime > $maxruntime) {
+                    
+                    proc_terminate($p['proc'], 9);
+                    //fclose($p['pipes'][1]);
+                    fclose($p['pipes'][0]);
+                    fclose($p['pipes'][2]);
+                    echo "TERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+                    @unlink($p['out']);
+                    
+                    continue;
+                }
+                
                 $pool[] = $p;
                 continue;
             }
-            
-            echo "CLOSING: " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
-            //fclose($p['pipes'][1]);
             fclose($p['pipes'][0]);
             fclose($p['pipes'][2]);
-            proc_close($p['proc']);
+            //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+            //fclose($p['pipes'][1]);
             
+            proc_close($p['proc']);
             
-            clearstatcache();
-            if (file_exists('/proc/'.$p['pid'])) {
-                $pool[] = $p;
-                continue;
-            }
-            echo "ENDED: " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
             
+            //clearstatcache();
+            //if (file_exists('/proc/'.$p['pid'])) {
+            //    $pool[] = $p;
+            //    continue;
+            //}
+            echo "ENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+            @unlink($p['out']);
             //unlink($p['out']);
         }
         echo "POOL SIZE: ". count($pool) ."\n";