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, settting 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

- (Goliath::Runner) initialize(argv, api)

Create a new Goliath::Runner

Parameters:

  • (Array) argv

    The command line arguments

  • (Object | nil) api

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



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

  @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

- (String) address

The address of the server @example 127.0.0.1

Returns:

  • (String)

    The server address



12
13
14
# File 'lib/goliath/runner.rb', line 12

def address
  @address
end

- (Object) api

The API application

Returns:

  • (Object)

    The API application the server will execute



44
45
46
# File 'lib/goliath/runner.rb', line 44

def api
  @api
end

- (Object) app

The Rack application

Returns:

  • (Object)

    The rack application the server will execute



40
41
42
# File 'lib/goliath/runner.rb', line 40

def app
  @app
end

- (Hash) app_options

Any additional server options

Returns:

  • (Hash)

    Any options to be passed to the server



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

def app_options
  @app_options
end

- (Boolean) daemonize

Flag to determine if the server should daemonize

Returns:

  • (Boolean)

    True if the server should daemonize, false otherwise



20
21
22
# File 'lib/goliath/runner.rb', line 20

def daemonize
  @daemonize
end

- (String) log_file

The log file for the server

Returns:

  • (String)

    The file the server should log too



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

Returns:

  • (Boolean)

    True if the server should log to stdout, false otherwise



28
29
30
# File 'lib/goliath/runner.rb', line 28

def log_stdout
  @log_stdout
end

- (Hash) options (readonly)

The parsed options

Returns:

  • (Hash)

    The options parsed by the runner



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

def options
  @options
end

- (String) pid_file

The pid file for the server

Returns:

  • (String)

    The file to write the servers pid file into



36
37
38
# File 'lib/goliath/runner.rb', line 36

def pid_file
  @pid_file
end

- (Array) plugins

The plugins the server will execute

Returns:

  • (Array)

    The list of plugins to be executed by the server



48
49
50
# File 'lib/goliath/runner.rb', line 48

def plugins
  @plugins
end

- (Integer) port

The port of the server @example 9000

Returns:

  • (Integer)

    The server port



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

Returns:

  • (Boolean)

    True to turn on verbose mode, false otherwise



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

Parameters:

  • (Array) plugins

    The list of plugins to use

Returns:

  • (Nil)


122
123
124
# File 'lib/goliath/runner.rb', line 122

def load_plugins(plugins)
  @plugins = plugins
end

- (OptionParser) options_parser

Create the options parser

Returns:

  • (OptionParser)

    Creates the options parser for the runner with the default options



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_parser
  @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') { show_options(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.

Returns:

  • (Nil)


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.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)

      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