Changed DataObjects/Core_notify_server.phpNotify.php
authorAlan <alan@roojs.com>
Thu, 5 Oct 2023 08:54:43 +0000 (16:54 +0800)
committerAlan <alan@roojs.com>
Thu, 5 Oct 2023 08:54:43 +0000 (16:54 +0800)
DataObjects/Core_notify_server.php
Notify.php

index f678e6c..b170c3d 100644 (file)
@@ -17,6 +17,72 @@ class Pman_Core_DataObjects_Core_notify_server extends DB_DataObject
     public $is_active;
     public $last_send;
     
+    function assignQueues($notify)
+    {
+         
+        $ns = DB_DataObject::factory('core_notify_server');
+        $ns->poolname = $notify->poolname;
+        $ns->is_active = 1;
+        $ns->orderBy('id ASC');
+        $map = $ns->fetchAll('id' ,'hostname' );
+        $ids = array_keys($map);
+        
+        if (empty($ids)) {
+            $this->jerr("no configured servers in core_notify_server for poolname = {$notify->poolname}");
+            
+        }
+        
+        // only run this on the first server...
+        if ($map[gethostname()] != $ids[0]) {
+            return; 
+        }
+        foreach($ids as $rn) {
+            $up[$rn]  = array();
+        }
+        
+        $num_servers = count($ids);
+        
+        // ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})
+        
+        
+        
+        $p = DB_DataObject::factory($notify->table);
+        $p->whereAdd("
+                sent < '2000-01-01'
+                and
+                event_id = 0
+                and
+                act_start < NOW() +  INTERVAL 3 HOUR 
+                and
+                server_id < 0"
+            
+        );
+        if ($p->count() < 1) {
+            return;
+        }
+        
+        $p->selectAdd();
+        $p->selectAdd("id, ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})  as rn");
+        $kv = $p->fetchAll('id,rn');
+        foreach($kv as $id => $r) {
+            $up[ $ids[$r] ][] = $id;
+        }
+        foreach($up as $sid => $nids) {
+            $p = DB_DataObject::factory($this->table);
+            $p->query("
+                UPDATE
+                    {$this->table}
+                SET
+                    server_id = $sid
+                WHERE
+                    id IN (". implode(",', $nids"). ')'
+            );
+        }
+        
+        
+          
+        
+    }
     
     
     
index 0e2d6a2..db30785 100644 (file)
@@ -422,61 +422,9 @@ class Pman_Core_Notify extends Pman
     
     function assignQueues()
     {
-        $ff = HTML_FlexyFramework::get();
-        
-        if (empty($ff->Core_Notify['servers'])) {
-            return;
-        }
-        
-        if (isset($ff->Core_Notify['servers-non-pool'][gethostname()])) {
-            return; 
-        }
-        
-        if (!isset($ff->Core_Notify['servers'][gethostname()])) {
-            
-            $this->jerr("Core_Notify['servers']['" . gethostname() ."'] is not set");
-        }
-        // only run this on the main server...
-        if (array_search(gethostname(),array_keys($ff->Core_Notify['servers'])) > 0) {
-            return;
-        }
         
-        $num_servers = count(array_keys($ff->Core_Notify['servers']));
-        $p = DB_DataObject::factory($this->table);
-        $p->whereAdd("
-                sent < '2000-01-01'
-                and
-                event_id = 0
-                and
-                act_start < NOW() +  INTERVAL 3 HOUR 
-                and
-                server_id < 0"
-            
-        );
-        if ($p->count() < 1) {
-            return;
-        }
-         $p = DB_DataObject::factory($this->table);
-        // 6 seconds on this machne...
-        $p->query("
-            UPDATE
-                {$this->table}
-            SET
-                server_id = ((@row_number := CASE WHEN @row_number IS NULL THEN 0 ELSE @row_number END  +1) % {$num_servers})
-            WHERE
-                sent < '2000-01-01'
-                and
-                event_id = 0
-                and
-                act_start < NOW() +  INTERVAL 3 HOUR 
-                and
-                server_id < 0
-            ORDER BY
-                id ASC
-            LIMIT
-                10000
-        ");
-
+        DB_DataObject::Factory('core_notify_server')->assignQueues($this);
+         
         
     }