2 require_once 'Pman.php';
5 * notification script runner
7 * This does not actualy send stuf out, it only starts the NotifySend/{id}
8 * which does the actuall notifcations.
10 * It manages a pool of notifiers.
16 class Pman_Core_Notify extends Pman
19 static $cli_desc = "Send out notification emails (usually from cron)";
21 static $cli_opts = array(
23 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
31 'desc' => 'List message to send, do not send them..',
39 'desc' => 'Show old messages..',
47 'desc' => 'Force redelivery, even if it has been sent before or not queued...',
57 var $table = 'core_notify';
58 var $target = 'Core/NotifySend';
59 var $evtype = ''; // any notification...
60 // this script should only handle EMAIL notifications..
64 $ff = HTML_FlexyFramework::get();
68 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
75 function get($r,$opts)
78 DB_DataObject::debugLevel($opts['debug']);
81 //date_default_timezone_set('UTC');
83 $showold = !empty($opts['old']);
84 if (!empty($opts['old'])) {
85 $opts['list'] = 1; // force listing..
88 $this->force = empty($opts['force']) ? 0 : 1;
90 if (!empty($opts['send-to'])) {
91 $this->send_to = $opts['send-to'];
95 $w = DB_DataObject::factory('core_notify_recur');
96 if (is_a($w, 'DB_DataObject')) {
97 $w->generateNotifications();
101 //DB_DataObject::debugLevel(1);
102 $w = DB_DataObject::factory($this->table);
105 $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
108 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
111 $w->orderBy('act_when ASC'); // oldest first.
112 $w->limit(1000); // we can run 1000 ...
114 $w->orderBy('act_when DESC'); // latest first
115 $w->limit(50); // we can run 1000 ...
117 if (!empty($this->evtype)) {
118 $w->evtype = $this->evtype;
124 $ar = $w->fetchAll();
126 if (!empty($opts['list'])) {
128 die("Nothing in Queue\n");
134 echo "$w->id : $w->person_id_email email : ".
135 $o->toEventString()." ". $w->status() . "\n";
148 $p = array_shift($ar);
149 if (!$this->poolfree()) {
150 array_unshift($ar,$p); /// put it back on..
154 if ($this->poolHasDomain($p->person_id_email)) {
155 if (in_array($p->person_id_email, $pushed)) {
156 // it's been pushed to the end, and nothing has
157 // been pushed since.
158 // give up, let the next run sort it out.
162 $ar[] = $p; // push it on the end..
164 $pushed[] = $p->person_id_email;
166 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
172 $this->run($p->id,$p->person_id_email);
177 // we should have a time limit here...
178 while(count($this->pool)) {
186 function run($id, $email, $cmdOpts="")
189 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
190 $descriptorspec = array(
191 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
192 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
193 2 => array("pipe", "w") // stderr is a file to write to
195 $php = $_SERVER["_"];
196 $sn = $_SERVER["SCRIPT_NAME"];
198 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
199 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
203 if (!empty($this->send_to)) {
204 $app .= ' --sent-to='.escapeshellarg($this->send_to);
206 $cmd = $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
210 echo "call proc_open $cmd\n";
211 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
212 $info = proc_get_status($p);
213 $this->pool[] = array(
215 'pid' => $info['pid'],
224 echo "RUN ({$info['pid']}) $cmd \n";
231 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
233 foreach($this->pool as $p) {
235 //echo "CHECK PID: " . $p['pid'] . "\n";
236 $info = proc_get_status($p['proc']);
239 // update if necessday.
241 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
242 $p['pid'] = $info['pid'];
245 echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
247 if ($info['running']) {
249 //if (file_exists('/proc/'.$p['pid'])) {
250 $runtime = time() - $p['started'];
251 //echo "RUNTIME ({$p['pid']}): $runtime\n";
252 if ($runtime > $maxruntime) {
254 proc_terminate($p['proc'], 9);
255 //fclose($p['pipes'][1]);
256 fclose($p['pipes'][0]);
257 fclose($p['pipes'][2]);
258 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
267 fclose($p['pipes'][0]);
268 fclose($p['pipes'][2]);
269 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
270 //fclose($p['pipes'][1]);
272 proc_close($p['proc']);
276 //if (file_exists('/proc/'.$p['pid'])) {
280 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
284 echo "POOL SIZE: ". count($pool) ."\n";
286 if (count($pool) < 10) {
293 * see if pool is already trying to deliver to this domain.?
294 * -- if so it get's pushed to the end of the queue.
297 function poolHasDomain($email)
299 $dom = strtolower(array_pop(explode('@',$email)));
300 foreach($this->pool as $p) {
301 $mdom = strtolower(array_pop(explode('@',$p['email'])));