module DRbQS::Misc

Constants

STRINGS_FOR_KEY

Public Class Methods

create_logger(log_file, log_level) click to toggle source
# File lib/drbqs/utility/misc.rb, line 46
def create_logger(log_file, log_level)
  if IO === log_file
    log_output = log_file
  elsif log_file
    log_output = FileName.create(log_file, :position => :middle, :directory => :parent, :type => :number)
  else
    log_output = STDOUT
  end
  logger = Logger.new(log_output)
  logger.level = log_level || Logger::ERROR
  logger
end
create_uri(opts = {}) click to toggle source

@param [Hash] opts The arguments of URI of server @option opts [Fixnum] :port Port number of a server @option opts [String] :host Hostname of a server @option opts [String] :unix Path for unix domain socket of a server

# File lib/drbqs/utility/misc.rb, line 28
def create_uri(opts = {})
  if opts[:port] || !opts[:unix]
    port = opts[:port] || ROOT_DEFAULT_PORT
    "druby://#{opts[:host]}:#{port}"
  elsif opts[:host]
    raise ArgumentError, "We can not set hostname to unix domain socket."
  else
    path = File.expand_path(opts[:unix])
    if !File.directory?(File.dirname(path))
      raise ArgumentError, "Directory #{File.dirname(path)} does not exist."
    elsif File.exist?(path)
      raise ArgumentError, "File #{path} already exists."
    end
    uri_drbunix(path)
  end
end
output_error(err, io = $stderr) click to toggle source
# File lib/drbqs/utility/misc.rb, line 88
def output_error(err, io = $stderr)
  backtrace = err.backtrace
  io.puts "#{backtrace[0]}: #{err.to_s} (#{err.class})"
  if backtrace.size > 1
    io.puts "        from #{backtrace[1..-1].join("\n        from ")}"
  end
end
process_running_normally?(pid) click to toggle source

If process of pid does not exist or its state is zombie then the method return false. If pid is invalid then the method also returns false. @param [Fixnum] pid PID of process

# File lib/drbqs/utility/misc.rb, line 83
def process_running_normally?(pid)
  Integer === pid && (ps_table = Sys::ProcTable.ps(pid)) && (ps_table.state != 'Z')
end
random_key(size = 20) click to toggle source
# File lib/drbqs/utility/misc.rb, line 72
def random_key(size = 20)
  n = STRINGS_FOR_KEY.size
  Array.new(size) do
    STRINGS_FOR_KEY[rand(n)]
  end.join
end
time_to_history_string(t) click to toggle source
# File lib/drbqs/utility/misc.rb, line 60
def time_to_history_string(t)
  t.strftime("%Y-%m-%d %H:%M:%S")
end
time_to_history_string2(t) click to toggle source
# File lib/drbqs/utility/misc.rb, line 65
def time_to_history_string2(t)
  t.strftime("%m-%d %H:%M:%S")
end
uri_drbunix(path) click to toggle source
# File lib/drbqs/utility/misc.rb, line 19
def uri_drbunix(path)
  "drbunix:#{path}"
end

Private Instance Methods

create_logger(log_file, log_level) click to toggle source
# File lib/drbqs/utility/misc.rb, line 46
def create_logger(log_file, log_level)
  if IO === log_file
    log_output = log_file
  elsif log_file
    log_output = FileName.create(log_file, :position => :middle, :directory => :parent, :type => :number)
  else
    log_output = STDOUT
  end
  logger = Logger.new(log_output)
  logger.level = log_level || Logger::ERROR
  logger
end
create_uri(opts = {}) click to toggle source

@param [Hash] opts The arguments of URI of server @option opts [Fixnum] :port Port number of a server @option opts [String] :host Hostname of a server @option opts [String] :unix Path for unix domain socket of a server

# File lib/drbqs/utility/misc.rb, line 28
def create_uri(opts = {})
  if opts[:port] || !opts[:unix]
    port = opts[:port] || ROOT_DEFAULT_PORT
    "druby://#{opts[:host]}:#{port}"
  elsif opts[:host]
    raise ArgumentError, "We can not set hostname to unix domain socket."
  else
    path = File.expand_path(opts[:unix])
    if !File.directory?(File.dirname(path))
      raise ArgumentError, "Directory #{File.dirname(path)} does not exist."
    elsif File.exist?(path)
      raise ArgumentError, "File #{path} already exists."
    end
    uri_drbunix(path)
  end
end
output_error(err, io = $stderr) click to toggle source
# File lib/drbqs/utility/misc.rb, line 88
def output_error(err, io = $stderr)
  backtrace = err.backtrace
  io.puts "#{backtrace[0]}: #{err.to_s} (#{err.class})"
  if backtrace.size > 1
    io.puts "        from #{backtrace[1..-1].join("\n        from ")}"
  end
end
process_running_normally?(pid) click to toggle source

If process of pid does not exist or its state is zombie then the method return false. If pid is invalid then the method also returns false. @param [Fixnum] pid PID of process

# File lib/drbqs/utility/misc.rb, line 83
def process_running_normally?(pid)
  Integer === pid && (ps_table = Sys::ProcTable.ps(pid)) && (ps_table.state != 'Z')
end
random_key(size = 20) click to toggle source
# File lib/drbqs/utility/misc.rb, line 72
def random_key(size = 20)
  n = STRINGS_FOR_KEY.size
  Array.new(size) do
    STRINGS_FOR_KEY[rand(n)]
  end.join
end
time_to_history_string(t) click to toggle source
# File lib/drbqs/utility/misc.rb, line 60
def time_to_history_string(t)
  t.strftime("%Y-%m-%d %H:%M:%S")
end
time_to_history_string2(t) click to toggle source
# File lib/drbqs/utility/misc.rb, line 65
def time_to_history_string2(t)
  t.strftime("%m-%d %H:%M:%S")
end
uri_drbunix(path) click to toggle source
# File lib/drbqs/utility/misc.rb, line 19
def uri_drbunix(path)
  "drbunix:#{path}"
end