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, settting up the rack application, creating a logger, and then executing the Goliath::Server with the loaded information.
Instance Attribute Summary (collapse)
-
- (String) address
The address of the server @example 127.0.0.1.
-
- (Object) api
The API application.
-
- (Object) app
The Rack application.
-
- (Hash) app_options
Any additional server options.
-
- (Boolean) daemonize
Flag to determine if the server should daemonize.
-
- (String) log_file
The log file for the server.
-
- (Boolean) log_stdout
Flag to determine if the server should log to standard output.
-
- (Hash) options
readonly
The parsed options.
-
- (String) pid_file
The pid file for the server.
-
- (Array) plugins
The plugins the server will execute.
-
- (Integer) port
The port of the server @example 9000.
-
- (Boolean) verbose
Flag to determine if the server should run in verbose mode.
Instance Method Summary (collapse)
-
- (Goliath::Runner) initialize(argv, api)
constructor
Create a new Goliath::Runner.
-
- (Nil) load_plugins(plugins)
Stores the list of plugins to be used by the server.
-
- (OptionParser) options_parser
Create the options parser.
-
- (Nil) run
Create environment to run the server.
Constructor Details
- (Goliath::Runner) initialize(argv, api)
Create a new Goliath::Runner
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/goliath/runner.rb', line 63 def initialize(argv, api) api.(, ) if api .parse!(argv) @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
- (String) address
The address of the server @example 127.0.0.1
12 13 14 |
# File 'lib/goliath/runner.rb', line 12 def address @address end |
- (Object) api
The API application
44 45 46 |
# File 'lib/goliath/runner.rb', line 44 def api @api end |
- (Object) app
The Rack application
40 41 42 |
# File 'lib/goliath/runner.rb', line 40 def app @app end |
- (Hash) app_options
Any additional server options
52 53 54 |
# File 'lib/goliath/runner.rb', line 52 def @app_options end |
- (Boolean) daemonize
Flag to determine if the server should daemonize
20 21 22 |
# File 'lib/goliath/runner.rb', line 20 def daemonize @daemonize end |
- (String) log_file
The log file for the server
32 33 34 |
# File 'lib/goliath/runner.rb', line 32 def log_file @log_file end |
- (Boolean) log_stdout
Flag to determine if the server should log to standard output
28 29 30 |
# File 'lib/goliath/runner.rb', line 28 def log_stdout @log_stdout end |
- (Hash) options (readonly)
The parsed options
56 57 58 |
# File 'lib/goliath/runner.rb', line 56 def @options end |
- (String) pid_file
The pid file for the server
36 37 38 |
# File 'lib/goliath/runner.rb', line 36 def pid_file @pid_file end |
- (Array) plugins
The plugins the server will execute
48 49 50 |
# File 'lib/goliath/runner.rb', line 48 def plugins @plugins end |
- (Integer) port
The port of the server @example 9000
16 17 18 |
# File 'lib/goliath/runner.rb', line 16 def port @port end |
- (Boolean) verbose
Flag to determine if the server should run in verbose mode
24 25 26 |
# File 'lib/goliath/runner.rb', line 24 def verbose @verbose end |
Instance Method Details
- (Nil) load_plugins(plugins)
Stores the list of plugins to be used by the server
122 123 124 |
# File 'lib/goliath/runner.rb', line 122 def load_plugins(plugins) @plugins = plugins end |
- (OptionParser) options_parser
Create the options parser
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 |
# File 'lib/goliath/runner.rb', line 84 def @options ||= { :address => Goliath::Server::DEFAULT_ADDRESS, :port => Goliath::Server::DEFAULT_PORT, :daemonize => false, :verbose => false, :log_stdout => false } @options_parser ||= OptionParser.new do |opts| opts. = "Usage: <server> [options]" opts.separator "" opts.separator "Server options:" opts.on('-e', '--environment NAME', "Set the execution environment (prod, dev or test) (default: #{Goliath.env})") { |val| Goliath.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('-u', '--user USER', "Run as specified user") {|v| @options[:user] = 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('-c', '--config FILE', "Config file (default: ./config/<server>.rb)") { |v| @options[:config] = v } opts.on('-P', '--pid FILE', "Pid file (default: off)") { |file| @options[:pid_file] = file } opts.on('-d', '--daemonize', "Run daemonized in the background (default: #{@options[:daemonize]})") { |v| @options[:daemonize] = v } opts.on('-v', '--verbose', "Enable verbose logging (default: #{@options[:verbose]})") { |v| @options[:verbose] = v } opts.on('-h', '--help', 'Display help message') { (opts) } end end |
- (Nil) run
Create environment to run the server. If daemonize is set this will fork off a child and kill the runner.
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 |
# File 'lib/goliath/runner.rb', line 130 def run unless Goliath.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) stdout_log_file = "#{File.dirname(@log_file)}/#{File.basename(@log_file)}_stdout.log" STDIN.reopen("/dev/null") STDOUT.reopen(stdout_log_file, "a") STDERR.reopen(STDOUT) run_server end else run_server end end |