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 $nice_level Unix 'nice' level to stop it jamming server up.
91 var $nice_level = false;
93 * @var $max_pool_size maximum runners to start at once.
95 var $max_pool_size = 10;
97 * @var $max_to_domain maximum connections to make to a single domain
99 var $max_to_domain = 10;
101 var $table = 'core_notify';
102 var $target = 'Core/NotifySend';
103 var $evtype = ''; // any notification...
104 // this script should only handle EMAIL notifications..
108 $ff = HTML_FlexyFramework::get();
110 die("access denied");
112 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
119 function get($r,$opts)
121 if ($opts['debug']) {
122 DB_DataObject::debugLevel($opts['debug']);
126 if (!empty($opts['poolsize'])) {
127 $this->max_pool_size = $opts['poolsize'];
130 if (empty($opts['limit'])) {
131 $opts['limit'] = '1000'; // not sure why it's not picking up the defautl..
133 //date_default_timezone_set('UTC');
135 $showold = !empty($opts['old']);
136 if (!empty($opts['old'])) {
137 $opts['list'] = 1; // force listing..
140 $this->force = empty($opts['force']) ? 0 : 1;
142 if (!empty($opts['send-to'])) {
143 $this->send_to = $opts['send-to'];
147 $w = DB_DataObject::factory('core_notify_recur');
148 if (is_a($w, 'DB_DataObject')) {
149 $w->generateNotifications();
151 if (!empty($opts['generate'])) {
152 $w = DB_DataObject::factory($opts['generate']);
153 if (is_a($w, 'DB_DataObject')) {
154 $w->generateNotifications();
161 //DB_DataObject::debugLevel(1);
162 $w = DB_DataObject::factory($this->table);
168 //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
169 $w->whereAdd("sent < '1970-01-01' OR sent IS NULL"); // eg.. sent is not valid..
171 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
173 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
176 $w->orderBy('act_when ASC'); // oldest first.
178 echo "QUEUE is {$w->count()}\n";
180 $w->limit($opts['limit']); // we can run 1000 ...
182 $w->orderBy('act_when DESC'); // latest first
183 $w->limit($opts['limit']); // we can run 1000 ...
185 if (!empty($this->evtype)) {
186 $w->evtype = $this->evtype;
192 $ar = $w->fetchAll();
194 if (!empty($opts['list'])) {
196 die("Nothing in Queue\n");
202 echo "$w->id : $w->person_id_email email : ".
203 $o->toEventString()." ". $w->status() . "\n";
208 //echo "BATCH SIZE: ". count($ar) . "\n";
214 echo "BATCH SIZE: ". count($ar) . "\n";
217 echo "COMPLETED MAIN QUEUE - running delated\n";
219 if (empty($pushed)) {
228 $p = array_shift($ar);
229 if (!$this->poolfree()) {
230 array_unshift($ar,$p); /// put it back on..
234 if ($this->poolHasDomain($p->person_id_email) > $this->max_to_domain) {
236 if ($pushed === false) {
237 // we only try once to requeue..
249 $this->run($p->id,$p->person_id_email);
255 // we should have a time limit here...
256 while(count($this->pool)) {
261 foreach($requeue as $p) {
263 $p->act_when = $p->sqlValue('NOW + INTERVAL 1 MINUTE');
273 function run($id, $email, $cmdOpts="")
276 static $renice = false;
278 require_once 'System.php';
279 $renice = System::which('renice');
283 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
284 $descriptorspec = array(
285 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
286 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
287 2 => array("pipe", "w") // stderr is a file to write to
289 $php = $_SERVER["_"];
290 $sn = $_SERVER["SCRIPT_NAME"];
292 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
293 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
297 if (!empty($this->send_to)) {
298 $app .= ' --sent-to='.escapeshellarg($this->send_to);
300 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
304 echo "call proc_open $cmd\n";
307 if ($this->max_pool_size === 1) {
313 if (!empty($this->opts['dryrun'])) {
318 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
319 $info = proc_get_status($p);
321 if ($this->nice_level !== false) {
322 $rcmd = "$renice {$this->nice_level} {$info['pid']}";
325 $this->pool[] = array(
327 'pid' => $info['pid'],
336 echo "RUN ({$info['pid']}) $cmd \n";
343 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
345 foreach($this->pool as $p) {
347 //echo "CHECK PID: " . $p['pid'] . "\n";
348 $info = proc_get_status($p['proc']);
351 // update if necessday.
352 if ($info['pid'] && $p['pid'] != $info['pid']) {
353 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
354 $p['pid'] = $info['pid'];
357 //echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
359 if ($info['running']) {
361 //if (file_exists('/proc/'.$p['pid'])) {
362 $runtime = time() - $p['started'];
363 //echo "RUNTIME ({$p['pid']}): $runtime\n";
364 if ($runtime > $maxruntime) {
366 proc_terminate($p['proc'], 9);
367 //fclose($p['pipes'][1]);
368 fclose($p['pipes'][0]);
369 fclose($p['pipes'][2]);
370 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
379 fclose($p['pipes'][0]);
380 fclose($p['pipes'][2]);
381 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
382 //fclose($p['pipes'][1]);
384 proc_close($p['proc']);
388 //if (file_exists('/proc/'.$p['pid'])) {
392 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
396 echo "POOL SIZE: ". count($pool) ."\n";
398 if (count($pool) < $this->max_pool_size) {
405 * see if pool is already trying to deliver to this domain.?
406 * -- if so it get's pushed to the end of the queue.
409 function poolHasDomain($email)
412 $dom = strtolower(array_pop(explode('@',$email)));
413 foreach($this->pool as $p) {
414 $mdom = strtolower(array_pop(explode('@',$p['email'])));