From 10a79d5a65f8d00808a5465d805b2c51900411a5 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 8 Mar 2019 12:50:48 +0100 Subject: [PATCH 1/3] Decouple CachedProductsRenderer from the Rails env This way we don't need to touch the class implementation to enable the products cache in development. Just change the default value in `app/models/spree/app_configuration_decorator.rb`. --- app/models/spree/app_configuration_decorator.rb | 2 +- config/environments/development.rb | 3 +++ config/environments/staging.rb | 3 +++ lib/open_food_network/cached_products_renderer.rb | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/spree/app_configuration_decorator.rb b/app/models/spree/app_configuration_decorator.rb index e909a0c69b..50d3321519 100644 --- a/app/models/spree/app_configuration_decorator.rb +++ b/app/models/spree/app_configuration_decorator.rb @@ -54,5 +54,5 @@ Spree::AppConfiguration.class_eval do preference :enable_localized_number?, :boolean, default: false # Enable cache - preference :enable_products_cache?, :boolean, default: true + preference :enable_products_cache?, :boolean, default: !Rails.env.development? end diff --git a/config/environments/development.rb b/config/environments/development.rb index 70e48c9aac..3e43ab7823 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -7,6 +7,9 @@ Openfoodnetwork::Application.configure do config.cache_classes = false config.cache_store = :memory_store + # Enable cache instrumentation, which is disabled by default + ActiveSupport::Cache::Store.instrument = true + # Log error messages when you accidentally call methods on nil. config.whiny_nils = true diff --git a/config/environments/staging.rb b/config/environments/staging.rb index b0e1b96301..0595e49495 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -42,6 +42,9 @@ Openfoodnetwork::Application.configure do # Use a different cache store in production config.cache_store = :dalli_store + # Enable cache instrumentation, which is disabled by default + ActiveSupport::Cache::Store.instrument = true + # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" diff --git a/lib/open_food_network/cached_products_renderer.rb b/lib/open_food_network/cached_products_renderer.rb index abc1f45713..ba96cea609 100644 --- a/lib/open_food_network/cached_products_renderer.rb +++ b/lib/open_food_network/cached_products_renderer.rb @@ -39,7 +39,7 @@ module OpenFoodNetwork end def use_cached_products? - Spree::Config[:enable_products_cache?] && (Rails.env.production? || Rails.env.staging?) + Spree::Config[:enable_products_cache?] end def uncached_products_json From 15a95a3bf5ccb2e8b414304d0734e2f40609e3aa Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 8 Mar 2019 12:57:54 +0100 Subject: [PATCH 2/3] Use :file_store cache store in development :memory_store uses the process' memory and therefore, what the background job writes is not reachable for the rails server process when it reads from the cache. See https://github.com/openfoodfoundation/openfoodnetwork/wiki/Products-cache#development. --- config/environments/development.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 3e43ab7823..3ec137a12a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -5,7 +5,9 @@ Openfoodnetwork::Application.configure do # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = false - config.cache_store = :memory_store + + # :file_store is used by default when no cache store is specifically configured. + # config.cache_store = :file_store # Enable cache instrumentation, which is disabled by default ActiveSupport::Cache::Store.instrument = true From fede58289bd3e4ff7573b36d3d1b1b6a340ad81e Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Mon, 11 Mar 2019 17:26:19 +0100 Subject: [PATCH 3/3] Move Cache::Store config to new initializer I don't know why but `Rails.logger` is still nil when evaluated from `configure` block in `config/environments/development.rb`. The only way I found to make ActiveSupport's cache to use the default logger is from an initializer. Note that `ActiveSupport::Cache::Store` uses `debug` level and so we need to set the dev logger in that same level to see its messages. If you want to debug in staging as well, you'll need to modify the log level manually. --- config/environments/development.rb | 5 ++--- config/environments/staging.rb | 3 --- config/initializers/cache_store.rb | 7 +++++++ 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 config/initializers/cache_store.rb diff --git a/config/environments/development.rb b/config/environments/development.rb index 3ec137a12a..20310726cb 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -9,9 +9,6 @@ Openfoodnetwork::Application.configure do # :file_store is used by default when no cache store is specifically configured. # config.cache_store = :file_store - # Enable cache instrumentation, which is disabled by default - ActiveSupport::Cache::Store.instrument = true - # Log error messages when you accidentally call methods on nil. config.whiny_nils = true @@ -45,4 +42,6 @@ Openfoodnetwork::Application.configure do # Show emails using Letter Opener config.action_mailer.delivery_method = :letter_opener config.action_mailer.default_url_options = { host: "0.0.0.0:3000" } + + config.log_level = :debug end diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 0595e49495..b0e1b96301 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -42,9 +42,6 @@ Openfoodnetwork::Application.configure do # Use a different cache store in production config.cache_store = :dalli_store - # Enable cache instrumentation, which is disabled by default - ActiveSupport::Cache::Store.instrument = true - # Enable serving of images, stylesheets, and JavaScripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" diff --git a/config/initializers/cache_store.rb b/config/initializers/cache_store.rb new file mode 100644 index 0000000000..009ebbaab6 --- /dev/null +++ b/config/initializers/cache_store.rb @@ -0,0 +1,7 @@ +unless Rails.env.production? + # Enable cache instrumentation, which is disabled by default + ActiveSupport::Cache::Store.instrument = true + + # Log message in the same default logger + ActiveSupport::Cache::Store.logger = Rails.logger +end