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',
70 var $table = 'core_notify';
71 var $target = 'Core/NotifySend';
72 var $evtype = ''; // any notification...
73 // this script should only handle EMAIL notifications..
77 $ff = HTML_FlexyFramework::get();
81 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
88 function get($r,$opts)
91 DB_DataObject::debugLevel($opts['debug']);
94 //date_default_timezone_set('UTC');
96 $showold = !empty($opts['old']);
97 if (!empty($opts['old'])) {
98 $opts['list'] = 1; // force listing..
101 $this->force = empty($opts['force']) ? 0 : 1;
103 if (!empty($opts['send-to'])) {
104 $this->send_to = $opts['send-to'];
108 $w = DB_DataObject::factory('core_notify_recur');
109 if (is_a($w, 'DB_DataObject')) {
110 $w->generateNotifications();
112 if (!empty($opts['generate'])) {
113 $w = DB_DataObject::factory($opts['generate']);
114 if (is_a($w, 'DB_DataObject')) {
115 $w->generateNotifications();
122 //DB_DataObject::debugLevel(1);
123 $w = DB_DataObject::factory($this->table);
129 //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
130 $w->whereAdd("sent < '1970-01-01'"); // eg.. sent is not valid..
132 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
134 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
137 $w->orderBy('act_when ASC'); // oldest first.
138 $w->limit(1000); // we can run 1000 ...
140 $w->orderBy('act_when DESC'); // latest first
141 $w->limit(50); // we can run 1000 ...
143 if (!empty($this->evtype)) {
144 $w->evtype = $this->evtype;
150 $ar = $w->fetchAll();
152 if (!empty($opts['list'])) {
154 die("Nothing in Queue\n");
160 echo "$w->id : $w->person_id_email email : ".
161 $o->toEventString()." ". $w->status() . "\n";
174 $p = array_shift($ar);
175 if (!$this->poolfree()) {
176 array_unshift($ar,$p); /// put it back on..
180 if ($this->poolHasDomain($p->person_id_email)) {
181 if (in_array($p->person_id_email, $pushed)) {
182 // it's been pushed to the end, and nothing has
183 // been pushed since.
184 // give up, let the next run sort it out.
188 $ar[] = $p; // push it on the end..
190 $pushed[] = $p->person_id_email;
192 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
198 $this->run($p->id,$p->person_id_email);
203 // we should have a time limit here...
204 while(count($this->pool)) {
212 function run($id, $email, $cmdOpts="")
215 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
216 $descriptorspec = array(
217 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
218 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
219 2 => array("pipe", "w") // stderr is a file to write to
221 $php = $_SERVER["_"];
222 $sn = $_SERVER["SCRIPT_NAME"];
224 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
225 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
229 if (!empty($this->send_to)) {
230 $app .= ' --sent-to='.escapeshellarg($this->send_to);
232 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
236 echo "call proc_open $cmd\n";
237 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
238 $info = proc_get_status($p);
239 $this->pool[] = array(
241 'pid' => $info['pid'],
250 echo "RUN ({$info['pid']}) $cmd \n";
257 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
259 foreach($this->pool as $p) {
261 //echo "CHECK PID: " . $p['pid'] . "\n";
262 $info = proc_get_status($p['proc']);
265 // update if necessday.
267 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
268 $p['pid'] = $info['pid'];
271 echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
273 if ($info['running']) {
275 //if (file_exists('/proc/'.$p['pid'])) {
276 $runtime = time() - $p['started'];
277 //echo "RUNTIME ({$p['pid']}): $runtime\n";
278 if ($runtime > $maxruntime) {
280 proc_terminate($p['proc'], 9);
281 //fclose($p['pipes'][1]);
282 fclose($p['pipes'][0]);
283 fclose($p['pipes'][2]);
284 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
293 fclose($p['pipes'][0]);
294 fclose($p['pipes'][2]);
295 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
296 //fclose($p['pipes'][1]);
298 proc_close($p['proc']);
302 //if (file_exists('/proc/'.$p['pid'])) {
306 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
310 echo "POOL SIZE: ". count($pool) ."\n";
312 if (count($pool) < 10) {
319 * see if pool is already trying to deliver to this domain.?
320 * -- if so it get's pushed to the end of the queue.
323 function poolHasDomain($email)
325 $dom = strtolower(array_pop(explode('@',$email)));
326 foreach($this->pool as $p) {
327 $mdom = strtolower(array_pop(explode('@',$p['email'])));