PERF: avoid shelling to get hostname aggressively

Previously we had many places in the app that called `hostname` to get
hostname of a server. This commit replaces the pattern in 2 ways

1. We cache the result in `Discourse.os_hostname` so it is only ever called once

2. We prefer to use Socket.gethostname which avoids making a shell command

This improves performance as we are not spawning hostname processes throughout
the app lifetime
This commit is contained in:
Sam Saffron
2020-02-18 15:11:30 +11:00
parent cfd56e9159
commit 28292d2759
9 changed files with 26 additions and 8 deletions

View File

@@ -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