Class: Goliath::Runner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(argv, api) ⇒ Goliath::Runner

Create a new Goliath::Runner

Parameters:

  • argv (Array)

    The command line arguments

  • api (Object | nil)

    The Goliath::API this runner is for, can be nil



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.options_parser(options_parser, options) if api
  options_parser.parse!(argv)

  # We've already dealt with the environment, so just discard it.
  options.delete(:env)

  @api = api
  @address = options.delete(:address)
  @port = options.delete(:port)

  @log_file = options.delete(:log_file)
  @pid_file = options.delete(:pid_file)

  @log_stdout = options.delete(:log_stdout)
  @daemonize = options.delete(:daemonize)
  @verbose = options.delete(:verbose)

  @server_options = options
end

Instance Attribute Details

#addressString

The address of the server @example 127.0.0.1

Returns:

  • (String)

    The server address



51
52
53
# File 'lib/goliath/runner.rb', line 51

def address
  @address
end

#apiObject

The API application

Returns:

  • (Object)

    The API application the server will execute



83
84
85
# File 'lib/goliath/runner.rb', line 83

def api
  @api
end

#appObject

The Rack application

Returns:

  • (Object)

    The rack application the server will execute



79
80
81
# File 'lib/goliath/runner.rb', line 79

def app
  @app
end

#app_optionsHash

Any additional server options

Returns:

  • (Hash)

    Any options to be passed to the server



94
95
96
# File 'lib/goliath/runner.rb', line 94

def app_options
  @app_options
end

#daemonizeBoolean

Flag to determine if the server should daemonize

Returns:

  • (Boolean)

    True if the server should daemonize, false otherwise



59
60
61
# File 'lib/goliath/runner.rb', line 59

def daemonize
  @daemonize
end

#log_fileString

The log file for the server

Returns:

  • (String)

    The file the server should log too



71
72
73
# File 'lib/goliath/runner.rb', line 71

def log_file
  @log_file
end

#log_stdoutBoolean

Flag to determine if the server should log to standard output

Returns:

  • (Boolean)

    True if the server should log to stdout, false otherwise



67
68
69
# File 'lib/goliath/runner.rb', line 67

def log_stdout
  @log_stdout
end

#loggerObject

Allow to inject a custom logger



90
91
92
# File 'lib/goliath/runner.rb', line 90

def logger
  @logger
end

#optionsHash (readonly)

The parsed options

Returns:

  • (Hash)

    The options parsed by the runner



98
99
100
# File 'lib/goliath/runner.rb', line 98

def options
  @options
end

#pid_fileString

The pid file for the server

Returns:

  • (String)

    The file to write the servers pid file into



75
76
77
# File 'lib/goliath/runner.rb', line 75

def pid_file
  @pid_file
end

#pluginsArray

The plugins the server will execute

Returns:

  • (Array)

    The list of plugins to be executed by the server



87
88
89
# File 'lib/goliath/runner.rb', line 87

def plugins
  @plugins
end

#portInteger

The port of the server @example 9000

Returns:

  • (Integer)

    The server port



55
56
57
# File 'lib/goliath/runner.rb', line 55

def port
  @port
end

#verboseBoolean

Flag to determine if the server should run in verbose mode

Returns:

  • (Boolean)

    True to turn on verbose mode, false otherwise



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

Parameters:

  • plugins (Array)

    The list of plugins to use

Returns:

  • (Nil)


185
186
187
# File 'lib/goliath/runner.rb', line 185

def load_plugins(plugins)
  @plugins = plugins
end

#options_parserOptionParser

Create the options parser

Returns:

  • (OptionParser)

    Creates the options parser for the runner with the default options



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_parser
  @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.banner = "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') { show_options(opts) }
  end
end

#runNil

Create environment to run the server. If daemonize is set this will fork off a child and kill the runner.

Returns:

  • (Nil)


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 options[:console]
    Goliath::Console.run!(setup_server)
    return
  end

  unless Goliath.env?(:test)
    $LOADED_FEATURES.unshift(File.basename($0))
    Dir.chdir(File.expand_path(File.dirname($0)))
  end

  if @daemonize
    Process.fork do
      Process.setsid
      exit if fork

      @pid_file ||= './goliath.pid'
      @log_file ||= File.expand_path('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