PERF: code not correctly caching git commands

Every check for Discourse version could result in shelling out.
This commit is contained in:
Sam 2017-10-04 14:22:23 +11:00
parent 0342324b47
commit a4d4db4f0c
2 changed files with 36 additions and 23 deletions

View File

@ -85,6 +85,11 @@ before_fork do |server, worker|
end end
end end
# preload discourse version
Discourse.git_version
Discourse.git_branch
Discourse.full_version
# get rid of rubbish so we don't share it # get rid of rubbish so we don't share it
GC.start GC.start

View File

@ -327,42 +327,50 @@ module Discourse
end end
end end
def self.git_version def self.ensure_version_file_loaded
return $git_version if $git_version unless @version_file_loaded
version_file = "#{Rails.root}/config/version.rb"
require version_file if File.exists?(version_file)
@version_file_loaded = true
end
end
git_cmd = 'git rev-parse HEAD' def self.git_version
self.load_version_or_git(git_cmd, Discourse::VERSION::STRING) { $git_version } ensure_version_file_loaded
$git_version ||=
begin
git_cmd = 'git rev-parse HEAD'
self.try_git(git_cmd, Discourse::VERSION::STRING)
end
end end
def self.git_branch def self.git_branch
return $git_branch if $git_branch ensure_version_file_loaded
git_cmd = 'git rev-parse --abbrev-ref HEAD' $git_branch ||=
self.load_version_or_git(git_cmd, 'unknown') { $git_branch } begin
git_cmd = 'git rev-parse --abbrev-ref HEAD'
self.try_git(git_cmd, 'unknown')
end
end end
def self.full_version def self.full_version
return $full_version if $full_version ensure_version_file_loaded
git_cmd = 'git describe --dirty --match "v[0-9]*"' $full_version ||=
self.load_version_or_git(git_cmd, 'unknown') { $full_version } begin
git_cmd = 'git describe --dirty --match "v[0-9]*"'
self.try_git(git_cmd, 'unknown')
end
end end
def self.load_version_or_git(git_cmd, default_value) def self.try_git(git_cmd, default_value)
version_file = "#{Rails.root}/config/version.rb"
version_value = false version_value = false
if File.exists?(version_file) begin
require version_file version_value = `#{git_cmd}`.strip
version_value = yield rescue
version_value = default_value
end end
# file does not exist or does not define the expected global variable
unless version_value
begin
version_value = `#{git_cmd}`.strip
rescue
version_value = default_value
end
end
if version_value.empty? if version_value.empty?
version_value = default_value version_value = default_value
end end