DataObjects/Core_notify_server.php
[Pman.Core] / DataObjects / Core_notify_server.php
1 <?php
2 /**
3  * Table Definition for core_notify_server
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_notify_server';    // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $hostname;
15     public $helo;
16     public $poolname;
17     public $is_active;
18     public $last_send;
19     
20     function assignQueues($notify)
21     {
22          
23         $ns = DB_DataObject::factory('core_notify_server');
24         $ns->poolname = $notify->poolname;
25         $ns->is_active = 1;
26         $ns->orderBy('id ASC');
27         $ids = $ns->fetchAll('id' );
28         
29         
30         if (empty($ids)) {
31             $notify->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
32             
33         }
34         $self = $this->getCurrent($notify);
35         
36         // only run this on the first server...
37         if ($self->id != $ids[0]) {
38             return; 
39         }
40         foreach($ids as $rn) {
41             $up[$rn]  = array();
42         }
43         
44         $num_servers = count($ids);
45         
46         if ($num_servers == 1) {
47             $p = DB_DataObject::factory($notify->table);
48             $p->query("
49                 UPDATE
50                     {$this->table}
51                 SET
52                     server_id = {$ids[0]}
53                 WHERE
54                     sent < '2000-01-01'
55                     and
56                     event_id = 0
57                     and
58                     act_start < NOW() +  INTERVAL 3 HOUR 
59                     and
60                     server_id < 0
61             ");
62             return;
63         }
64         
65         // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})
66         
67         
68         
69         $p = DB_DataObject::factory($notify->table);
70         $p->whereAdd("
71                 sent < '2000-01-01'
72                 and
73                 event_id = 0
74                 and
75                 act_start < NOW() +  INTERVAL 3 HOUR 
76                 and
77                 server_id < 0"
78             
79         );
80         if ($p->count() < 1) {
81             return;
82         }
83         
84         $p->selectAdd();
85         $p->selectAdd("id, ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})  as rn");
86         $kv = $p->fetchAll('id,rn');
87         foreach($kv as $id => $r) {
88             $up[ $ids[$r] ][] = $id;
89         }
90         foreach($up as $sid => $nids) {
91             $p = DB_DataObject::factory($notify->table);
92             $p->query("
93                 UPDATE
94                     {$this->table}
95                 SET
96                     server_id = $sid
97                 WHERE
98                     id IN (". implode(",', $nids"). ')'
99             );
100         }
101         
102         
103           
104         
105     }
106     function getCurrent($notify)
107     {
108         $ns = DB_DataObject::factory('core_notify_server');
109         $ns->poolname = $notify->poolname;
110         $ns->is_active = 1;
111         $ns->hostname = gethostname();
112         if (!$ns->count()) {
113             $notify->jerr("Server not found for this server " .  gethostname() . " in core_notify_server" );
114         }
115         $ns->find(true);
116         return $ns;
117       
118 }