Skip to content

Instantly share code, notes, and snippets.

@beltrachi
Last active December 11, 2015 22:49
Show Gist options
  • Select an option

  • Save beltrachi/4672655 to your computer and use it in GitHub Desktop.

Select an option

Save beltrachi/4672655 to your computer and use it in GitHub Desktop.
An Ubiquo Extension to reload settings every x seconds. Very useful when you want changes to be forwarded to workers or other rails instances.
# Extension that automatically reloads settings after some seconds, to allow
# web interface update settings changes after Rails loading.
module UbiquoExtensions
module SettingsExtension
def self.included(base)
base.class_eval do
class << self
include ClassMethods
cattr_accessor :last_reload_at
cattr_accessor :cache_time
alias_method_chain :get, :reload
end
self.cache_time = 5 # seconds
end
end
module ClassMethods
# Adds a reload check every x seconds
def get_with_reload(*params)
self.last_reload_at ||= Time.now
if Time.now - self.last_reload_at > self.cache_time
Rails.logger.debug("Reloading overridable Settings after being cached #{Time.now - self.last_reload_at}")
self.load_from_backend!
self.last_reload_at = Time.now
end
get_without_reload(*params)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment