From 97d5bc47b6caee3a7d58b5d34d7b869ca2848ebb Mon Sep 17 00:00:00 2001 From: Vaidehi Joshi Date: Fri, 6 Nov 2020 08:58:59 -0800 Subject: [PATCH] Use OPENRESTY_URL over protocol + domain (#11289) * Use OPENRESTY_URL over protocol + domain * Prefer OpenResty over Openresty --- .env_sample | 7 +++---- app/services/edge_cache/bust.rb | 2 +- app/services/edge_cache/bust/nginx.rb | 15 ++++++--------- spec/services/edge_cache/bust_spec.rb | 10 ++++------ 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/.env_sample b/.env_sample index bfdbefa8a..61b486f6f 100644 --- a/.env_sample +++ b/.env_sample @@ -6,10 +6,6 @@ export APP_DOMAIN="localhost:3000" export APP_PROTOCOL="http://" export FOREM_OWNER_SECRET="secret" -# Openresty domain + Protocol setting for development -export OPENRESTY_DOMAIN="" -export OPENRESTY_PROTOCOL="" - # Community Related Variables export COMMUNITY_NAME="DEV(local)" @@ -37,6 +33,9 @@ export REDIS_SIDEKIQ_URL="redis://localhost:6379" # Elasticsearch export ELASTICSEARCH_URL="http://localhost:9200" +# OpenResty +export OPENRESTY_URL="" + # SSL config export FORCE_SSL_IN_RAILS="not applicable in dev" diff --git a/app/services/edge_cache/bust.rb b/app/services/edge_cache/bust.rb index d40f66a56..0162dc972 100644 --- a/app/services/edge_cache/bust.rb +++ b/app/services/edge_cache/bust.rb @@ -44,7 +44,7 @@ module EdgeCache end def nginx_enabled? - ApplicationConfig["OPENRESTY_PROTOCOL"].present? && ApplicationConfig["OPENRESTY_DOMAIN"].present? + ApplicationConfig["OPENRESTY_URL"].present? end end end diff --git a/app/services/edge_cache/bust/nginx.rb b/app/services/edge_cache/bust/nginx.rb index e13f00d46..6453f2f1a 100644 --- a/app/services/edge_cache/bust/nginx.rb +++ b/app/services/edge_cache/bust/nginx.rb @@ -4,7 +4,7 @@ module EdgeCache def self.call(path) return unless nginx_available? - uri = URI.parse("#{openresty_path}#{path}") + uri = URI.parse("#{ApplicationConfig['OPENRESTY_URL']}#{path}") http = Net::HTTP.new(uri.host, uri.port) response = http.request Net::HTTP::Purge.new(uri.request_uri) @@ -13,10 +13,6 @@ module EdgeCache response.body end - def self.openresty_path - "#{ApplicationConfig['OPENRESTY_PROTOCOL']}#{ApplicationConfig['OPENRESTY_DOMAIN']}" - end - def self.nginx_available? # TODO: (Vaidehi Joshi) - Right now, we are checking that nginx is # available on every purge request/call to this bust service. If we are going @@ -24,16 +20,17 @@ module EdgeCache # available just once, and persist it on the class with @provider_available?. # Then, we could allow for an array of @paths = [] to be passed in, # and on single bust instance could bust multiple paths in order. - uri = URI.parse(openresty_path) + uri = URI.parse(ApplicationConfig["OPENRESTY_URL"]) http = Net::HTTP.new(uri.host, uri.port) response = http.get(uri.request_uri) return true if response.is_a?(Net::HTTPSuccess) rescue StandardError - # If we can't connect to Openresty, alert ourselves that + # If we can't connect to OpenResty, alert ourselves that # it is unavailable and return false. - Rails.logger.error("Could not connect to Openresty via #{openresty_path}!") - DatadogStatsClient.increment("edgecache_bust.service_unavailable", tags: ["path:#{openresty_path}"]) + Rails.logger.error("Could not connect to OpenResty via #{ApplicationConfig['OPENRESTY_URL']}!") + DatadogStatsClient.increment("edgecache_bust.service_unavailable", + tags: ["path:#{ApplicationConfig['OPENRESTY_URL']}"]) false end end diff --git a/spec/services/edge_cache/bust_spec.rb b/spec/services/edge_cache/bust_spec.rb index c3f5bfc81..7dab96667 100644 --- a/spec/services/edge_cache/bust_spec.rb +++ b/spec/services/edge_cache/bust_spec.rb @@ -58,7 +58,7 @@ RSpec.describe EdgeCache::Bust, type: :service do stub_fastly end - context "when openresty is not configured" do + context "when OpenResty is not configured" do before do stub_nginx end @@ -74,7 +74,7 @@ RSpec.describe EdgeCache::Bust, type: :service do end end - context "when openresty is configured and available" do + context "when OpenResty is configured and available" do before do configure_nginx end @@ -97,8 +97,7 @@ RSpec.describe EdgeCache::Bust, type: :service do end def stub_nginx - allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_PROTOCOL").and_return(nil) - allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_DOMAIN").and_return(nil) + allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_URL").and_return(nil) end def configure_fastly @@ -108,7 +107,6 @@ RSpec.describe EdgeCache::Bust, type: :service do end def configure_nginx - allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_PROTOCOL").and_return("http://") - allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_DOMAIN").and_return("localhost:9090") + allow(ApplicationConfig).to receive(:[]).with("OPENRESTY_URL").and_return("http://localhost:9090") end end