Notify.php
[Pman.Core] / Notify.php
1 <?php
2 require_once 'Pman.php';
3
4 /**
5  * notification script runner
6  *
7  * This does not actualy send stuf out, it only starts the NotifySend/{id}
8  * which does the actuall notifcations.
9  *
10  * It manages a pool of notifiers.
11  * 
12  * 
13  */
14
15
16 class Pman_Core_Notify extends Pman
17 {
18     
19     static $cli_desc = "Send out notification emails (usually from cron)";
20     
21     static $cli_opts = array(
22         'debug' => array(
23             'desc' => 'Turn on debugging (see DataObjects debugLevel )',
24             'default' => 0,
25             'short' => 'v',
26             'min' => 1,
27             'max' => 1,
28             
29         ),
30         'list' => array(
31             'desc' => 'List message to send, do not send them..',
32             'default' => 0,
33             'short' => 'l',
34             'min' => 0,
35             'max' => 0,
36             
37         ),
38         'old' => array(
39             'desc' => 'Show old messages..',
40             'default' => 0,
41             'short' => 'l',
42             'min' => 0,
43             'max' => 0,
44             
45         )
46     );
47     
48     
49     
50     var $table = 'core_notify';
51     var $target = 'Core/NotifySend';
52     var $evtype = ''; // any notification...
53                     // this script should only handle EMAIL notifications..
54     
55     function getAuth()
56     {
57         $ff = HTML_FlexyFramework::get();
58         if (!$ff->cli) {
59             die("access denied");
60         }
61         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
62         return true;
63         
64     }
65     
66     var $pool = array();
67     
68     function get($r,$opts)    
69     {
70         if ($opts['debug']) {
71             DB_DataObject::debugLevel($opts['debug']);
72             print_r($opts);
73         }
74         //date_default_timezone_set('UTC');
75        // phpinfo();exit;
76         $showold = $opts['old'];
77         if (!empty($opts['old'])) {
78             $opts['list'] = 1; // force listing..
79         }
80         
81         
82         $w = DB_DataObject::factory($this->table);
83         
84         if (!$showold) {
85             $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
86             $w->whereAdd('act_when < NOW()'); // eg.. not if future..
87     
88             $w->orderBy('act_when ASC'); // oldest first.
89             $w->limit(1000); // we can run 1000 ...
90         } else {
91             $w->orderBy('act_when DESC'); // latest first
92             $w->limit(50); // we can run 1000 ...
93         }
94         if (!empty($this->evtype)) {
95             $w->evtype = $this->evtype;
96         }
97         
98         $w->autoJoin();
99         
100         
101         $ar = $w->fetchAll();
102         
103         if (!empty($opts['list'])) {
104             if (empty($ar)) {
105                 die("Nothing in Queue\n");
106             }
107             foreach($ar as $w) {
108                 $o = $w->object();
109                 echo "$e->id : $w->person_id_email email    : ". $o->toEventString()."\n";
110             }
111             exit;
112         }
113         
114         
115         
116         while (true) {
117             if (empty($ar)) {
118                 break;
119             }
120             
121             $p = array_shift($ar);
122             if (!$this->poolfree()) {
123                 array_unshift($ar,$p); /// put it back on..
124                 sleep(3);
125                 continue;
126             }
127             if ($this->poolHasDomain($p->person_id_email)) {
128                 $ar[] = $p; // push it on the end..
129                 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
130                 sleep(3);
131                 continue;
132             }
133             
134             
135             $this->run($p->id,$p->person_id_email);
136         }
137         while(count($this->pool)) {
138             $this->poolfree();
139             sleep(3);
140         }
141         
142         die("DONE\n");
143     }
144     
145     function run($id, $email)
146     {
147        // phpinfo();exit;
148         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
149         $descriptorspec = array(
150             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
151             1 => array("file", $tn, 'w'),  // stdout is a pipe that the child will write to
152             2 => array("pipe", "w") // stderr is a file to write to
153          );
154         $php = $_SERVER["_"];
155         $sn =  $_SERVER["SCRIPT_NAME"];
156         
157         $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
158         $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . '  ' . $this->target . '/'. $id;
159         $cmd = $php . ' ' . $app. ' &';
160         echo $cmd . "\n";
161         $pipe = array();
162         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
163         $this->pool[] = array(
164                 'proc' => $p,
165                 'out' => $tn,
166                 'cmd' => $cmd,
167                 'email' => $email
168         );
169     }
170     
171     function poolfree()
172     {
173         $pool = array();
174         foreach($this->pool as $p) {
175             $ar = proc_get_status($p['proc']);
176            // print_r($p);
177             //print_r($ar);
178             if ($ar['running']) {
179                 $pool[] = $p;
180                 continue;
181             }
182             echo $p['cmd'] . " : " . file_get_contents($p['out']);
183             //unlink($p['out']);
184         }
185         $this->pool = $pool;
186         if (count($pool) < 10) {
187             return true;
188         }
189         return false;
190         
191     }
192     /**
193      * see if pool is already trying to deliver to this domain.?
194      * -- if so it get's pushed to the end of the queue.
195      *
196      */
197     function poolHasDomain($email)
198     {
199         $dom = strtolower(array_pop(explode('@',$email)));
200         foreach($this->pool as $p) {
201             $mdom = strtolower(array_pop(explode('@',$p['email'])));
202             if ($mdom == $dom) {
203                 return true;
204             }
205         }
206         return false;
207         
208     }
209
210 }