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