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 = "Runs the notification queue (usually from cron)
20 Normally used to sends out emails to anyone in the notification list.
22 /etc/cron.d/pman-core-notify
23 * * * * * www-data /usr/bin/php /home/gitlive/web.mtrack/admin.php Core/Notify > /dev/null
27 static $cli_opts = array(
29 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
37 'desc' => 'List message to send, do not send them..',
45 'desc' => 'Show old messages..',
53 'desc' => 'Force redelivery, even if it has been sent before or not queued...',
60 'desc' => 'Generate notifications for a table, eg. cash_invoice',
67 'desc' => 'Limit search for no. to send to ',
74 'desc' => 'Dry run - do not send.',
81 'desc' => 'Pool size',
89 var $max_pool_size = 10;
91 var $table = 'core_notify';
92 var $target = 'Core/NotifySend';
93 var $evtype = ''; // any notification...
94 // this script should only handle EMAIL notifications..
98 $ff = HTML_FlexyFramework::get();
100 die("access denied");
102 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
109 function get($r,$opts)
111 if ($opts['debug']) {
112 DB_DataObject::debugLevel($opts['debug']);
116 if (!empty($opts['poolsize'])) {
117 $this->max_pool_size = $opts['poolsize'];
120 if (empty($opts['limit'])) {
121 $opts['limit'] = '1000'; // not sure why it's not picking up the defautl..
123 //date_default_timezone_set('UTC');
125 $showold = !empty($opts['old']);
126 if (!empty($opts['old'])) {
127 $opts['list'] = 1; // force listing..
130 $this->force = empty($opts['force']) ? 0 : 1;
132 if (!empty($opts['send-to'])) {
133 $this->send_to = $opts['send-to'];
137 $w = DB_DataObject::factory('core_notify_recur');
138 if (is_a($w, 'DB_DataObject')) {
139 $w->generateNotifications();
141 if (!empty($opts['generate'])) {
142 $w = DB_DataObject::factory($opts['generate']);
143 if (is_a($w, 'DB_DataObject')) {
144 $w->generateNotifications();
151 //DB_DataObject::debugLevel(1);
152 $w = DB_DataObject::factory($this->table);
158 //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
159 $w->whereAdd("sent < '1970-01-01'"); // eg.. sent is not valid..
161 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
163 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
166 $w->orderBy('act_when ASC'); // oldest first.
168 echo "QUEUE is {$w->count()}\n";
170 $w->limit($opts['limit']); // we can run 1000 ...
172 $w->orderBy('act_when DESC'); // latest first
173 $w->limit($opts['limit']); // we can run 1000 ...
175 if (!empty($this->evtype)) {
176 $w->evtype = $this->evtype;
182 $ar = $w->fetchAll();
184 if (!empty($opts['list'])) {
186 die("Nothing in Queue\n");
192 echo "$w->id : $w->person_id_email email : ".
193 $o->toEventString()." ". $w->status() . "\n";
198 //echo "BATCH SIZE: ". count($ar) . "\n";
203 echo "BATCH SIZE: ". count($ar) . "\n";
206 echo "COMPLETED MAIN QUEUE - running delated\n";
208 if (empty($pushed)) {
217 $p = array_shift($ar);
218 if (!$this->poolfree()) {
219 array_unshift($ar,$p); /// put it back on..
223 if ($this->poolHasDomain($p->person_id_email)) {
233 $this->run($p->id,$p->person_id_email);
239 // we should have a time limit here...
240 while(count($this->pool)) {
248 function run($id, $email, $cmdOpts="")
251 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
252 $descriptorspec = array(
253 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
254 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
255 2 => array("pipe", "w") // stderr is a file to write to
257 $php = $_SERVER["_"];
258 $sn = $_SERVER["SCRIPT_NAME"];
260 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
261 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
265 if (!empty($this->send_to)) {
266 $app .= ' --sent-to='.escapeshellarg($this->send_to);
268 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
272 echo "call proc_open $cmd\n";
275 if (!empty($this->opts['dryrun'])) {
280 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
281 $info = proc_get_status($p);
282 $this->pool[] = array(
284 'pid' => $info['pid'],
293 echo "RUN ({$info['pid']}) $cmd \n";
300 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
302 foreach($this->pool as $p) {
304 //echo "CHECK PID: " . $p['pid'] . "\n";
305 $info = proc_get_status($p['proc']);
308 // update if necessday.
309 if ($info['pid'] && $p['pid'] != $info['pid']) {
310 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
311 $p['pid'] = $info['pid'];
314 //echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
316 if ($info['running']) {
318 //if (file_exists('/proc/'.$p['pid'])) {
319 $runtime = time() - $p['started'];
320 //echo "RUNTIME ({$p['pid']}): $runtime\n";
321 if ($runtime > $maxruntime) {
323 proc_terminate($p['proc'], 9);
324 //fclose($p['pipes'][1]);
325 fclose($p['pipes'][0]);
326 fclose($p['pipes'][2]);
327 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
336 fclose($p['pipes'][0]);
337 fclose($p['pipes'][2]);
338 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
339 //fclose($p['pipes'][1]);
341 proc_close($p['proc']);
345 //if (file_exists('/proc/'.$p['pid'])) {
349 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
353 echo "POOL SIZE: ". count($pool) ."\n";
355 if (count($pool) < $this->max_pool_size) {
362 * see if pool is already trying to deliver to this domain.?
363 * -- if so it get's pushed to the end of the queue.
366 function poolHasDomain($email)
368 $dom = strtolower(array_pop(explode('@',$email)));
369 foreach($this->pool as $p) {
370 $mdom = strtolower(array_pop(explode('@',$p['email'])));