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)
80 static $current = false;;
82 if ($current !== false) {
86 $ns = DB_DataObject::factory('core_notify_server');
87 $ns->poolname = $notify->poolname;
89 $ns->hostname = gethostname();
91 $notify->jerr("Server not found for this server " . gethostname() . " in core_notify_server" );
99 function isFirstServer()
101 $servers = $this->availableServers();
102 if (empty($servers)) {
105 // only run this on the first server...
106 return $this->id == $servers[0]->id;
110 // called on current server.
111 function assignQueues($notify)
115 $servers = $this->availableServers();
117 foreach($servers as $s) {
123 $notify->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
127 // only run this on the first server...
128 if ($this->id != $ids[0]) {
131 foreach($ids as $rn) {
135 $num_servers = count($ids);
137 if ($num_servers == 1) {
138 $p = DB_DataObject::factory($notify->table);
143 server_id = {$ids[0]}
149 act_start < NOW() + INTERVAL 3 HOUR
151 server_id != {$ids[0]}
156 // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END +1) % {$num_servers})
160 $p = DB_DataObject::factory($notify->table);
166 act_start < NOW() + INTERVAL 3 HOUR
168 server_id NOT IN (" . implode(",", $ids) . ")
170 if ($p->count() < 1) {
175 $p->selectAdd("id, ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END +1) % {$num_servers}) as rn");
176 $kv = $p->fetchAll('id','rn');
177 foreach($kv as $id => $r) {
178 $up[ $ids[$r] ][] = $id;
180 foreach($up as $sid => $nids) {
184 $p = DB_DataObject::factory($notify->table);
191 id IN (". implode(',', $nids). ')'
195 DB_DataObject::factory("core_notify_blacklist")->prune();
198 // called on current server.
200 function availableServers()
202 $ns = DB_DataObject::factory('core_notify_server');
203 $ns->poolname = $this->poolname;
205 $ns->orderBy('id ASC');
206 return $ns->fetchAll();
210 function updateNotifyToNextServer( $cn , $when = false, $allow_same = false)
212 // fixme - this should take into account blacklisted - and return false if no more servers are available
213 $email = empty($cn->to_email) ? ($cn->person() ? $cn->person()->email : $cn->to_email) : $cn->to_email;
215 $w = DB_DataObject::factory($cn->tableName());
218 $servers = $this->availableServers();
220 foreach($servers as $i => $s) {
221 if ($s->id == $this->id) {
226 $offset = ($start + 1) % count($servers);
228 while ($offset != $start) {
229 $s = $servers[$offset];
230 if (!$s->isBlacklisted($email)) {
235 if ($good == false && $allow_same) {
239 if ($good == false) {
246 $w->server_id = $good->id;
247 $w->act_when = $when === false ? $w->sqlValue('NOW() + INTERVAL 1 MINUTE') : $when;
253 function isBlacklisted($email)
255 // return current server id..
256 static $cache = array();
259 $ea = explode('@',$email);
260 $dom = strtolower(array_pop($ea));
261 if (isset($cache[$dom])) {
265 $cd = DB_DataObject::factory('core_domain')->loadOrCreate($dom);
267 $bl = DB_DataObject::factory('core_notify_blacklist');
268 $bl->server_id = $this->id;
269 $bl->domain_id = $cd->id;
279 $ff = HTML_FlexyFramework::get();
280 $ff->Mail['helo'] = $this->helo;
283 function checkSmtpResponse($errmsg, $core_domain)
285 $bl = DB_DataObject::factory('core_notify_blacklist');
286 $bl->server_id = $this->id;
287 $bl->domain_id = $core_domain->id;
291 // is it a blacklist message
292 if (!$bl->messageIsBlacklisted($errmsg)) {
295 $bl->error_str = $errmsg;
296 $bl->added_dt = $bl->sqlValue("NOW()");