This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 48ebc952 Rework: cache where possible; use marker file for migrating 
status
48ebc952 is described below

commit 48ebc9527d40e5d2fd28b72c20e42198f272341a
Author: Sebb <[email protected]>
AuthorDate: Tue Jan 9 23:40:21 2024 +0000

    Rework: cache where possible; use marker file for migrating status
---
 .gitignore               |  1 +
 lib/whimsy/asf/status.rb | 48 ++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
index 11bf4474..59742496 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,4 +8,5 @@ work
 .rakeTasks
 /assets/
 maintenance.txt
+migrating.txt
 .ruby-version
diff --git a/lib/whimsy/asf/status.rb b/lib/whimsy/asf/status.rb
index 58877489..c70cc5a1 100644
--- a/lib/whimsy/asf/status.rb
+++ b/lib/whimsy/asf/status.rb
@@ -7,24 +7,36 @@ require 'resolv'
 
 # common methods
 module Status
+  ACTIVE_HOSTNAME = "whimsy.apache.org"
+
+  # Cache unchanging values
+  @currentIP = nil # may not be resolvable
+  @hostname = nil
 
   # Are we the active node?
+  # i.e. is our IP address the same as that of the active node
   def self.active?
     Resolv::DNS.open do |rs|
-      active = rs.getaddress("whimsy.apache.org") # Official hostname as IP
-      current = rs.getaddress(Socket.gethostname) rescue nil # local as IP
-      return current == active
+      active = rs.getaddress(ACTIVE_HOSTNAME) # Official hostname as IP; might 
change during run
+      begin
+        @currentIP ||= rs.getaddress(hostname) # local as IP; should not 
change during a run
+      rescue Resolv::ResolvError => e # allow this to fail on a test node
+        raise unless testnode?
+        $stderr.puts "WARNING: Failed to resolve local IP address: #{e}"
+      end
+      return @currentIP == active
     end
   end
 
   # this hostname
   def self.hostname
-    `hostname`.chomp # TODO: could be cached?
+    @hostname ||= Socket.gethostname
+    @hostname
   end
 
   # are we migrating?
-  def self.migrating?
-    false # Edit as needed
+  def self.migrating? # This is used to disable updates, see 
self.updates_disallowed_reason
+    File.exist? '/srv/whimsy/migrating.txt' # default is false
   end
 
   # are we a test node?
@@ -47,8 +59,28 @@ module Status
   end
 end
 
+def self.currentIP # intended for CLI testing
+  Resolv::DNS.open do |rs|
+    begin
+      @currentIP ||= rs.getaddress(hostname) # local as IP; should not change 
during a run
+    rescue Resolv::ResolvError => e # allow this to fail on a test node
+      raise unless testnode?
+      $stderr.puts "WARNING: Failed to resolve local IP address: #{e}"
+    end
+  end
+end
+
+def self.activeIP # intended for CLI testing
+  Resolv::DNS.open.getaddress(ACTIVE_HOSTNAME)
+end
+
 # for debugging purposes
 if __FILE__ == $0
-  puts "active?: #{Status.active?} hostname: #{Status.hostname} migrating?: 
#{Status.migrating?}"
-  puts "reason: #{Status.updates_disallowed_reason}"
+  puts "hostname: #{Status.hostname}"
+  puts "IP: #{Status.currentIP || 'nil'}"
+  puts "Active IP: #{Status.activeIP} for #{Status::ACTIVE_HOSTNAME}"
+  puts "active?: #{Status.active?}"
+  puts "migrating?: #{Status.migrating?}"
+  puts "testnode?: #{Status.testnode?}"
+  puts "updates disallowed reason: #{Status.updates_disallowed_reason}"
 end

Reply via email to