Sha256: 695a1821abcbdef41d1065b5f01d4784a098fe17e3fe1ec21255335a893811c9

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module AbAdmin
  module Concerns
    module Reloadable
      extend ActiveSupport::Concern

      included do
        class_attribute :reload_checker
      end

      module ClassMethods
        def has_reload_check(key=nil, logger=nil, &block)
          self.reload_checker = ::AbAdmin::Concerns::Reloadable::Reloader.new(key, logger, &block)
        end

        def check_reload
          self.reload_checker.check
        end
      end

      class Reloader

        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

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

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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ab_admin-0.11.0 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.10.0 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.9.0 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.8.3 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.8.2 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.8.1 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.8.0 lib/ab_admin/concerns/reloadable.rb
ab_admin-0.7.0 lib/ab_admin/concerns/reloadable.rb