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         if (!$w->get($id) || strtotime($w->act_when) < strtotime($w->sent)) {
41             die("invalid id or time");
42         }
43         $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
44         $w->orderBy('act_when ASC'); // oldest first.
45         $w->limit(1000); // we can run 1000 ...
46         $ar = $w->fetchAll('id');
47         
48         while (true) {
49             if (empty($ar)) {
50                 break;
51             }
52             if (!$this->poolfree()) {
53                 sleep(3);
54                 continue;
55             }
56             $p = array_shift($ar);
57             $this->run($p);
58         }
59         while(count($this->pool)) {
60             $this->poolfree();
61             sleep(3);
62         }
63         
64         die("DONE\n");
65     }
66     
67     function run($id)
68     {
69         $descriptorspec = array(
70             0 => array("file", "/dev/null", 'r'),  // stdin is a pipe that the child will read from
71             1 => array("file", "/dev/null", 'a'),  // stdout is a pipe that the child will write to
72             2 => array("file", "/dev/null", "a") // stderr is a file to write to
73          );
74         $php = $_SERVER["_"];
75         $cwd = getcwd(); // same as run on.. (so script should end up being same relatively..)
76         $app = $cwd . '/'. $_SERVER["SCRIPT_NAME"] . '  ' . $this->target . '/'. $id;
77         $cmd = $php . ' ' . $app;
78         echo $cmd . "\n";
79         $pipe = array();
80         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
81         $this->pool[] = $p;
82     }
83     
84     function poolfree() {
85         $pool = array();
86         foreach($this->pool as $p) {
87             $ar = proc_get_Status($p);
88             //var_dump($ar);
89             if ($p['running']) {
90                 $pool[] = $p;
91             }
92         }
93         $this->pool = $pool;
94         if (count($pool) < 10) {
95             return true;
96         }
97         return false;
98         
99     }
100     
101     
102 }