docbrown/app/services/edge_cache/bust.rb
Kirk Haines ed74f2f245
Abstract DatadogStatsClient to ForemStatsClient (#12304)
* This change abstracts the DatadogStatsClient into a ForemStatsClient.
The purpose of this abstraction is to set the foundation for a subsequent PR that will allow one to use New Relic for recording Forem stats, instead of Datadog, if there is a New Relic configuration found.
This specific change creates an abstraction layer that can be built upon, without changing any actual default behavior. All specs still pass.

* Use delegate instead of explicit methods.

* Delegate instead of explicit methods.

* Fix the error.

* Refactor according to the suggestions in the comments.

* Ooops.  Stats work better when all of the code is committed.

* Removing the alias of count to increment since that was done in error.
2021-01-27 11:25:44 -05:00

62 lines
1.4 KiB
Ruby

module EdgeCache
class Bust
TIMEFRAMES = [
[-> { 1.week.ago }, "week"],
[-> { 1.month.ago }, "month"],
[-> { 1.year.ago }, "year"],
[-> { 5.years.ago }, "infinity"],
].freeze
def self.call(path)
bust(path)
end
class << self
protected
def bust(path)
provider_class = determine_provider_class
return unless provider_class
if provider_class.respond_to?(:call)
provider_class.call(path)
true
else
Rails.logger.warn("#{provider_class} cannot be used without a #call implementation!")
ForemStatsClient.increment("edgecache_bust.invalid_provider_class",
tags: ["provider_class:#{provider_class}"])
false
end
end
end
def self.determine_provider_class
provider =
if fastly_enabled?
"fastly"
elsif nginx_enabled?
"nginx"
end
return unless provider
const_get(provider.capitalize)
end
private_class_method :determine_provider_class
def self.fastly_enabled?
ApplicationConfig["FASTLY_API_KEY"].present? && ApplicationConfig["FASTLY_SERVICE_ID"].present?
end
private_class_method :fastly_enabled?
def self.nginx_enabled?
ApplicationConfig["OPENRESTY_URL"].present?
end
private_class_method :nginx_enabled?
end
end