Class: MotherBrain::SrvCtl
- Inherits:
-
Object
- Object
- MotherBrain::SrvCtl
- Extended by:
- MB::Mixin::CodedExit
- Defined in:
- lib/mb/srv_ctl.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
- .default_options ⇒ Object
- .parse(args, filename) ⇒ Hash
- .run(args, filename) ⇒ Object
- .ui ⇒ MB::Cli::Shell::Color, MB::Cli::Shell::Basic
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ SrvCtl
constructor
A new instance of SrvCtl.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SrvCtl
Returns a new instance of SrvCtl.
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( = {}) = self.class..merge() @config = MB::Config.from_file([:config]) unless [:log_level].nil? @config.log.level = [:log_level] end unless [:log_location].nil? @config.log.location = [:log_location] end unless [:daemonize].nil? @config.server.daemonize = [:daemonize] end unless [:pid_file].nil? @config.server.pid = [:pid_file] end MB::Logging.setup(@config.to_logger) end |
Instance Attribute Details
#config ⇒ Object (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_options ⇒ Object
8 9 10 11 12 |
# File 'lib/mb/srv_ctl.rb', line 8 def { config: MB::Config.default_path } end |
.parse(args, filename) ⇒ 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) = Hash.new OptionParser.new("Usage: #{filename} [options]") do |opts| opts.on("-c", "--config PATH", "path to configuration file", "(default: '#{[:config]}')") do |v| [:config] = File.(v) end opts.on("-v", "--verbose", "run with verbose output") do [:log_level] = Logger::DEBUG end opts.on("-d", "--daemonize", "run in daemon mode") do [:daemonize] = true unless [:log_location] [: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| [:pid_file] = File.(v) end opts.on("-l", "--log PATH", String, "path to log file") do |v| [:log_location] = v end opts.on("-k", "--kill", "kill mbsrv if running") do |v| [:kill] = v end opts.on_tail("-h", "--help", "show this message") do ui.say opts exit end end.parse!(args) end |
.run(args, filename) ⇒ Object
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 = parse(args, filename) ctl = new() [:kill] ? ctl.stop : ctl.start rescue MB::MBError => ex ui.error ex exit_with(ex) end |
.ui ⇒ 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
#start ⇒ Object
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 |
#stop ⇒ Object
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 |