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     static $cli_desc = "Send out notification emails (usually from cron)";
20     
21     static $cli_opts = array(
22         'debug' => array(
23             'desc' => 'Turn on debugging (see DataObjects debugLevel )',
24             'default' => 0,
25             'short' => 'v',
26             'min' => 1,
27             'max' => 1,
28             
29         ),
30         'list' => array(
31             'desc' => 'List message to send, do not send them..',
32             'default' => 0,
33             'short' => 'l',
34             'min' => 0,
35             'max' => 1,
36             
37         )
38     );
39     
40     
41     
42     var $table = 'core_notify';
43     var $target = 'Core/NotifySend';
44     var $evtype = ''; // any notification...
45                     // this script should only handle EMAIL notifications..
46     
47     function getAuth()
48     {
49         $ff = HTML_FlexyFramework::get();
50         if (!$ff->cli) {
51             die("access denied");
52         }
53         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
54         return true;
55         
56     }
57     
58     var $pool = array();
59     
60     function get($r,$opts)    
61     {
62         if ($opts['debug']) {
63             DB_DataObject::debugLevel($opts['debug']);
64         }
65         //date_default_timezone_set('UTC');
66        // phpinfo();exit;
67         
68         
69         
70         $w = DB_DataObject::factory($this->table);
71         $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
72         $w->whereAdd('act_when < NOW()'); // eg.. not if future..
73
74         $w->orderBy('act_when ASC'); // oldest first.
75         if (!empty($this->evtype)) {
76             $w->evtype = $this->evtype;
77         }
78         
79         $w->autoJoin();
80         $w->limit(1000); // we can run 1000 ...
81         
82         $ar = $w->fetchAll();
83         
84         if (!empty($opts['list'])) {
85             foreach($ar as $w) {
86                 $o = $w->object();
87                 echo "$e->id : $w->person_id_email email    : ". $o->toEventString()."\n";
88             }
89             exit;
90             
91             
92         }
93         
94         
95         
96         while (true) {
97             if (empty($ar)) {
98                 break;
99             }
100             
101             $p = array_shift($ar);
102             if (!$this->poolfree()) {
103                 array_unshift($ar,$p); /// put it back on..
104                 sleep(3);
105                 continue;
106             }
107             if ($this->poolHasDomain($p->person_id_email)) {
108                 $ar[] = $p; // push it on the end..
109                 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
110                 sleep(3);
111                 continue;
112             }
113             
114             
115             $this->run($p->id,$p->person_id_email);
116         }
117         while(count($this->pool)) {
118             $this->poolfree();
119             sleep(3);
120         }
121         
122         die("DONE\n");
123     }
124     
125     function run($id, $email)
126     {
127        // phpinfo();exit;
128         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
129         $descriptorspec = array(
130             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
131             1 => array("file", $tn, 'w'),  // stdout is a pipe that the child will write to
132             2 => array("pipe", "w") // stderr is a file to write to
133          );
134         $php = $_SERVER["_"];
135         $sn =  $_SERVER["SCRIPT_NAME"];
136         
137         $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
138         $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . '  ' . $this->target . '/'. $id;
139         $cmd = $php . ' ' . $app. ' &';
140         echo $cmd . "\n";
141         $pipe = array();
142         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
143         $this->pool[] = array(
144                 'proc' => $p,
145                 'out' => $tn,
146                 'cmd' => $cmd,
147                 'email' => $email
148         );
149     }
150     
151     function poolfree()
152     {
153         $pool = array();
154         foreach($this->pool as $p) {
155             $ar = proc_get_status($p['proc']);
156            // print_r($p);
157             //print_r($ar);
158             if ($ar['running']) {
159                 $pool[] = $p;
160                 continue;
161             }
162             echo $p['cmd'] . " : " . file_get_contents($p['out']);
163             //unlink($p['out']);
164         }
165         $this->pool = $pool;
166         if (count($pool) < 10) {
167             return true;
168         }
169         return false;
170         
171     }
172     /**
173      * see if pool is already trying to deliver to this domain.?
174      * -- if so it get's pushed to the end of the queue.
175      *
176      */
177     function poolHasDomain($email)
178     {
179         $dom = strtolower(array_pop(explode('@',$email)));
180         foreach($this->pool as $p) {
181             $mdom = strtolower(array_pop(explode('@',$p['email'])));
182             if ($mdom == $dom) {
183                 return true;
184             }
185         }
186         return false;
187         
188     }
189
190 }