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...',
54 'desc' => 'Generate notifications for a table, eg. cash_invoice',
64 var $table = 'core_notify';
65 var $target = 'Core/NotifySend';
66 var $evtype = ''; // any notification...
67 // this script should only handle EMAIL notifications..
71 $ff = HTML_FlexyFramework::get();
75 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
82 function get($r,$opts)
85 DB_DataObject::debugLevel($opts['debug']);
88 //date_default_timezone_set('UTC');
90 $showold = !empty($opts['old']);
91 if (!empty($opts['old'])) {
92 $opts['list'] = 1; // force listing..
95 $this->force = empty($opts['force']) ? 0 : 1;
97 if (!empty($opts['send-to'])) {
98 $this->send_to = $opts['send-to'];
102 $w = DB_DataObject::factory('core_notify_recur');
103 if (is_a($w, 'DB_DataObject')) {
104 $w->generateNotifications();
106 if (!empty($opts['generate'])) {
107 $w = DB_DataObject::factory($opts['generate']);
108 if (is_a($w, 'DB_DataObject')) {
109 $w->generateNotifications();
116 //DB_DataObject::debugLevel(1);
117 $w = DB_DataObject::factory($this->table);
123 $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
124 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
126 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
129 $w->orderBy('act_when ASC'); // oldest first.
130 $w->limit(1000); // we can run 1000 ...
132 $w->orderBy('act_when DESC'); // latest first
133 $w->limit(50); // we can run 1000 ...
135 if (!empty($this->evtype)) {
136 $w->evtype = $this->evtype;
142 $ar = $w->fetchAll();
144 if (!empty($opts['list'])) {
146 die("Nothing in Queue\n");
152 echo "$w->id : $w->person_id_email email : ".
153 $o->toEventString()." ". $w->status() . "\n";
166 $p = array_shift($ar);
167 if (!$this->poolfree()) {
168 array_unshift($ar,$p); /// put it back on..
172 if ($this->poolHasDomain($p->person_id_email)) {
173 if (in_array($p->person_id_email, $pushed)) {
174 // it's been pushed to the end, and nothing has
175 // been pushed since.
176 // give up, let the next run sort it out.
180 $ar[] = $p; // push it on the end..
182 $pushed[] = $p->person_id_email;
184 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
190 $this->run($p->id,$p->person_id_email);
195 // we should have a time limit here...
196 while(count($this->pool)) {
204 function run($id, $email, $cmdOpts="")
207 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
208 $descriptorspec = array(
209 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
210 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
211 2 => array("pipe", "w") // stderr is a file to write to
213 $php = $_SERVER["_"];
214 $sn = $_SERVER["SCRIPT_NAME"];
216 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
217 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
221 if (!empty($this->send_to)) {
222 $app .= ' --sent-to='.escapeshellarg($this->send_to);
224 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
228 echo "call proc_open $cmd\n";
229 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
230 $info = proc_get_status($p);
231 $this->pool[] = array(
233 'pid' => $info['pid'],
242 echo "RUN ({$info['pid']}) $cmd \n";
249 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
251 foreach($this->pool as $p) {
253 //echo "CHECK PID: " . $p['pid'] . "\n";
254 $info = proc_get_status($p['proc']);
257 // update if necessday.
259 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
260 $p['pid'] = $info['pid'];
263 echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
265 if ($info['running']) {
267 //if (file_exists('/proc/'.$p['pid'])) {
268 $runtime = time() - $p['started'];
269 //echo "RUNTIME ({$p['pid']}): $runtime\n";
270 if ($runtime > $maxruntime) {
272 proc_terminate($p['proc'], 9);
273 //fclose($p['pipes'][1]);
274 fclose($p['pipes'][0]);
275 fclose($p['pipes'][2]);
276 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
285 fclose($p['pipes'][0]);
286 fclose($p['pipes'][2]);
287 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
288 //fclose($p['pipes'][1]);
290 proc_close($p['proc']);
294 //if (file_exists('/proc/'.$p['pid'])) {
298 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
302 echo "POOL SIZE: ". count($pool) ."\n";
304 if (count($pool) < 10) {
311 * see if pool is already trying to deliver to this domain.?
312 * -- if so it get's pushed to the end of the queue.
315 function poolHasDomain($email)
317 $dom = strtolower(array_pop(explode('@',$email)));
318 foreach($this->pool as $p) {
319 $mdom = strtolower(array_pop(explode('@',$p['email'])));