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