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 var $table = 'core_notify';
20 var $target = 'Core/NotifySend';
21 var $evtype = ''; // any notification...
22 // this script should only handle EMAIL notifications..
26 $ff = HTML_FlexyFramework::get();
30 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
39 //DB_DataObject::debugLevel(1);
40 //date_default_timezone_set('UTC');
43 $w = DB_DataObject::factory($this->table);
44 $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
45 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
47 $w->orderBy('act_when ASC'); // oldest first.
48 if (!empty($this->evtype)) {
49 $w->evtype = $this->evtype;
53 $w->limit(1000); // we can run 1000 ...
62 $p = array_shift($ar);
63 if (!$this->poolfree()) {
64 array_unshift($ar,$p); /// put it back on..
68 if ($this->poolHasDomain($p->person_id_email)) {
69 $ar[] = $p; // push it on the end..
70 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
76 $this->run($p->id,$p->person_id_email);
78 while(count($this->pool)) {
86 function run($id, $email)
89 $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
90 $descriptorspec = array(
91 0 => array("pipe", 'r'), // stdin is a pipe that the child will read from
92 1 => array("file", $tn, 'w'), // stdout is a pipe that the child will write to
93 2 => array("pipe", "w") // stderr is a file to write to
96 $sn = $_SERVER["SCRIPT_NAME"];
98 $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
99 $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . ' ' . $this->target . '/'. $id;
100 $cmd = $php . ' ' . $app. ' &';
103 $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
104 $this->pool[] = array(
115 foreach($this->pool as $p) {
116 $ar = proc_get_status($p['proc']);
119 if ($ar['running']) {
123 echo $p['cmd'] . " : " . file_get_contents($p['out']);
127 if (count($pool) < 10) {
134 * see if pool is already trying to deliver to this domain.?
135 * -- if so it get's pushed to the end of the queue.
138 function poolHasDomain($email)
140 $dom = strtolower(array_pop(explode('@',$email)));
141 foreach($this->pool as $p) {
142 $mdom = strtolower(array_pop(explode('@',$p['email'])));