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.',
82 var $max_pool_size = 10;
84 var $table = 'core_notify';
85 var $target = 'Core/NotifySend';
86 var $evtype = ''; // any notification...
87 // this script should only handle EMAIL notifications..
91 $ff = HTML_FlexyFramework::get();
95 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
102 function get($r,$opts)
104 if ($opts['debug']) {
105 DB_DataObject::debugLevel($opts['debug']);
111 //date_default_timezone_set('UTC');
113 $showold = !empty($opts['old']);
114 if (!empty($opts['old'])) {
115 $opts['list'] = 1; // force listing..
118 $this->force = empty($opts['force']) ? 0 : 1;
120 if (!empty($opts['send-to'])) {
121 $this->send_to = $opts['send-to'];
125 $w = DB_DataObject::factory('core_notify_recur');
126 if (is_a($w, 'DB_DataObject')) {
127 $w->generateNotifications();
129 if (!empty($opts['generate'])) {
130 $w = DB_DataObject::factory($opts['generate']);
131 if (is_a($w, 'DB_DataObject')) {
132 $w->generateNotifications();
139 //DB_DataObject::debugLevel(1);
140 $w = DB_DataObject::factory($this->table);
146 //$w->whereAdd('act_when > sent'); // eg.. sent is not valid..
147 $w->whereAdd("sent < '1970-01-01'"); // eg.. sent is not valid..
149 $w->whereAdd('act_start > NOW() - INTERVAL 14 DAY'); // ignore the ones stuck in the queue
151 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
154 $w->orderBy('act_when ASC'); // oldest first.
155 $w->limit(1000); // we can run 1000 ...
157 $w->orderBy('act_when DESC'); // latest first
158 $w->limit($opts['limit']); // we can run 1000 ...
160 if (!empty($this->evtype)) {
161 $w->evtype = $this->evtype;
167 $ar = $w->fetchAll();
169 if (!empty($opts['list'])) {
171 die("Nothing in Queue\n");
177 echo "$w->id : $w->person_id_email email : ".
178 $o->toEventString()." ". $w->status() . "\n";
191 $p = array_shift($ar);
192 if (!$this->poolfree()) {
193 array_unshift($ar,$p); /// put it back on..
197 if ($this->poolHasDomain($p->person_id_email)) {
198 if (in_array($p->person_id_email, $pushed)) {
199 // it's been pushed to the end, and nothing has
200 // been pushed since.s
201 // give up, let the next run sort it out.
205 $ar[] = $p; // push it on the end..
207 $pushed[] = $p->person_id_email;
209 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
215 $this->run($p->id,$p->person_id_email);
223 // we should have a time limit here...
224 while(count($this->pool)) {
232 function run($id, $email, $cmdOpts="")
235 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
236 $descriptorspec = array(
237 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
238 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
239 2 => array("pipe", "w") // stderr is a file to write to
241 $php = $_SERVER["_"];
242 $sn = $_SERVER["SCRIPT_NAME"];
244 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . '/'. $sn)); // same as run on.. (so script should end up being same relatively..)
245 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
249 if (!empty($this->send_to)) {
250 $app .= ' --sent-to='.escapeshellarg($this->send_to);
252 $cmd = 'exec ' . $php . ' ' . $app . ' ' . $cmdOpts; //. ' &';
256 echo "call proc_open $cmd\n";
257 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
258 $info = proc_get_status($p);
259 $this->pool[] = array(
261 'pid' => $info['pid'],
270 echo "RUN ({$info['pid']}) $cmd \n";
277 $maxruntime = 2 * 60; // 2 minutes.. ?? should be long enoguh
279 foreach($this->pool as $p) {
281 //echo "CHECK PID: " . $p['pid'] . "\n";
282 $info = proc_get_status($p['proc']);
285 // update if necessday.
287 echo "CHANING PID FROM " . $p['pid'] . " TO ". $info['pid']. "\n";
288 $p['pid'] = $info['pid'];
291 echo @file_get_contents('/proc/'. $p['pid'] .'/cmdline') . "\n";
293 if ($info['running']) {
295 //if (file_exists('/proc/'.$p['pid'])) {
296 $runtime = time() - $p['started'];
297 //echo "RUNTIME ({$p['pid']}): $runtime\n";
298 if ($runtime > $maxruntime) {
300 proc_terminate($p['proc'], 9);
301 //fclose($p['pipes'][1]);
302 fclose($p['pipes'][0]);
303 fclose($p['pipes'][2]);
304 echo "\nTERMINATING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
313 fclose($p['pipes'][0]);
314 fclose($p['pipes'][2]);
315 //echo "CLOSING: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
316 //fclose($p['pipes'][1]);
318 proc_close($p['proc']);
322 //if (file_exists('/proc/'.$p['pid'])) {
326 echo "\nENDED: ({$p['pid']}) " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
330 echo "POOL SIZE: ". count($pool) ."\n";
332 if (count($pool) < $this->max_pool_size) {
339 * see if pool is already trying to deliver to this domain.?
340 * -- if so it get's pushed to the end of the queue.
343 function poolHasDomain($email)
345 $dom = strtolower(array_pop(explode('@',$email)));
346 foreach($this->pool as $p) {
347 $mdom = strtolower(array_pop(explode('@',$p['email'])));