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',
90 var $max_pool_size = 10;
92 var $table = 'core_notify';
93 var $target = 'Core/NotifySend';
94 var $evtype = ''; // any notification...
95 // this script should only handle EMAIL notifications..
99 $ff = HTML_FlexyFramework::get();
101 die("access denied");
103 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
110 function get($r,$opts)
112 if ($opts['debug']) {
113 DB_DataObject::debugLevel($opts['debug']);
117 if (!empty($opts['poolsize'])) {
118 $this->max_pool_size = $opts['poolsize'];
121 if (empty($opts['limit'])) {
122 $opts['limit'] = '1000'; // not sure why it's not picking up the defautl..
124 //date_default_timezone_set('UTC');
126 $showold = !empty($opts['old']);
127 if (!empty($opts['old'])) {
128 $opts['list'] = 1; // force listing..
131 $this->force = empty($opts['force']) ? 0 : 1;
133 if (!empty($opts['send-to'])) {
134 $this->send_to = $opts['send-to'];
138 $w = DB_DataObject::factory('core_notify_recur');
139 if (is_a($w, 'DB_DataObject')) {
140 $w->generateNotifications();
142 if (!empty($opts['generate'])) {
143 $w = DB_DataObject::factory($opts['generate']);
144 if (is_a($w, 'DB_DataObject')) {
145 $w->generateNotifications();
152 //DB_DataObject::debugLevel(1);
153 $w = DB_DataObject::factory($this->table);
159 //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
160 $w->whereAdd("sent < '1970-01-01'"); // eg.. sent is not valid..
162 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
164 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
167 $w->orderBy('act_when ASC'); // oldest first.
169 echo "QUEUE is {$w->count()}\n";
171 $w->limit($opts['limit']); // we can run 1000 ...
173 $w->orderBy('act_when DESC'); // latest first
174 $w->limit($opts['limit']); // we can run 1000 ...
176 if (!empty($this->evtype)) {
177 $w->evtype = $this->evtype;
183 $ar = $w->fetchAll();
185 if (!empty($opts['list'])) {
187 die("Nothing in Queue\n");
193 echo "$w->id : $w->person_id_email email : ".
194 $o->toEventString()." ". $w->status() . "\n";
199 //echo "BATCH SIZE: ". count($ar) . "\n";
205 echo "BATCH SIZE: ". count($ar) . "\n";
208 echo "COMPLETED MAIN QUEUE - running delated\n";
210 if (empty($pushed)) {
219 $p = array_shift($ar);
220 if (!$this->poolfree()) {
221 array_unshift($ar,$p); /// put it back on..
225 if ($this->poolHasDomain($p->person_id_email)) {
227 if ($pushed === false) {
228 // we only try once to requeue..
240 $this->run($p->id,$p->person_id_email);
246 // we should have a time limit here...
247 while(count($this->pool)) {
253 foreach($requeue as $p) {
255 $p->event_when = strtotime($p->event_when . ' + 1 MINUTE');
265 function run($id, $email, $cmdOpts="")
271 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
272 $descriptorspec = array(
273 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
274 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
275 2 => array("pipe", "w") // stderr is a file to write to
277 $php = $_SERVER["_"];
278 $sn = $_SERVER["SCRIPT_NAME"];
280 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
281 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
285 if (!empty($this->send_to)) {
286 $app .= ' --sent-to='.escapeshellarg($this->send_to);
288 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
292 echo "call proc_open $cmd\n";
295 if ($this->max_pool_size === 1) {
301 if (!empty($this->opts['dryrun'])) {
306 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
307 $info = proc_get_status($p);
308 $this->pool[] = array(
310 'pid' => $info['pid'],
319 echo "RUN ({$info['pid']}) $cmd \n";
326 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
328 foreach($this->pool as $p) {
330 //echo "CHECK PID: " . $p['pid'] . "\n";
331 $info = proc_get_status($p['proc']);
334 // update if necessday.
335 if ($info['pid'] && $p['pid'] != $info['pid']) {
336 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
337 $p['pid'] = $info['pid'];
340 //echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
342 if ($info['running']) {
344 //if (file_exists('/proc/'.$p['pid'])) {
345 $runtime = time() - $p['started'];
346 //echo "RUNTIME ({$p['pid']}): $runtime\n";
347 if ($runtime > $maxruntime) {
349 proc_terminate($p['proc'], 9);
350 //fclose($p['pipes'][1]);
351 fclose($p['pipes'][0]);
352 fclose($p['pipes'][2]);
353 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
362 fclose($p['pipes'][0]);
363 fclose($p['pipes'][2]);
364 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
365 //fclose($p['pipes'][1]);
367 proc_close($p['proc']);
371 //if (file_exists('/proc/'.$p['pid'])) {
375 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
379 echo "POOL SIZE: ". count($pool) ."\n";
381 if (count($pool) < $this->max_pool_size) {
388 * see if pool is already trying to deliver to this domain.?
389 * -- if so it get's pushed to the end of the queue.
392 function poolHasDomain($email)
394 $dom = strtolower(array_pop(explode('@',$email)));
395 foreach($this->pool as $p) {
396 $mdom = strtolower(array_pop(explode('@',$p['email'])));