class Ruboty::Brains::Redis

Constants

KEY

Attributes

thread[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/ruboty/brains/redis.rb, line 16
def initialize
  super
  @thread = Thread.new { sync }
  @thread.abort_on_exception = true
end

Public Instance Methods

data() click to toggle source
# File lib/ruboty/brains/redis.rb, line 22
def data
  @data ||= pull || {}
end
validate!() click to toggle source

Override.

Calls superclass method
# File lib/ruboty/brains/redis.rb, line 27
def validate!
  super
  Ruboty.die("#{self.class.usage}") unless url
end

Private Instance Methods

client() click to toggle source
# File lib/ruboty/brains/redis.rb, line 56
def client
  @client ||= ::Redis::Namespace.new(namespace, redis: redis)
end
interval() click to toggle source
# File lib/ruboty/brains/redis.rb, line 72
def interval
  (ENV["REDIS_SAVE_INTERVAL"] || 5).to_i
end
namespace() click to toggle source
# File lib/ruboty/brains/redis.rb, line 60
def namespace
  ENV["REDIS_NAMESPACE"] || "ruboty"
end
pull() click to toggle source
# File lib/ruboty/brains/redis.rb, line 38
def pull
  if str = client.get(KEY)
    Marshal.load(str)
  end
rescue TypeError
end
push() click to toggle source
# File lib/ruboty/brains/redis.rb, line 34
def push
  client.set(KEY, Marshal.dump(data))
end
redis() click to toggle source
# File lib/ruboty/brains/redis.rb, line 64
def redis
  ::Redis.new(url: url)
end
sync() click to toggle source
# File lib/ruboty/brains/redis.rb, line 45
def sync
  loop do
    wait
    push
  end
end
url() click to toggle source
# File lib/ruboty/brains/redis.rb, line 68
def url
  ENV["REDIS_URL"] || ENV["REDISTOGO_URL"]
end
wait() click to toggle source
# File lib/ruboty/brains/redis.rb, line 52
def wait
  sleep(interval)
end