Class: AbAdmin::Concerns::Reloadable::Reloader

Inherits:
Object
  • Object
show all
Defined in:
lib/ab_admin/concerns/reloadable.rb

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, logger = nil, &block) ⇒ Reloader

Returns a new instance of Reloader


22
23
24
25
26
27
28
# File 'lib/ab_admin/concerns/reloadable.rb', line 22

def initialize(key=nil, logger=nil, &block)
  @key = key || "#{model_name.singular}_check_reload_key"
  @block = block
  @logger = logger || Rails.logger
  @stamp = fetch_stamp
  log "init #@key with value #@stamp"
end

Instance Method Details

#checkObject


34
35
36
37
38
39
40
41
# File 'lib/ab_admin/concerns/reloadable.rb', line 34

def check
  stamp = fetch_stamp
  if @stamp != stamp
    @stamp = stamp
    @block.call()
    log "#@key reloaded to #@stamp"
  end
end

#expireObject


43
44
45
46
# File 'lib/ab_admin/concerns/reloadable.rb', line 43

def expire
  log "#@key expire"
  Rails.cache.delete(@key)
end

#fetch_stampObject


30
31
32
# File 'lib/ab_admin/concerns/reloadable.rb', line 30

def fetch_stamp
  Rails.cache.fetch(@key) { Time.now.to_i }
end

#log(msg) ⇒ Object


48
49
50
# File 'lib/ab_admin/concerns/reloadable.rb', line 48

def log(msg)
  @logger.info "[RELOADER] (#{Process.pid}) #{msg}"
end