diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cec61bf9d6f..8bef4ed2a59 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -111,29 +111,8 @@ class ApplicationController < ActionController::Base end end - def self.last_ar_cache_reset - @last_ar_cache_reset - end - - def self.last_ar_cache_reset=(val) - @last_ar_cache_reset - end - rescue_from ActiveRecord::StatementInvalid do |e| - - last_cache_reset = ApplicationController.last_ar_cache_reset - - if e.message =~ /UndefinedColumn/ && (last_cache_reset.nil? || last_cache_reset < 30.seconds.ago) - Rails.logger.warn "Clear Active Record cache cause schema appears to have changed!" - - ApplicationController.last_ar_cache_reset = Time.zone.now - - ActiveRecord::Base.connection.query_cache.clear - (ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table| - table.classify.constantize.reset_column_information rescue nil - end - end - + Discourse.reset_active_record_cache_if_needed(e) raise e end diff --git a/config/initializers/100-sidekiq.rb b/config/initializers/100-sidekiq.rb index 3940cdd4c8e..fee9a68ca05 100644 --- a/config/initializers/100-sidekiq.rb +++ b/config/initializers/100-sidekiq.rb @@ -49,6 +49,7 @@ class SidekiqLogsterReporter < Sidekiq::ExceptionHandler::Logger def call(ex, context = {}) return if Jobs::HandledExceptionWrapper === ex + Discourse.reset_active_record_cache_if_needed(ex) # Pass context to Logster fake_env = {} diff --git a/lib/discourse.rb b/lib/discourse.rb index 20227f2dfcd..d8e66fa405a 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -392,4 +392,24 @@ module Discourse [SiteSetting.tos_topic_id, SiteSetting.guidelines_topic_id, SiteSetting.privacy_topic_id] end + cattr_accessor :last_ar_cache_reset + + def self.reset_active_record_cache_if_needed(e) + last_cache_reset = Discourse.last_ar_cache_reset + if e && e.message =~ /UndefinedColumn/ && (last_cache_reset.nil? || last_cache_reset < 30.seconds.ago) + Rails.logger.warn "Clear Active Record cache cause schema appears to have changed!" + Discourse.last_ar_cache_reset = Time.zone.now + Discourse.reset_active_record_cache + end + end + + def self.reset_active_record_cache + ActiveRecord::Base.connection.query_cache.clear + (ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table| + table.classify.constantize.reset_column_information rescue nil + end + nil + end + + end