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..',
50 var $table = 'core_notify';
51 var $target = 'Core/NotifySend';
52 var $evtype = ''; // any notification...
53 // this script should only handle EMAIL notifications..
57 $ff = HTML_FlexyFramework::get();
61 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
68 function get($r,$opts)
71 DB_DataObject::debugLevel($opts['debug']);
74 //date_default_timezone_set('UTC');
76 $showold = $opts['old'];
77 if (!empty($opts['old'])) {
78 $opts['list'] = 1; // force listing..
82 $w = DB_DataObject::factory($this->table);
85 $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
86 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
88 $w->orderBy('act_when ASC'); // oldest first.
89 $w->limit(1000); // we can run 1000 ...
91 $w->orderBy('act_when DESC'); // latest first
92 $w->limit(50); // we can run 1000 ...
94 if (!empty($this->evtype)) {
95 $w->evtype = $this->evtype;
101 $ar = $w->fetchAll();
103 if (!empty($opts['list'])) {
105 die("Nothing in Queue\n");
111 echo "$e->id : $w->person_id_email email : ".
112 $o->toEventString()." ". $w->status() . "\n";
124 $p = array_shift($ar);
125 if (!$this->poolfree()) {
126 array_unshift($ar,$p); /// put it back on..
130 if ($this->poolHasDomain($p->person_id_email)) {
131 $ar[] = $p; // push it on the end..
132 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
138 $this->run($p->id,$p->person_id_email);
140 while(count($this->pool)) {
148 function run($id, $email)
151 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
152 $descriptorspec = array(
153 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
154 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
155 2 => array("pipe", "w") // stderr is a file to write to
157 $php = $_SERVER["_"];
158 $sn = $_SERVER["SCRIPT_NAME"];
160 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
161 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
162 $cmd = $php . ' ' . $app. ' &';
165 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
166 $this->pool[] = array(
177 foreach($this->pool as $p) {
178 $ar = proc_get_status($p['proc']);
181 if ($ar['running']) {
185 echo $p['cmd'] . " : " . file_get_contents($p['out']);
189 if (count($pool) < 10) {
196 * see if pool is already trying to deliver to this domain.?
197 * -- if so it get's pushed to the end of the queue.
200 function poolHasDomain($email)
202 $dom = strtolower(array_pop(explode('@',$email)));
203 foreach($this->pool as $p) {
204 $mdom = strtolower(array_pop(explode('@',$p['email'])));