module Pokan::Cluster::ClassMethods
Attributes
Public Instance Methods
Loads the server configuration, expecting a valid config hash in a block. This allows you to use whatever configuration format, as long as you can transform it to a valid pokan hash.
# File lib/pokan-cluster.rb, line 94 def config(&block) @server || load!(block.call) initialize_log store_hwinfo delegate end
Convenience method, allowing the user to set the file containing the configuration in the YAML format. In the future, maybe add builtin support for other different formats.
# File lib/pokan-cluster.rb, line 104 def config_file(path, options={}) config { YAML.load(File.read path) } end
Disables log for the gossip server
# File lib/pokan-cluster.rb, line 109 def disable_log @logging = false end
Sets the seed address and redis port, if specified
class Server include Pokan::Cluster gossip_with '10.10.10.3', :redis => 6549 end
If no Redis port is specified, 6379
is assumed (as it is the default Redis port).
# File lib/pokan-cluster.rb, line 123 def gossip_with(address, options) update_autorunner_option(:gossip_with, address) update_autorunner_option(:redis, options[:redis] || DEFAULTS[:redis]) end
# File lib/pokan-cluster.rb, line 128 def method_missing(meth, *args, &block) @delegator.send(meth, *args, &block) end
Private Instance Methods
adds callbacks on the server instance so that some events are logged
# File lib/pokan-cluster.rb, line 201 def add_log_callbacks @server.on(:start) do |address, tcp_port, udp_port, epoll, gossip_interval, seed_address, seed_port| @logger.debug LOG_MESSAGES[:start][:debug].with(address, tcp_port, udp_port, epoll, gossip_interval, seed_address, @log_level.to_s, @log_file).string @logger.info LOG_MESSAGES[:start][:info].with(address).string end @server.before(:sync) do |address, port| @logger.debug LOG_MESSAGES[:before_sync][:debug].with(address, port).string @logger.info LOG_MESSAGES[:before_sync][:info].string end @server.after(:sync) do |time_elapsed| @logger.debug LOG_MESSAGES[:after_sync][:debug].with(time_elapsed).string @logger.info LOG_MESSAGES[:after_sync][:info].string end @server.on(:new_key) do |key, value| @logger.debug LOG_MESSAGES[:new_key][:debug].with(key.to_s, value).string end @server.before(:gossip) do |address, port, digest_length| @logger.debug LOG_MESSAGES[:before_gossip][:debug].with(address, digest_length).string @logger.info LOG_MESSAGES[:before_gossip][:info].with(address).string end @server.after(:gossip) do @logger.debug LOG_MESSAGES[:after_gossip][:debug].string end @server.on(:shutdown) do @logger.info LOG_MESSAGES[:shutdown].string end end
# File lib/pokan-cluster.rb, line 162 def config_autorunner options = {} options.merge!({ :gossip_with => @seed }) unless @seed.nil? options.merge!({ :redis => @redis }) unless @redis.nil? Autorunner.options = options end
# File lib/pokan-cluster.rb, line 263 def cpuinfo @server.store(:nCores, Host::Processor.refresh.cores) end
Initializes the delegator instance so that unknown methods will be delegated to the server instance, providing a better API for developers who want to customize their server behavior.
# File lib/pokan-cluster.rb, line 242 def delegate require 'delegate' @delegator = DelegateClass(Pokan::Server).new(@server) end
Adds callbacks for logging on the happening of certain events, such as new keys being defined, changed, gossip messages, etc.
# File lib/pokan-cluster.rb, line 179 def initialize_log @log_initialized ||= false if logging? && !@log_initialized setup_logging add_log_callbacks end @log_initialized = true end
Sets variables according to the hash passed.
# File lib/pokan-cluster.rb, line 135 def load!(config_hash) config = Configuration.new(config_hash) @address = config.get('pokan.address') @server = Pokan::Server.new(@address) update_ivs(config) config_autorunner end
# File lib/pokan-cluster.rb, line 267 def loadinfo @server.store(:load1, Host::LoadAverage.refresh.on_last(1)) @server.store(:load5, Host::LoadAverage.refresh.on_last(5)) @server.store(:load15, Host::LoadAverage.refresh.on_last(15)) @server.store(:runningProcesses, Host::LoadAverage.refresh.running_processes) end
# File lib/pokan-cluster.rb, line 258 def meminfo @server.store(:totalMem, Host::Memory.refresh.total) @server.store(:freeMem, Host::Memory.refresh.free) end
# File lib/pokan-cluster.rb, line 274 def prepend(a_string, to_string) (a_string.to_s + to_string.to_s).to_sym end
requires and sets up the ‘logging` library.
# File lib/pokan-cluster.rb, line 191 def setup_logging @logger = Logging.logger['pokan-cluster'] appenders = [Logging.appenders.stdout] appenders << Logging.appenders.file(@log_file) if @log_file @logger.add_appenders *appenders @logger.level = @log_level end
Stores hardware information (memory, CPU and load average), unless the user specified an empty string in the configuration file (or in a server class).
# File lib/pokan-cluster.rb, line 250 def store_hwinfo @server.every(@gossip_interlval) { meminfo if @shares.include?('memory') cpuinfo if @shares.include?('cpu') loadinfo if @shares.include?('load') } end
# File lib/pokan-cluster.rb, line 170 def update_autorunner_option(key, value) opt = Autorunner.options opt[key] = value Autorunner.options = opt end
# File lib/pokan-cluster.rb, line 144 def update_ivs(config) @tcp_port = config.get('pokan.connect.tcp') || DEFAULTS[:tcp_port] @udp_port = config.get('pokan.connect.udp') || DEFAULTS[:udp_port] @epoll = config.get('pokan.connect.epoll') || DEFAULTS[:epoll] @gossip_interval = config.get('pokan.gossip.every') || DEFAULTS[:gossip_interval] @seed = config.get('pokan.seed.address') @redis = config.get('pokan.seed.redis') || DEFAULTS[:redis] @logging = config.get('pokan.log.enabled') || DEFAULTS[:logging] @log_level = (config.get('pokan.log.level') || DEFAULTS[:level]).to_sym @log_file = config.get('pokan.log.file') if config.get('pokan.gossip.share').nil? @shares = DEFAULTS[:shares] else @shares = config.get('pokan.gossip.share').split(',') end end