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         } else {
90             $w->orderBy('act_when DESC'); // latest first
91         }
92         if (!empty($this->evtype)) {
93             $w->evtype = $this->evtype;
94         }
95         
96         $w->autoJoin();
97         $w->limit(1000); // we can run 1000 ...
98         
99         $ar = $w->fetchAll();
100         
101         if (!empty($opts['list'])) {
102             if (empty($ar)) {
103                 die("Nothing in Queue\n");
104             }
105             foreach($ar as $w) {
106                 $o = $w->object();
107                 echo "$e->id : $w->person_id_email email    : ". $o->toEventString()."\n";
108             }
109             exit;
110         }
111         
112         
113         
114         while (true) {
115             if (empty($ar)) {
116                 break;
117             }
118             
119             $p = array_shift($ar);
120             if (!$this->poolfree()) {
121                 array_unshift($ar,$p); /// put it back on..
122                 sleep(3);
123                 continue;
124             }
125             if ($this->poolHasDomain($p->person_id_email)) {
126                 $ar[] = $p; // push it on the end..
127                 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
128                 sleep(3);
129                 continue;
130             }
131             
132             
133             $this->run($p->id,$p->person_id_email);
134         }
135         while(count($this->pool)) {
136             $this->poolfree();
137             sleep(3);
138         }
139         
140         die("DONE\n");
141     }
142     
143     function run($id, $email)
144     {
145        // phpinfo();exit;
146         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
147         $descriptorspec = array(
148             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
149             1 => array("file", $tn, 'w'),  // stdout is a pipe that the child will write to
150             2 => array("pipe", "w") // stderr is a file to write to
151          );
152         $php = $_SERVER["_"];
153         $sn =  $_SERVER["SCRIPT_NAME"];
154         
155         $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
156         $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . '  ' . $this->target . '/'. $id;
157         $cmd = $php . ' ' . $app. ' &';
158         echo $cmd . "\n";
159         $pipe = array();
160         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
161         $this->pool[] = array(
162                 'proc' => $p,
163                 'out' => $tn,
164                 'cmd' => $cmd,
165                 'email' => $email
166         );
167     }
168     
169     function poolfree()
170     {
171         $pool = array();
172         foreach($this->pool as $p) {
173             $ar = proc_get_status($p['proc']);
174            // print_r($p);
175             //print_r($ar);
176             if ($ar['running']) {
177                 $pool[] = $p;
178                 continue;
179             }
180             echo $p['cmd'] . " : " . file_get_contents($p['out']);
181             //unlink($p['out']);
182         }
183         $this->pool = $pool;
184         if (count($pool) < 10) {
185             return true;
186         }
187         return false;
188         
189     }
190     /**
191      * see if pool is already trying to deliver to this domain.?
192      * -- if so it get's pushed to the end of the queue.
193      *
194      */
195     function poolHasDomain($email)
196     {
197         $dom = strtolower(array_pop(explode('@',$email)));
198         foreach($this->pool as $p) {
199             $mdom = strtolower(array_pop(explode('@',$p['email'])));
200             if ($mdom == $dom) {
201                 return true;
202             }
203         }
204         return false;
205         
206     }
207
208 }