NotifySend.php
[Pman.Core] / NotifySend.php
1 <?php
2 require_once 'Pman.php';
3
4 /**
5  * notification script runner
6  *
7  * This does not actualy send stuf out, it only starts the NotifySend/{id}
8  * which does the actuall notifcations.
9  *
10  * It manages a pool of notifiers.
11  * 
12  * 
13  */
14
15
16 class Pman_Core_NotifySend extends Pman
17 {
18     
19     var $table = 'core_notify'
20     function getAuth()
21     {
22         $ff = HTML_FlexyFramework::get();
23         if (!$ff->cli) {
24             die("access denied");
25         }
26         //HTML_FlexyFramework::ensureSingle(__FILE__, $this);
27         return true;
28         
29     }
30     
31     var $pool = array();
32     
33     function get($id)    
34     {
35         DB_DataObject::debugLevel(1);
36         //date_default_timezone_set('UTC');
37         // phpinfo();exit;
38         
39         $w = DB_DataObject::factory($this->table);
40         
41         if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
42             die("invalid id or time");
43         }
44          
45         die("DONE\n");
46     }
47     
48     function run($id)
49     {
50         $descriptorspec = array(
51             0 => array("file", "/dev/null", 'r'),  // stdin is a pipe that the child will read from
52             1 => array("file", "/dev/null", 'a'),  // stdout is a pipe that the child will write to
53             2 => array("file", "/dev/null", "a") // stderr is a file to write to
54          );
55         $php = $_SERVER["_"];
56         $cwd = getcwd(); // same as run on.. (so script should end up being same relatively..)
57         $app = $cwd . '/'. $_SERVER["SCRIPT_NAME"] . '  ' . $this->target . '/'. $id;
58         $cmd = $php . ' ' . $app;
59         echo $cmd . "\n";
60         $pipe = array();
61         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
62         $this->pool[] = $p;
63     }
64     
65     function poolfree() {
66         $pool = array();
67         foreach($this->pool as $p) {
68             $ar = proc_get_Status($p);
69             //var_dump($ar);
70             if ($p['running']) {
71                 $pool[] = $p;
72             }
73         }
74         $this->pool = $pool;
75         if (count($pool) < 10) {
76             return true;
77         }
78         return false;
79         
80     }
81     
82     
83 }