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

#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



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

#addressString

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

#apiObject

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

#appObject

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

#app_optionsHash

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

#daemonizeBoolean

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

#log_fileString

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

#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



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

def log_stdout
  @log_stdout
end

#optionsHash (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

#pid_fileString

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

#pluginsArray

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

#portInteger

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

#verboseBoolean

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

#load_app(&blk) ⇒ Object

Given a block, this will use Rack::Builder to create the application

Parameters:

  • blk (Block)

    The application block to load

Returns:

  • (Object)

    The Rack 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

Parameters:

  • plugins (Array)

    The list of plugins to use

Returns:

  • (Nil)


128
129
130
# File 'lib/goliath/runner.rb', line 128

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



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_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.banner = "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') { 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)


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.expand_path('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