Merge branch 'master' of http://git.roojs.com:8081/Pman.Core
[Pman.Core] / Notify.php
index d3d4678..15bbc7a 100644 (file)
@@ -16,7 +16,13 @@ require_once 'Pman.php';
 class Pman_Core_Notify extends Pman
 {
     
-    static $cli_desc = "Send out notification emails (usually from cron)";
+    static $cli_desc = "Runs the notification queue (usually from cron)
+                        Normally used to sends out emails to anyone in the notification list.
+    
+                        /etc/cron.d/pman-core-notify
+                        * *  * * *     www-data     /usr/bin/php /home/gitlive/web.mtrack/admin.php  Core/Notify > /dev/null
+    
+";
     
     static $cli_opts = array(
         'debug' => array(
@@ -43,13 +49,20 @@ class Pman_Core_Notify extends Pman
             'max' => 0,
             
         ),
-         'force' => array(
+        'force' => array(
             'desc' => 'Force redelivery, even if it has been sent before or not queued...',
             'default' => 0,
             'short' => 'f',
             'min' => 0,
             'max' => 0,
         ),
+        'generate' => array(
+            'desc' => 'Generate notifications for a table, eg. cash_invoice',
+            'default' => '',
+            'short' => 'g',
+            'min' => 0,
+            'max' => 1,
+        ),
     );
     
     
@@ -58,7 +71,7 @@ class Pman_Core_Notify extends Pman
     var $target = 'Core/NotifySend';
     var $evtype = ''; // any notification...
                     // this script should only handle EMAIL notifications..
-    
+    var $force = false;
     function getAuth()
     {
         $ff = HTML_FlexyFramework::get();
@@ -92,11 +105,29 @@ class Pman_Core_Notify extends Pman
         }
      
         
+        $w = DB_DataObject::factory('core_notify_recur');
+        if (is_a($w, 'DB_DataObject')) {
+            $w->generateNotifications();
+        }
+        if (!empty($opts['generate'])) {
+            $w = DB_DataObject::factory($opts['generate']);
+            if (is_a($w, 'DB_DataObject')) {
+                $w->generateNotifications();
+            }
+            exit;
+            
+            
+        }
+     
+        //DB_DataObject::debugLevel(1);
         $w = DB_DataObject::factory($this->table);
         
         if (!$showold) {
-            $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
             
+            // standard
+            
+            $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
+            $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
             if (!$this->force) {
                 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
             }
@@ -176,7 +207,7 @@ class Pman_Core_Notify extends Pman
         die("DONE\n");
     }
     
-    function run($id, $email)
+    function run($id, $email, $cmdOpts="")
     {
        // phpinfo();exit;
         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
@@ -188,7 +219,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 +227,11 @@ class Pman_Core_Notify extends Pman
         if (!empty($this->send_to)) {
             $app .= ' --sent-to='.escapeshellarg($this->send_to);
         }
-        $cmd = $php . ' ' . $app. ' &';
+        $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
         
-        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(
@@ -207,27 +239,70 @@ class Pman_Core_Notify extends Pman
                 'pid' => $info['pid'],
                 'out' => $tn,
                 'cmd' => $cmd,
-                'email' => $email
+                'email' => $email,
+                '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']) {
+                echo "CHANING PID FROM " . $p['pid']  .  "  TO ". $info['pid']. "\n";
+                $p['pid'] = $info['pid'];
+            }
+            
+            echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
             
             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 "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+                    @unlink($p['out']);
+                    
+                    continue;
+                }
+                
                 $pool[] = $p;
                 continue;
             }
-            echo "ENDED: " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+            fclose($p['pipes'][0]);
+            fclose($p['pipes'][2]);
+            //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 "\nENDED: ({$p['pid']}) " .  $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
+            @unlink($p['out']);
             //unlink($p['out']);
         }
         echo "POOL SIZE: ". count($pool) ."\n";
@@ -256,4 +331,8 @@ class Pman_Core_Notify extends Pman
         
     }
 
+    function output()
+    {
+        die("Done\n");
+    }
 }
\ No newline at end of file