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 = "Send out notification emails (usually from cron)";
21 static $cli_opts = array(
23 'desc' => 'Turn on debugging (see DataObjects debugLevel )',
31 'desc' => 'List message to send, do not send them..',
39 'desc' => 'Show old messages..',
47 'desc' => 'Force redelivery, even if it has been sent before or not queued...',
57 var $table = 'core_notify';
58 var $target = 'Core/NotifySend';
59 var $evtype = ''; // any notification...
60 // this script should only handle EMAIL notifications..
64 $ff = HTML_FlexyFramework::get();
68 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
75 function get($r,$opts)
78 DB_DataObject::debugLevel($opts['debug']);
81 //date_default_timezone_set('UTC');
83 $showold = !empty($opts['old']);
84 if (!empty($opts['old'])) {
85 $opts['list'] = 1; // force listing..
88 $this->force = empty($opts['force']) ? 0 : 1;
90 if (!empty($opts['send-to'])) {
91 $this->send_to = $opts['send-to'];
95 $w = DB_DataObject::factory($this->table);
98 $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
101 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
104 $w->orderBy('act_when ASC'); // oldest first.
105 $w->limit(1000); // we can run 1000 ...
107 $w->orderBy('act_when DESC'); // latest first
108 $w->limit(50); // we can run 1000 ...
110 if (!empty($this->evtype)) {
111 $w->evtype = $this->evtype;
117 $ar = $w->fetchAll();
119 if (!empty($opts['list'])) {
121 die("Nothing in Queue\n");
127 echo "$w->id : $w->person_id_email email : ".
128 $o->toEventString()." ". $w->status() . "\n";
141 $p = array_shift($ar);
142 if (!$this->poolfree()) {
143 array_unshift($ar,$p); /// put it back on..
147 if ($this->poolHasDomain($p->person_id_email)) {
148 if (in_array($p->person_id_email, $pushed)) {
149 // it's been pushed to the end, and nothing has
150 // been pushed since.
151 // give up, let the next run sort it out.
155 $ar[] = $p; // push it on the end..
157 $pushed[] = $p->person_id_email;
159 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
165 $this->run($p->id,$p->person_id_email);
170 // we should have a time limit here...
171 while(count($this->pool)) {
179 function run($id, $email)
182 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
183 $descriptorspec = array(
184 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
185 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
186 2 => array("pipe", "w") // stderr is a file to write to
188 $php = $_SERVER["_"];
189 $sn = $_SERVER["SCRIPT_NAME"];
191 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
192 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
196 if (!empty($this->send_to)) {
197 $app .= ' --sent-to='.escapeshellarg($this->send_to);
199 $cmd = $php . ' ' . $app. ' &';
203 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
204 $info = proc_get_status($p);
205 $this->pool[] = array(
207 'pid' => $info['pid'],
221 foreach($this->pool as $p) {
223 echo "CHECK PID: " . $p['pid'] . "\n";
224 $info = proc_get_status($p['proc']);
226 if ($info['running']) {
228 //if (file_exists('/proc/'.$p['pid'])) {
233 fclose($p['pipes'][1]);
234 fclose($p['pipes'][0]);
235 fclose($p['pipes'][2]);
236 proc_close($p['proc']);
238 echo "ENDED: " . $p['cmd'] . " : " . file_get_contents($p['out']) . "\n";
243 echo "POOL SIZE: ". count($pool) ."\n";
245 if (count($pool) < 10) {
252 * see if pool is already trying to deliver to this domain.?
253 * -- if so it get's pushed to the end of the queue.
256 function poolHasDomain($email)
258 $dom = strtolower(array_pop(explode('@',$email)));
259 foreach($this->pool as $p) {
260 $mdom = strtolower(array_pop(explode('@',$p['email'])));