class Ruboty::Brains::Moneta

Constants

KEY

Attributes

thread[R]

Public Class Methods

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

Public Instance Methods

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

Override.

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

Private Instance Methods

fetch_option(backend) click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 79
def fetch_option(backend)
  prefix = backend.to_s.underscore.upcase
  ENV.reduce({}) do |option, env|
    match = env[0].to_s.match(/MONETA_#{prefix}_([A-Z_]+)$/)
    next option if match.nil?
    option[match[1].downcase.to_sym] = env[1]
    option
  end
end
interval() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 61
def interval
  (ENV["MONETA_SAVE_INTERVAL"] || 5).to_i
end
key() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 106
def key
  @key ||= "#{namespace}:#{KEY}"
end
master() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 89
def master
  @master ||= ::Moneta.new(master_backend, fetch_option(master_backend))
end
master_backend() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 65
def master_backend
  (ENV["MONETA_BACKEND"] || "Memory").to_sym
end
namespace() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 75
def namespace
  ENV["MONETA_NAMESPACE"] || "ruboty"
end
pull() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 36
def pull
  if str = master[key]
    str
  end
rescue TypeError
end
push() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 32
def push
  master[key] = data
end
replicate() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 43
def replicate
  slaves.each do |slave|
    slave[key] = data
  end
end
slave_backend() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 69
def slave_backend
  eval(ENV["MONETA_BACKEND_SLAVE"] || "")
rescue NameError
  ENV["MONETA_BACKEND_SLAVE"]
end
slaves() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 93
def slaves
  @slaves ||=
  if slave_backend.is_a?(String)
    [::Moneta.new(slave_backend.to_sym, fetch_option(slave_backend))]
  elsif slave_backend.is_a?(Array)
    slave_backend.map do |slave_name|
      ::Moneta.new(slave_name.to_sym, fetch_option(slave_name))
    end
  else
    []
  end
end
sync() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 53
def sync
  loop do
    wait
    push
    replicate
  end
end
wait() click to toggle source
# File lib/ruboty/moneta/brains/moneta.rb, line 49
def wait
  sleep(interval)
end