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' => 'o',
42             'min' => 0,
43             'max' => 0,
44             
45         ),
46          'force' => array(
47             'desc' => 'Force redelivery, even if it has been sent before or not queued...',
48             'default' => 0,
49             'short' => 'f',
50             'min' => 0,
51             'max' => 0,
52         ),
53     );
54     
55     
56     
57     var $table = 'core_notify';
58     var $target = 'Core/NotifySend';
59     var $evtype = ''; // any notification...
60                     // this script should only handle EMAIL notifications..
61     
62     function getAuth()
63     {
64         $ff = HTML_FlexyFramework::get();
65         if (!$ff->cli) {
66             die("access denied");
67         }
68         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
69         return true;
70         
71     }
72     
73     var $pool = array();
74     
75     function get($r,$opts)    
76     {
77         if ($opts['debug']) {
78             DB_DataObject::debugLevel($opts['debug']);
79             print_r($opts);
80         }
81         //date_default_timezone_set('UTC');
82        // phpinfo();exit;
83         $showold = !empty($opts['old']);
84         if (!empty($opts['old'])) {
85             $opts['list'] = 1; // force listing..
86         }
87         $this->force = empty($opts['force']) ? 0 : 1;
88      
89         
90         $w = DB_DataObject::factory($this->table);
91         
92         if (!$showold) {
93             $w->whereAdd('act_when > sent'); // eg.. sent is not valid..
94             
95             if (!$this->force) {
96                 $w->whereAdd('act_when < NOW()'); // eg.. not if future..
97             }
98     
99             $w->orderBy('act_when ASC'); // oldest first.
100             $w->limit(1000); // we can run 1000 ...
101         } else {
102             $w->orderBy('act_when DESC'); // latest first
103             $w->limit(50); // we can run 1000 ...
104         }
105         if (!empty($this->evtype)) {
106             $w->evtype = $this->evtype;
107         }
108         
109         $w->autoJoin();
110         
111         
112         $ar = $w->fetchAll();
113         
114         if (!empty($opts['list'])) {
115             if (empty($ar)) {
116                 die("Nothing in Queue\n");
117             }
118             foreach($ar as $w) {
119                 $o = $w->object();
120                 
121                 
122                 echo "$w->id : $w->person_id_email email    : ".
123                         $o->toEventString()."    ". $w->status() . "\n";
124             }
125             exit;
126         }
127         
128         
129         
130         while (true) {
131             if (empty($ar)) {
132                 break;
133             }
134             
135             $p = array_shift($ar);
136             if (!$this->poolfree()) {
137                 array_unshift($ar,$p); /// put it back on..
138                 sleep(3);
139                 continue;
140             }
141             if ($this->poolHasDomain($p->person_id_email)) {
142                 $ar[] = $p; // push it on the end..
143                 echo "domain {$p->person_id_email} already on queue, pushing to end.\n";
144                 sleep(3);
145                 continue;
146             }
147             
148             
149             $this->run($p->id,$p->person_id_email);
150         }
151         while(count($this->pool)) {
152             $this->poolfree();
153             sleep(3);
154         }
155         
156         die("DONE\n");
157     }
158     
159     function run($id, $email)
160     {
161        // phpinfo();exit;
162         $tn = tempnam(ini_get('session.save_path'),'stdout') . '.stdout';
163         $descriptorspec = array(
164             0 => array("pipe", 'r'),  // stdin is a pipe that the child will read from
165             1 => array("file", $tn, 'w'),  // stdout is a pipe that the child will write to
166             2 => array("pipe", "w") // stderr is a file to write to
167          );
168         $php = $_SERVER["_"];
169         $sn =  $_SERVER["SCRIPT_NAME"];
170         
171         $cwd = $sn[0] == '/' ? dirname($sn) : dirname(realpath(getcwd() . $sn)); // same as run on.. (so script should end up being same relatively..)
172         $app = $cwd . '/' . basename($_SERVER["SCRIPT_NAME"]) . '  ' . $this->target . '/'. $id;
173         if ($this->force) {
174             $app .= ' -f';
175         }
176         
177         $cmd = $php . ' ' . $app. ' &';
178         
179         echo $cmd . "\n";
180         $pipe = array();
181         $p = proc_open($cmd, $descriptorspec, $pipes, $cwd );
182         $this->pool[] = array(
183                 'proc' => $p,
184                 'out' => $tn,
185                 'cmd' => $cmd,
186                 'email' => $email
187         );
188     }
189     
190     function poolfree()
191     {
192         $pool = array();
193         foreach($this->pool as $p) {
194             $ar = proc_get_status($p['proc']);
195            // print_r($p);
196             //print_r($ar);
197             if ($ar['running']) {
198                 $pool[] = $p;
199                 continue;
200             }
201             echo $p['cmd'] . " : " . file_get_contents($p['out']);
202             //unlink($p['out']);
203         }
204         $this->pool = $pool;
205         if (count($pool) < 10) {
206             return true;
207         }
208         return false;
209         
210     }
211     /**
212      * see if pool is already trying to deliver to this domain.?
213      * -- if so it get's pushed to the end of the queue.
214      *
215      */
216     function poolHasDomain($email)
217     {
218         $dom = strtolower(array_pop(explode('@',$email)));
219         foreach($this->pool as $p) {
220             $mdom = strtolower(array_pop(explode('@',$p['email'])));
221             if ($mdom == $dom) {
222                 return true;
223             }
224         }
225         return false;
226         
227     }
228
229 }