Sha256: 292392e57ad9b151f8be7d0279392383fb9a429bd789e1f93efb6b0cc78fc297

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'resque/pool'
require "resque/pool/lifeguard/version"

module Resque
  class Pool
    class Lifeguard
      include Logging

      def initialize hostname: Socket.gethostname, defaults: -> (env) { {} }
        @defaults = defaults
        @hostname = hostname
      end

      def self.pool_key
        'resque-pool-lifeguard'
      end

      def self.all_pools
        all = Resque.redis.hgetall pool_key
        all.map { |k, v|
          [k, Resque.decode(v)]
        }.to_h
      rescue Resque::Helpers::DecodeException
        reset!
        {}
      end

      def self.reset!
        Resque.redis.del pool_key
      end

      def call env
        @values = values || set_defaults(env)
      end

      def reset!
        Resque.redis.hdel pool_key, hostname
      end

      def values
        Resque.decode Resque.redis.hget pool_key, hostname
      rescue Resque::Helpers::DecodeException
        reset!
        nil
      rescue Redis::TimeoutError, Redis::CannotConnectError
        log "Could not get config from redis"
        @values
      end

      def values= values
        Resque.redis.hset pool_key, hostname, (Resque.encode values)
      end

      def [] queues
        values.to_h[queues]
      end

      def []= queues, count
        self.values = if count.zero?
          values.to_h.tap do |vals| vals.delete queues end
        else
          values.to_h.merge queues => count
        end
      end

      private
      attr_reader :defaults
      attr_reader :hostname

      def pool_key
        self.class.pool_key
      end

      def set_defaults env
        self.values = defaults.(env)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
resque-pool-lifeguard-0.3.0 lib/resque/pool/lifeguard.rb
resque-pool-lifeguard-0.2.1 lib/resque/pool/lifeguard.rb