3 * Table Definition for core_notify_server
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'core_notify_server'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
22 function applyFilters($q, $au, $roo)
24 if (isset($q['_with_queue_size'])) {
25 $this->addQueueSize();
30 function addQueueSize()
32 // look for database links for server_id (which should find core_notify + others..)
33 $cn = get_class(DB_DataObject::factory('core_notify'));
35 foreach($this->databaseLinks() as $tbl => $kv) {
36 foreach($kv as $k=>$v) {
37 if ($v != 'core_notify_server:id') {
41 $test = DB_DAtaObject::factory($tbl);
42 if (!is_a($test, $cn)) {
49 die("OOPS - no tables for notify_server references");
52 foreach($tables as $t) {
59 server_id = core_notify_server.id
61 sent < '1970-01-01' OR sent IS NULL
67 $this->selectAdd("(" . implode(" + ", $totals) . ") as in_queue ");
76 // most services should call this first..
78 function getCurrent($notify, $force = false)
80 static $current = false;;
82 if ($current !== false) {
86 $ns = DB_DataObject::factory('core_notify_server');
88 $ns->poolname = $notify->poolname;
90 $ns->hostname = gethostname();
92 if ($ns->find(true)) {
97 $notify->jerr("Server not found for this server " . gethostname() . " in core_notify_server" );
99 // fallback to any server - if we are using force. (this is so helo will work...)
101 $ns = DB_DataObject::factory('core_notify_server');
103 $ns->hostname = gethostname();
104 if (!$ns->find(true)) {
105 $notify->jerr("Server not found for this server " . gethostname() . " in core_notify_server" );
112 function isFirstServer()
114 $servers = $this->availableServers();
115 if (empty($servers)) {
118 // only run this on the first server...
119 return $this->id == $servers[0]->id;
123 // called on current server.
124 function assignQueues($notify)
128 $servers = $this->availableServers();
130 foreach($servers as $s) {
136 $notify->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
140 // only run this on the first server...
141 if ($this->id != $ids[0]) {
144 foreach($ids as $rn) {
148 $num_servers = count($ids);
150 if ($num_servers == 1) {
151 $p = DB_DataObject::factory($notify->table);
156 server_id = {$ids[0]}
162 act_start < NOW() + INTERVAL 3 HOUR
164 server_id != {$ids[0]}
169 // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END +1) % {$num_servers})
173 $p = DB_DataObject::factory($notify->table);
179 act_start < NOW() + INTERVAL 3 HOUR
181 server_id NOT IN (" . implode(",", $ids) . ")
183 if ($p->count() < 1) {
188 $p->selectAdd("id, ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END +1) % {$num_servers}) as rn");
189 $kv = $p->fetchAll('id','rn');
190 foreach($kv as $id => $r) {
191 $up[ $ids[$r] ][] = $id;
193 foreach($up as $sid => $nids) {
197 $p = DB_DataObject::factory($notify->table);
204 id IN (". implode(',', $nids). ')'
208 DB_DataObject::factory("core_notify_blacklist")->prune();
211 // called on current server.
213 function availableServers()
215 $ns = DB_DataObject::factory('core_notify_server');
216 $ns->poolname = $this->poolname;
218 $ns->orderBy('id ASC');
219 return $ns->fetchAll();
223 function updateNotifyToNextServer( $cn , $when = false, $allow_same = false)
225 // fixme - this should take into account blacklisted - and return false if no more servers are available
226 $email = empty($cn->to_email) ? ($cn->person() ? $cn->person()->email : $cn->to_email) : $cn->to_email;
228 $w = DB_DataObject::factory($cn->tableName());
231 $servers = $this->availableServers();
233 foreach($servers as $i => $s) {
234 if ($s->id == $this->id) {
239 $offset = ($start + 1) % count($servers);
241 while ($offset != $start) {
242 $s = $servers[$offset];
243 if (!$s->isBlacklisted($email)) {
248 if ($good == false && $allow_same) {
252 if ($good == false) {
259 $w->server_id = $good->id;
260 $w->act_when = $when === false ? $w->sqlValue('NOW() + INTERVAL 1 MINUTE') : $when;
266 function isBlacklisted($email)
268 // return current server id..
269 static $cache = array();
272 $ea = explode('@',$email);
273 $dom = strtolower(array_pop($ea));
274 if (isset($cache[$dom])) {
278 $cd = DB_DataObject::factory('core_domain')->loadOrCreate($dom);
280 $bl = DB_DataObject::factory('core_notify_blacklist');
281 $bl->server_id = $this->id;
282 $bl->domain_id = $cd->id;
292 $ff = HTML_FlexyFramework::get();
293 $ff->Mail['helo'] = $this->helo;
296 function checkSmtpResponse($errmsg, $core_domain)
298 $bl = DB_DataObject::factory('core_notify_blacklist');
299 $bl->server_id = $this->id;
300 $bl->domain_id = $core_domain->id;
304 // is it a blacklist message
305 if (!$bl->messageIsBlacklisted($errmsg)) {
308 $bl->error_str = $errmsg;
309 $bl->added_dt = $bl->sqlValue("NOW()");