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