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                 
110                 
111                 echo "$e->id : $w->person_id_email email    : ".
112                         $o->toEventString()."    ". $w->status() . "\n";
113             }
114             exit;
115         }
116         
117         
118         
119         while (true) {
120             if (empty($ar)) {
121                 break;
122             }
123             
124             $p = array_shift($ar);
125             if (!$this->poolfree()) {
126                 array_unshift($ar,$p); /// put it back on..
127                 sleep(3);
128                 continue;
129             }
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";
133                 sleep(3);
134                 continue;
135             }
136             
137             
138             $this->run($p->id,$p->person_id_email);
139         }
140         while(count($this->pool)) {
141             $this->poolfree();
142             sleep(3);
143         }
144         
145         die("DONE\n");
146     }
147     
148     function run($id, $email)
149     {
150        // phpinfo();exit;
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
156          );
157         $php = $_SERVER["_"];
158         $sn =  $_SERVER["SCRIPT_NAME"];
159         
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. ' &';
163         echo $cmd . "\n";
164         $pipe = array();
165         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
166         $this->pool[] = array(
167                 'proc' => $p,
168                 'out' => $tn,
169                 'cmd' => $cmd,
170                 'email' => $email
171         );
172     }
173     
174     function poolfree()
175     {
176         $pool = array();
177         foreach($this->pool as $p) {
178             $ar = proc_get_status($p['proc']);
179            // print_r($p);
180             //print_r($ar);
181             if ($ar['running']) {
182                 $pool[] = $p;
183                 continue;
184             }
185             echo $p['cmd'] . " : " . file_get_contents($p['out']);
186             //unlink($p['out']);
187         }
188         $this->pool = $pool;
189         if (count($pool) < 10) {
190             return true;
191         }
192         return false;
193         
194     }
195     /**
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.
198      *
199      */
200     function poolHasDomain($email)
201     {
202         $dom = strtolower(array_pop(explode('@',$email)));
203         foreach($this->pool as $p) {
204             $mdom = strtolower(array_pop(explode('@',$p['email'])));
205             if ($mdom == $dom) {
206                 return true;
207             }
208         }
209         return false;
210         
211     }
212
213 }