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