Class: Katalyst::Healthcheck::Store::Redis
- Inherits:
-
Object
- Object
- Katalyst::Healthcheck::Store::Redis
- 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
- #options ⇒ Object readonly
Class Method Summary collapse
- .rails_redis_config ⇒ Object
-
.rails_redis_url ⇒ String
Redis URL defined in rails config.
Instance Method Summary collapse
-
#delete(name) ⇒ Object
Remove task state.
-
#initialize(options = {}) ⇒ Redis
constructor
A new instance of Redis.
-
#read ⇒ Array<Hash>
List of tasks attribute data.
-
#update(name, task_state = {}) ⇒ Object
Write task state.
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( = {}) = { url: self.class.rails_redis_url }.merge() if defined?(Rails) = DEFAULT_OPTIONS.merge() namespaced_cache_key = [ENV["RAILS_ENV"], [:cache_key]].compact.join("_") @options = Struct.new(:url, :cache_key).new([:url], namespaced_cache_key) end |
Instance Attribute Details
#options ⇒ Object (readonly)
38 39 40 |
# File 'lib/katalyst/healthcheck/store/redis.rb', line 38 def @options end |
Class Method Details
.rails_redis_config ⇒ Object
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_url ⇒ String
Returns 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
76 77 78 |
# File 'lib/katalyst/healthcheck/store/redis.rb', line 76 def delete(name) update(name, nil) end |
#read ⇒ Array<Hash>
Returns 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
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 |