A platform for community discussion. Free, open, simple.
PERF: avoid shelling to get hostname aggressively
Sam Saffron28292d275994
diff --git a/app/jobs/base.rb b/app/jobs/base.rbindex b43bcb5729c..b4e1f2e77b0 100644--- a/app/jobs/base.rb+++ b/app/jobs/base.rb@@ -40,7 +40,7 @@ module Jobsself.class.mutex.synchronize do@data = {}- @data["hostname"] = `hostname`.strip # Hostname+ @data["hostname"] = Discourse.os_hostname@data["pid"] = Process.pid # Pid@data["database"] = db # DB name - multisite db name it ran on@data["job_id"] = jid # Job unique IDdiff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rbindex 1f839483b51..3f5614a0840 100644--- a/app/models/optimized_image.rb+++ b/app/models/optimized_image.rb@@ -9,7 +9,7 @@ class OptimizedImage < ActiveRecord::BaseURL_REGEX ||= /(\/optimized\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*)/def self.lock(upload_id, width, height)- @hostname ||= `hostname`.strip rescue "unknown"+ @hostname ||= Discourse.os_hostname# note, the extra lock here ensures we only optimize one image per machine on webs# this can very easily lead to runaway CPU so slowing it down is beneficial and it is hijacked#diff --git a/config/initializers/100-sidekiq.rb b/config/initializers/100-sidekiq.rbindex f3241e35625..892380708dd 100644--- a/config/initializers/100-sidekiq.rb+++ b/config/initializers/100-sidekiq.rb@@ -69,7 +69,7 @@ if Sidekiq.server?Rails.application.config.after_initialize doscheduler_hostname = ENV["UNICORN_SCHEDULER_HOSTNAME"]- if !scheduler_hostname || scheduler_hostname.split(',').include?(`hostname`.strip)+ if !scheduler_hostname || scheduler_hostname.split(',').include?(Discourse.os_hostname)MiniScheduler.start(workers: GlobalSetting.mini_scheduler_workers)endenddiff --git a/lib/demon/sidekiq.rb b/lib/demon/sidekiq.rbindex 6a8dc48d522..e5ff82f702c 100644--- a/lib/demon/sidekiq.rb+++ b/lib/demon/sidekiq.rb@@ -39,7 +39,7 @@ class Demon::Sidekiq < ::Demon::Base[['critical', 8], ['default', 4], ['low', 2], ['ultra_low', 1]].each do |queue_name, weight|custom_queue_hostname = ENV["UNICORN_SIDEKIQ_#{queue_name.upcase}_QUEUE_HOSTNAME"]- if !custom_queue_hostname || custom_queue_hostname.split(',').include?(`hostname`.strip)+ if !custom_queue_hostname || custom_queue_hostname.split(',').include?(Discourse.os_hostname)options << "-q"options << "#{queue_name},#{weight}"enddiff --git a/lib/discourse.rb b/lib/discourse.rbindex 62ba6133164..57e8778e90b 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -312,6 +312,24 @@ module Discourseendend+ # hostname of the server, operating system level+ # called os_hostname so we do no confuse it with current_hostname+ def self.os_hostname+ @os_hostname ||=+ begin+ require 'socket'+ Socket.gethostname+ rescue => e+ warn_exception(e, message: 'Socket.gethostname is not working')+ begin+ `hostname`.strip+ rescue => e+ warn_exception(e, message: 'hostname command is not working')+ 'unknown_host'+ end+ end+ end+# Get the current base URL for the current sitedef self.current_hostnameSiteSetting.force_hostname.presence || RailsMultisite::ConnectionManagement.current_hostnamediff --git a/lib/discourse_logstash_logger.rb b/lib/discourse_logstash_logger.rbindex 301354e991f..ba02b31935c 100644--- a/lib/discourse_logstash_logger.rb+++ b/lib/discourse_logstash_logger.rb@@ -8,7 +8,7 @@ class DiscourseLogstashLoggeruri: uri,sync: true,customize_event: ->(event) {- event['hostname'] = `hostname`.chomp+ event['hostname'] = Discourse.os_hostnameevent['severity_name'] = event['severity']event['severity'] = Object.const_get("Logger::Severity::#{event['severity']}")event['type'] = typediff --git a/lib/freedom_patches/schema_migration_details.rb b/lib/freedom_patches/schema_migration_details.rbindex 0f60581a698..5b06a81c26a 100644--- a/lib/freedom_patches/schema_migration_details.rb+++ b/lib/freedom_patches/schema_migration_details.rb@@ -31,7 +31,7 @@ module FreedomPatches)SQL- hostname = `hostname` rescue ""+ hostname = Discourse.os_hostnamesql = ActiveRecord::Base.public_send(:sanitize_sql_array, [sql, {version: version || "",duration: (time.real * 1000).to_i,diff --git a/lib/message_bus_diags.rb b/lib/message_bus_diags.rbindex d56badfe620..16bde3a9a14 100644--- a/lib/message_bus_diags.rb+++ b/lib/message_bus_diags.rb@@ -5,7 +5,7 @@ class MessageBusDiags@host_info = {}def self.my_id- @my_id ||= "#{`hostname`}-#{Process.pid}"+ @my_id ||= "#{Discourse.os_hostname}-#{Process.pid}"enddef self.seen_host(name)diff --git a/script/mwrap_sidekiq b/script/mwrap_sidekiqindex b3a73a59743..8c2a73a5b7b 100755--- a/script/mwrap_sidekiq+++ b/script/mwrap_sidekiq@@ -128,7 +128,7 @@ begin[['critical', 8], ['default', 4], ['low', 2], ['ultra_low', 1]].each do |queue_name, weight|custom_queue_hostname = ENV["UNICORN_SIDEKIQ_#{queue_name.upcase}_QUEUE_HOSTNAME"]- if !custom_queue_hostname || custom_queue_hostname.split(',').include?(`hostname`.strip)+ if !custom_queue_hostname || custom_queue_hostname.split(',').include?(Discourse.os_hostname)options << "-q"options << "#{queue_name},#{weight}"end