Class: MotherBrain::SrvCtl

Inherits:
Object
  • Object
show all
Extended by:
MB::Mixin::CodedExit
Defined in:
lib/mb/srv_ctl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SrvCtl

Returns a new instance of SrvCtl.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :config (String) — default: MB::Config.default_path
  • :daemonize (Boolean)
  • :pid_file (String)
  • :log_level (Integer)
  • :log_location (String)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mb/srv_ctl.rb', line 86

def initialize(options = {})
  options  = self.class.default_options.merge(options)
  @config  = MB::Config.from_file(options[:config])

  unless options[:log_level].nil?
    @config.log.level = options[:log_level]
  end

  unless options[:log_location].nil?
    @config.log.location = options[:log_location]
  end

  unless options[:daemonize].nil?
    @config.server.daemonize = options[:daemonize]
  end

  unless options[:pid_file].nil?
    @config.server.pid = options[:pid_file]
  end

  MB::Logging.setup(@config.to_logger)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



79
80
81
# File 'lib/mb/srv_ctl.rb', line 79

def config
  @config
end

Class Method Details

.default_optionsObject



8
9
10
11
12
# File 'lib/mb/srv_ctl.rb', line 8

def default_options
  {
    config: MB::Config.default_path
  }
end

.parse(args, filename) ⇒ Hash

Parameters:

  • args (Array)
  • filename (#to_s)

Returns:

  • (Hash)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mb/srv_ctl.rb', line 18

def parse(args, filename)
  options = Hash.new

  OptionParser.new("Usage: #{filename} [options]") do |opts|
    opts.on("-c", "--config PATH", "path to configuration file", "(default: '#{default_options[:config]}')") do |v|
      options[:config] = File.expand_path(v)
    end

    opts.on("-v", "--verbose", "run with verbose output") do
      options[:log_level] = Logger::DEBUG
    end

    opts.on("-d", "--daemonize", "run in daemon mode") do
      options[:daemonize] = true

      unless options[:log_location]
        options[:log_location] = FileSystem.logs.join('application.log').to_s
      end
    end

    opts.on("-P", "--pid PATH", String, "pid file to read/write from") do |v|
      options[:pid_file] = File.expand_path(v)
    end

    opts.on("-l", "--log PATH", String, "path to log file") do |v|
      options[:log_location] = v
    end

    opts.on("-k", "--kill", "kill mbsrv if running") do |v|
      options[:kill] = v
    end

    opts.on_tail("-h", "--help", "show this message") do
      ui.say opts
      exit
    end
  end.parse!(args)

  options
end

.run(args, filename) ⇒ Object

Parameters:

  • args (Array)
  • filename (String)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mb/srv_ctl.rb', line 61

def run(args, filename)
  MB::Logging.setup

  options = parse(args, filename)
  ctl = new(options)

  options[:kill] ? ctl.stop : ctl.start
rescue MB::MBError => ex
  ui.error ex
  exit_with(ex)
end

.uiMB::Cli::Shell::Color, MB::Cli::Shell::Basic

Returns:

  • (MB::Cli::Shell::Color, MB::Cli::Shell::Basic)


74
75
76
# File 'lib/mb/srv_ctl.rb', line 74

def ui
  @ui ||= MB::Cli::Shell.shell.new
end

Instance Method Details

#startObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mb/srv_ctl.rb', line 109

def start
  if config.server.daemonize
    if pid
      ui.say "mbsrv already started"
      exit 1
    end

    daemonize
  end

  MB::Application.run(config)
end

#stopObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mb/srv_ctl.rb', line 122

def stop
  unless pid
    ui.say "mbsrv not started"
    exit 0
  end

  Process.kill('TERM', pid)
  destroy_pid
rescue Errno::ESRCH
  destroy_pid
end