Module: DRbQS::Misc

Included in:
Command::Base, Server, Server::History, Server::Message, Setting::Base, Setting::Server
Defined in:
lib/drbqs/utility/misc.rb

Defined Under Namespace

Classes: LoggerDummy

Constant Summary collapse

STRINGS_FOR_KEY =
('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a

Class Method Summary collapse

Class Method Details

.create_logger(log_file, log_level) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# 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 = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    The arguments of URI of server

Options Hash (opts):

  • :port (Fixnum)

    Port number of a server

  • :host (String)

    Hostname of a server

  • :unix (String)

    Path for unix domain socket of a server



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



88
89
90
91
92
93
94
# 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) ⇒ Boolean

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.

Parameters:

  • pid (Fixnum)

    PID of process

Returns:

  • (Boolean)


83
84
85
# 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) ⇒ Object



72
73
74
75
76
77
# 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) ⇒ Object



60
61
62
# 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) ⇒ Object



65
66
67
# 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) ⇒ Object



19
20
21
# File 'lib/drbqs/utility/misc.rb', line 19

def uri_drbunix(path)
  "drbunix:#{path}"
end