Class: Katalyst::Healthcheck::Store::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/katalyst/healthcheck/store/redis.rb

Constant Summary collapse

DEFAULT_CACHE_KEY =
"katalyst_healthcheck_tasks"
MAX_WRITE_TIME =

milliseconds

5000
DEFAULT_HOST =
ENV.fetch("DEFAULT_REDIS_HOST", "localhost")
DEFAULT_PORT =
ENV.fetch("DEFAULT_REDIS_PORT", "6379")
DEFAULT_OPTIONS =
{
  url:       "redis://#{DEFAULT_HOST}:#{DEFAULT_PORT}",
  cache_key: DEFAULT_CACHE_KEY,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



40
41
42
43
44
45
# File 'lib/katalyst/healthcheck/store/redis.rb', line 40

def initialize(options = {})
  options  = { url: self.class.rails_redis_url }.merge(options) if defined?(Rails)
  options  = DEFAULT_OPTIONS.merge(options)
  namespaced_cache_key = [ENV["RAILS_ENV"], options[:cache_key]].compact.join("_")
  @options = Struct.new(:url, :cache_key).new(options[:url], namespaced_cache_key)
end

Instance Attribute Details

#optionsObject (readonly)



38
39
40
# File 'lib/katalyst/healthcheck/store/redis.rb', line 38

def options
  @options
end

Class Method Details

.rails_redis_configObject



30
31
32
33
34
# File 'lib/katalyst/healthcheck/store/redis.rb', line 30

def rails_redis_config
  Rails.application&.config_for(:redis)
rescue StandardError
  {}
end

.rails_redis_urlString

Returns Redis URL defined in rails config.

Returns:

  • (String)

    Redis URL defined in rails config



21
22
23
24
25
26
27
28
# File 'lib/katalyst/healthcheck/store/redis.rb', line 21

def rails_redis_url
  ENV.fetch("REDIS_URL") do
    redis_config = rails_redis_config || {}
    host         = redis_config["host"] || DEFAULT_HOST
    port         = redis_config["port"] || DEFAULT_PORT
    "redis://#{host}:#{port}"
  end
end

Instance Method Details

#delete(name) ⇒ Object

Remove task state

Parameters:

  • name (String)

    name of the task



76
77
78
# File 'lib/katalyst/healthcheck/store/redis.rb', line 76

def delete(name)
  update(name, nil)
end

#readArray<Hash>

Returns List of tasks attribute data.

Returns:

  • (Array<Hash>)

    List of tasks attribute data



48
49
50
51
52
# File 'lib/katalyst/healthcheck/store/redis.rb', line 48

def read
  data  = fetch
  tasks = data["tasks"] || {}
  tasks.values
end

#update(name, task_state = {}) ⇒ Object

Write task state

Parameters:

  • name (String)

    name of the task

  • task_state (Hash, nil) (defaults to: {})

    task details. If null, task state will be removed



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/katalyst/healthcheck/store/redis.rb', line 57

def update(name, task_state = {})
  lock_manager.lock!("#{cache_key}_lock", MAX_WRITE_TIME, {}) do
    now                = Time.now
    data               = fetch
    data["version"]    = Katalyst::Healthcheck::VERSION
    data["updated_at"] = now
    data["created_at"] ||= now
    task_data          = data["tasks"] ||= {}
    if task_state.nil?
      task_data.delete(name)
    else
      task_data[name] = task_state
    end
    client.set(cache_key, JSON.generate(data))
  end
end