FIX: Automatically recover from bad sprockets cache in development (#8364)

We were having issues in development mode where the JS code had errors due to a bad cache. When starting a server in development mode in bin/unicorn we now get the git sha of the discourse HEAD and get a git sha of all plugins, and store them in a file. If the sha has changed then we delete tmp/cache to refresh the assets cache.
This commit is contained in:
Martin Brennan 2019-11-19 09:15:09 +10:00 committed by GitHub
parent a4dbec5bf9
commit 93d7abe372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 22 deletions

View File

@ -4,12 +4,35 @@
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
RAILS_ROOT = File.expand_path("../../", Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
require 'digest'
dev_mode = false
def ensure_cache_clean!
all_plugin_directories = Pathname.new(RAILS_ROOT + '/plugins').children.select(&:directory?)
core_git_sha = `git rev-parse HEAD`
sorted_plugin_shas = all_plugin_directories.map do |plugin_dir|
"#{plugin_dir}:" + `git ls-files -s #{plugin_dir} | git hash-object --stdin`
end.sort
supersha_hash = Digest::SHA1.hexdigest((sorted_plugin_shas + [core_git_sha]).join('|'))
hash_file = "#{RAILS_ROOT}/tmp/plugin-hash"
old_hash = File.exists?(hash_file) ? File.read(hash_file) : nil
if old_hash && old_hash != supersha_hash
puts "WARNING: It looks like your discourse plugins or core version have recently changed."
puts "The tmp/cache directory will be wiped to avoid development issues."
`rm -rf #{RAILS_ROOT}/tmp/cache`
puts
end
File.write(hash_file, supersha_hash)
end
# in development do some fussing around, to automate config
if !ARGV.include?("-E") &&
!ARGV.include?("--env") &&
@ -38,6 +61,7 @@ if !ARGV.include?("-E") &&
ENV["UNICORN_SIDEKIQS"] ||= "1"
ensure_cache_clean!
end
if ARGV.include?("--help")

View File

@ -183,29 +183,8 @@ module Discourse
end
def self.activate_plugins!
all_plugins = Plugin::Instance.find_all("#{Rails.root}/plugins")
if Rails.env.development?
plugin_hash = Digest::SHA1.hexdigest(all_plugins.map { |p| p.path }.sort.join('|'))
hash_file = "#{Rails.root}/tmp/plugin-hash"
old_hash = begin
File.read(hash_file)
rescue Errno::ENOENT
end
if old_hash && old_hash != plugin_hash
puts "WARNING: It looks like your discourse plugins have recently changed."
puts "It is highly recommended to remove your `tmp` directory, otherwise"
puts "plugins might not work."
puts
else
File.write(hash_file, plugin_hash)
end
end
@plugins = []
all_plugins.each do |p|
Plugin::Instance.find_all("#{Rails.root}/plugins").each do |p|
v = p.metadata.required_version || Discourse::VERSION::STRING
if Discourse.has_needed_version?(Discourse::VERSION::STRING, v)
p.activate!