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
-
#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.
-
#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_app(&blk) ⇒ Object
Given a block, this will use Rack::Builder to create the application.
-
#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
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
#address ⇒ String
The address of the server @example 127.0.0.1
12 13 14 |
# File 'lib/goliath/runner.rb', line 12 def address @address end |
#api ⇒ Object
The API application
44 45 46 |
# File 'lib/goliath/runner.rb', line 44 def api @api end |
#app ⇒ Object
The Rack application
40 41 42 |
# File 'lib/goliath/runner.rb', line 40 def app @app end |
#app_options ⇒ Hash
Any additional server options
52 53 54 |
# File 'lib/goliath/runner.rb', line 52 def @app_options end |
#daemonize ⇒ Boolean
Flag to determine if the server should daemonize
20 21 22 |
# File 'lib/goliath/runner.rb', line 20 def daemonize @daemonize end |
#log_file ⇒ String
The log file for the server
32 33 34 |
# File 'lib/goliath/runner.rb', line 32 def log_file @log_file end |
#log_stdout ⇒ Boolean
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 |
#options ⇒ Hash (readonly)
The parsed options
56 57 58 |
# File 'lib/goliath/runner.rb', line 56 def @options end |
#pid_file ⇒ String
The pid file for the server
36 37 38 |
# File 'lib/goliath/runner.rb', line 36 def pid_file @pid_file end |
#plugins ⇒ Array
The plugins the server will execute
48 49 50 |
# File 'lib/goliath/runner.rb', line 48 def plugins @plugins end |
#port ⇒ Integer
The port of the server @example 9000
16 17 18 |
# File 'lib/goliath/runner.rb', line 16 def port @port end |
#verbose ⇒ Boolean
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
#load_app(&blk) ⇒ Object
Given a block, this will use Rack::Builder to create the application
120 121 122 |
# File 'lib/goliath/runner.rb', line 120 def load_app(&blk) @app = ::Rack::Builder.app(&blk) end |
#load_plugins(plugins) ⇒ Nil
Stores the list of plugins to be used by the server
128 129 130 |
# File 'lib/goliath/runner.rb', line 128 def load_plugins(plugins) @plugins = plugins end |
#options_parser ⇒ OptionParser
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 |
# 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('-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.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 |
#run ⇒ Nil
Create environment to run the server. If daemonize is set this will fork off a child and kill the runner.
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 |
# File 'lib/goliath/runner.rb', line 136 def run if @daemonize Process.fork do Process.setsid exit if fork @pid_file ||= './goliath.pid' @log_file ||= File.('goliath.log') store_pid(Process.pid) Dir.chdir(File.dirname(__FILE__)) 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 |