NotifySend.php
[Pman.Core] / Notify.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_Notify extends Pman
17 {
18     
19     var $table = 'core_notify';
20     var $target = 'Core/NotifySend';
21     
22     function getAuth()
23     {
24         $ff = HTML_FlexyFramework::get();
25         if (!$ff->cli) {
26             die("access denied");
27         }
28         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
29         return true;
30         
31     }
32     
33     var $pool = array();
34     
35     function get()    
36     {
37         DB_DataObject::debugLevel(1);
38         //date_default_timezone_set('UTC');
39        // phpinfo();exit;
40         
41         $w = DB_DataObject::factory($this->table);
42         $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
43         $w->orderBy('act_when ASC'); // oldest first.
44         $w->limit(1000); // we can run 1000 ...
45         $ar = $w->fetchAll('id');
46         
47         while (true) {
48             if (empty($ar)) {
49                 break;
50             }
51             if (!$this->poolfree()) {
52                 sleep(3);
53                 continue;
54             }
55             $p = array_shift($ar);
56             $this->run($p);
57         }
58         while(count($this->pool)) {
59             $this->poolfree();
60             sleep(3);
61         }
62         
63         die("DONE\n");
64     }
65     
66     function run($id)
67     {
68         $descriptorspec = array(
69             0 => array("file", "/dev/null", 'r'),  // stdin is a pipe that the child will read from
70             1 => array("file", "/dev/null", 'a'),  // stdout is a pipe that the child will write to
71             2 => array("file", "/dev/null", "a") // stderr is a file to write to
72          );
73         $php = $_SERVER["_"];
74         $cwd = getcwd(); // same as run on.. (so script should end up being same relatively..)
75         $app = $cwd . '/'. $_SERVER["SCRIPT_NAME"] . '  ' . $this->target . '/'. $id;
76         $cmd = $php . ' ' . $app;
77         echo $cmd . "\n";
78         $pipe = array();
79         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
80         $this->pool[] = $p;
81     }
82     
83     function poolfree() {
84         $pool = array();
85         foreach($this->pool as $p) {
86             $ar = proc_get_Status($p);
87             //var_dump($ar);
88             if ($p['running']) {
89                 $pool[] = $p;
90             }
91         }
92         $this->pool = $pool;
93         if (count($pool) < 10) {
94             return true;
95         }
96         return false;
97         
98     }
99     
100     
101 }