Skip to content

A platform for community discussion. Free, open, simple.

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

Martin Brennan93d7abe372ec
diff --git a/bin/unicorn b/bin/unicorn
index 865e8d87dce..38e238c1f31 100755
--- a/bin/unicorn
+++ b/bin/unicorn
@@ -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")
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 891bc01f07f..c61a2f14e7d 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -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!