Class: StarlingServer::Runner
- Inherits:
-
Object
- Object
- StarlingServer::Runner
- Defined in:
- lib/starling/server_runner.rb
Class Method Summary collapse
Instance Method Summary collapse
- #drop_privileges ⇒ Object
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #load_config_file(filename) ⇒ Object
- #parse_options ⇒ Object
- #setup_signal_traps ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/starling/server_runner.rb', line 19 def initialize @@instance = self @process = ProcessHelper.new([:logger], [:pid_file], [:user], [:group]) pid = @process.running? if pid STDERR.puts "There is already a Starling process running (pid #{pid}), exiting." exit(1) elsif pid.nil? STDERR.puts "Cleaning up stale pidfile at #{[:pid_file]}." end start end |
Class Method Details
.run ⇒ Object
11 12 13 |
# File 'lib/starling/server_runner.rb', line 11 def self.run new end |
.shutdown ⇒ Object
15 16 17 |
# File 'lib/starling/server_runner.rb', line 15 def self.shutdown @@instance.shutdown end |
Instance Method Details
#drop_privileges ⇒ Object
159 160 161 162 |
# File 'lib/starling/server_runner.rb', line 159 def drop_privileges Process.euid = [:user] if [:user] Process.egid = [:group] if [:group] end |
#load_config_file(filename) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/starling/server_runner.rb', line 36 def load_config_file(filename) YAML.load(File.open(filename))['starling'].each do |key, value| # alias some keys case key when "queue_path" then key = "path" when "log_file" then key = "logger" end [key.to_sym] = value if [:log_level].instance_of?(String) [:log_level] = Logger.const_get([:log_level]) end end end |
#parse_options ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/starling/server_runner.rb', line 51 def self. = { :host => '127.0.0.1', :port => 22122, :path => File.join(%w( / tmp starling spool )), :log_level => Logger::INFO, :daemonize => false, :timeout => 0, :pid_file => File.join(%w( / tmp starling starling.pid )) } OptionParser.new do |opts| opts.summary_width = 25 opts. = "Starling (#{StarlingServer::VERSION})\n\n", "usage: starling [options...]\n", " starling --help\n", " starling --version\n" opts.separator "" opts.separator "Configuration:" opts.on("-f", "--config FILENAME", "Config file (yaml) to load") do |filename| load_config_file(filename) end opts.on("-q", "--queue_path PATH", :REQUIRED, "Path to store Starling queue logs", "(default: #{[:path]})") do |queue_path| [:path] = queue_path end opts.separator ""; opts.separator "Network:" opts.on("-hHOST", "--host HOST", "Interface on which to listen (default: #{[:host]})") do |host| [:host] = host end opts.on("-pHOST", "--port PORT", Integer, "TCP port on which to listen (default: #{[:port]})") do |port| [:port] = port end opts.separator ""; opts.separator "Process:" opts.on("-d", "Run as a daemon.") do [:daemonize] = true end opts.on("-PFILE", "--pid FILENAME", "save PID in FILENAME when using -d option.", "(default: #{[:pid_file]})") do |pid_file| [:pid_file] = pid_file end opts.on("-u", "--user USER", Integer, "User to run as") do |user| [:user] = user end opts.on("-gGROUP", "--group GROUP", "Group to run as") do |group| [:group] = group end opts.separator ""; opts.separator "Logging:" opts.on("-L", "--log [FILE]", "Path to print debugging information.") do |log_path| [:logger] = log_path end opts.on("-l", "--syslog CHANNEL", "Write logs to the syslog instead of a log file.") do |channel| [:syslog_channel] = channel end opts.on("-v", "Increase logging verbosity (may be used multiple times).") do [:log_level] -= 1 end opts.on("-t", "--timeout [SECONDS]", Integer, "Time in seconds before disconnecting inactive clients (0 to disable).", "(default: #{[:timeout]})") do |timeout| [:timeout] = timeout end opts.separator ""; opts.separator "Miscellaneous:" opts.on_tail("-?", "--help", "Display this usage information.") do puts "#{opts}\n" exit end opts.on_tail("-V", "--version", "Print version number and exit.") do puts "Starling #{StarlingServer::VERSION}\n\n" exit end end.parse! end |
#setup_signal_traps ⇒ Object
175 176 177 178 |
# File 'lib/starling/server_runner.rb', line 175 def setup_signal_traps Signal.trap("INT") { shutdown } Signal.trap("TERM") { shutdown } end |
#shutdown ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/starling/server_runner.rb', line 164 def shutdown begin STDOUT.puts "Shutting down." StarlingServer::Base.logger.info "Shutting down." @server.stop rescue Object => e STDERR.puts "There was an error shutting down: #{e}" exit(70) end end |
#start ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/starling/server_runner.rb', line 144 def start drop_privileges @process.daemonize if [:daemonize] setup_signal_traps @process.write_pid_file STDOUT.puts "Starting at #{[:host]}:#{[:port]}." @server = StarlingServer::Base.new() @server.run @process.remove_pid_file end |