Class: Goliath::Runner
- Inherits:
-
Object
- Object
- Goliath::Runner
- Defined in:
- lib/goliath/runner.rb
Overview
The Goliath::Runner is responsible for parsing any provided options, setting up the rack application, creating a logger, and then executing the Goliath::Server with the loaded information.
Instance Attribute Summary collapse
-
#address ⇒ String
The address of the server @example 127.0.0.1.
-
#api ⇒ Object
The API application.
-
#app ⇒ Object
The Rack application.
-
#app_options ⇒ Hash
Any additional server options.
-
#daemonize ⇒ Boolean
Flag to determine if the server should daemonize.
-
#log_file ⇒ String
The log file for the server.
-
#log_stdout ⇒ Boolean
Flag to determine if the server should log to standard output.
-
#logger ⇒ Object
Allow to inject a custom logger.
-
#options ⇒ Hash
readonly
The parsed options.
-
#pid_file ⇒ String
The pid file for the server.
-
#plugins ⇒ Array
The plugins the server will execute.
-
#port ⇒ Integer
The port of the server @example 9000.
-
#verbose ⇒ Boolean
Flag to determine if the server should run in verbose mode.
Instance Method Summary collapse
-
#initialize(argv, api) ⇒ Goliath::Runner
constructor
Create a new Goliath::Runner.
-
#load_plugins(plugins) ⇒ Nil
Stores the list of plugins to be used by the server.
-
#options_parser ⇒ OptionParser
Create the options parser.
-
#run ⇒ Nil
Create environment to run the server.
Constructor Details
#initialize(argv, api) ⇒ Goliath::Runner
Create a new Goliath::Runner
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/goliath/runner.rb', line 105 def initialize(argv, api) api.(, ) if api .parse!(argv) # We've already dealt with the environment, so just discard it. .delete(:env) @api = api @address = .delete(:address) @port = .delete(:port) @log_file = .delete(:log_file) @pid_file = .delete(:pid_file) @log_stdout = .delete(:log_stdout) @daemonize = .delete(:daemonize) @verbose = .delete(:verbose) @server_options = end |
Instance Attribute Details
#address ⇒ String
The address of the server @example 127.0.0.1
51 52 53 |
# File 'lib/goliath/runner.rb', line 51 def address @address end |
#api ⇒ Object
The API application
83 84 85 |
# File 'lib/goliath/runner.rb', line 83 def api @api end |
#app ⇒ Object
The Rack application
79 80 81 |
# File 'lib/goliath/runner.rb', line 79 def app @app end |
#app_options ⇒ Hash
Any additional server options
94 95 96 |
# File 'lib/goliath/runner.rb', line 94 def @app_options end |
#daemonize ⇒ Boolean
Flag to determine if the server should daemonize
59 60 61 |
# File 'lib/goliath/runner.rb', line 59 def daemonize @daemonize end |
#log_file ⇒ String
The log file for the server
71 72 73 |
# File 'lib/goliath/runner.rb', line 71 def log_file @log_file end |
#log_stdout ⇒ Boolean
Flag to determine if the server should log to standard output
67 68 69 |
# File 'lib/goliath/runner.rb', line 67 def log_stdout @log_stdout end |
#logger ⇒ Object
Allow to inject a custom logger
90 91 92 |
# File 'lib/goliath/runner.rb', line 90 def logger @logger end |
#options ⇒ Hash (readonly)
The parsed options
98 99 100 |
# File 'lib/goliath/runner.rb', line 98 def @options end |
#pid_file ⇒ String
The pid file for the server
75 76 77 |
# File 'lib/goliath/runner.rb', line 75 def pid_file @pid_file end |
#plugins ⇒ Array
The plugins the server will execute
87 88 89 |
# File 'lib/goliath/runner.rb', line 87 def plugins @plugins end |
#port ⇒ Integer
The port of the server @example 9000
55 56 57 |
# File 'lib/goliath/runner.rb', line 55 def port @port end |
#verbose ⇒ Boolean
Flag to determine if the server should run in verbose mode
63 64 65 |
# File 'lib/goliath/runner.rb', line 63 def verbose @verbose end |
Instance Method Details
#load_plugins(plugins) ⇒ Nil
Stores the list of plugins to be used by the server
185 186 187 |
# File 'lib/goliath/runner.rb', line 185 def load_plugins(plugins) @plugins = plugins end |
#options_parser ⇒ OptionParser
Create the options parser
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/goliath/runner.rb', line 129 def @options ||= { :address => Goliath::Server::DEFAULT_ADDRESS, :port => Goliath::Server::DEFAULT_PORT, :daemonize => false, :verbose => false, :log_stdout => false, :env => Goliath::DEFAULT_ENV } @options_parser ||= OptionParser.new do |opts| opts. = "Usage: <server> [options]" opts.separator "" opts.separator "Server options:" # The environment isn't set as part of this option parsing routine, but # we'll leave the flag here so a call to --help shows it correctly. opts.on('-e', '--environment NAME', "Set the execution environment (default: #{@options[:env]})") { |val| @options[:env] = val } opts.on('-a', '--address HOST', "Bind to HOST address (default: #{@options[:address]})") { |addr| @options[:address] = addr } opts.on('-p', '--port PORT', "Use PORT (default: #{@options[:port]})") { |port| @options[:port] = port.to_i } opts.on('-S', '--socket FILE', "Bind to unix domain socket") { |v| @options[:address] = v; @options[:port] = nil } opts.on('-E', '--einhorn', "Use Einhorn socket manager") { |v| @options[:einhorn] = true } opts.separator "" opts.separator "Daemon options:" opts.on('-u', '--user USER', "Run as specified user") {|v| @options[:user] = v } opts.on('-c', '--config FILE', "Config file (default: ./config/<server>.rb)") { |v| @options[:config] = v } opts.on('-d', '--daemonize', "Run daemonized in the background (default: #{@options[:daemonize]})") { |v| @options[:daemonize] = v } opts.on('-l', '--log FILE', "Log to file (default: off)") { |file| @options[:log_file] = file } opts.on('-s', '--stdout', "Log to stdout (default: #{@options[:log_stdout]})") { |v| @options[:log_stdout] = v } opts.on('-P', '--pid FILE', "Pid file (default: off)") { |file| @options[:pid_file] = file } opts.separator "" opts.separator "SSL options:" opts.on('--ssl', 'Enables SSL (default: off)') {|v| @options[:ssl] = v } opts.on('--ssl-key FILE', 'Path to private key') {|v| @options[:ssl_key] = v } opts.on('--ssl-cert FILE', 'Path to certificate') {|v| @options[:ssl_cert] = v } opts.on('--ssl-verify', 'Enables SSL certificate verification') {|v| @options[:ssl_verify] = v } opts.separator "" opts.separator "Common options:" opts.on('-C', '--console', 'Start a console') { @options[:console] = true } opts.on('-v', '--verbose', "Enable verbose logging (default: #{@options[:verbose]})") { |v| @options[:verbose] = v } opts.on('-h', '--help', 'Display help message') { (opts) } end end |
#run ⇒ Nil
Create environment to run the server. If daemonize is set this will fork off a child and kill the runner.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/goliath/runner.rb', line 193 def run if [:console] Goliath::Console.run!(setup_server) return end unless Goliath.env?(:test) $LOADED_FEATURES.unshift(File.basename($0)) Dir.chdir(File.(File.dirname($0))) end if @daemonize Process.fork do Process.setsid exit if fork @pid_file ||= './goliath.pid' @log_file ||= File.('goliath.log') store_pid(Process.pid) File.umask(0000) log_extension = File.extname(@log_file) stdout_log_file = "#{File.dirname(@log_file)}/#{File.basename(@log_file, log_extension)}_stdout#{log_extension}" STDIN.reopen("/dev/null") STDOUT.reopen(stdout_log_file, "a") STDERR.reopen(STDOUT) run_server remove_pid end else run_server end end |