Class: LogStash::WebServer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/logstash/webserver.rb

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1".freeze
DEFAULT_PORTS =
(9600..9700).freeze
DEFAULT_ENVIRONMENT =
'production'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, agent, options = {}) ⇒ WebServer

Returns a new instance of WebServer.



21
22
23
24
25
26
27
28
29
30
# File 'lib/logstash/webserver.rb', line 21

def initialize(logger, agent, options={})
  @logger = logger
  @agent = agent
  @http_host = options[:http_host] || DEFAULT_HOST
  @http_ports = options[:http_ports] || DEFAULT_PORTS
  @http_environment = options[:http_environment] || DEFAULT_ENVIRONMENT
  @options = {}
  @status = nil
  @running = Concurrent::AtomicBoolean.new(false)
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def agent
  @agent
end

#binderObject (readonly)

Returns the value of attribute binder.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def binder
  @binder
end

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def config
  @config
end

#eventsObject (readonly)

Returns the value of attribute events.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def events
  @events
end

#http_environmentObject (readonly)

Returns the value of attribute http_environment.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def http_environment
  @http_environment
end

#http_hostObject (readonly)

Returns the value of attribute http_host.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def http_host
  @http_host
end

#http_portsObject (readonly)

Returns the value of attribute http_ports.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def http_ports
  @http_ports
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def options
  @options
end

#runnerObject (readonly)

Returns the value of attribute runner.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def runner
  @runner
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/logstash/webserver.rb', line 13

def status
  @status
end

Instance Method Details

#addressObject



66
67
68
# File 'lib/logstash/webserver.rb', line 66

def address
  "#{http_host}:#{@port}"
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logstash/webserver.rb', line 32

def run
  logger.debug("Starting puma")

  stop # Just in case

  running!

  http_ports.each_with_index do |port, idx|
    begin
      if running?
        @port = port
        logger.debug("Trying to start WebServer", :port => @port)
        start_webserver(@port)
      else
        break # we are closing down the server so just get out of the loop
      end
    rescue Errno::EADDRINUSE
      if http_ports.count == 1
        raise Errno::EADDRINUSE.new(I18n.t("logstash.web_api.cant_bind_to_port", :port => http_ports.first))
      elsif idx == http_ports.count-1
        raise Errno::EADDRINUSE.new(I18n.t("logstash.web_api.cant_bind_to_port_in_range", :http_ports => http_ports))
      end
    end
  end
end

#running!Object



58
59
60
# File 'lib/logstash/webserver.rb', line 58

def running!
  @running.make_true
end

#running?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/logstash/webserver.rb', line 62

def running?
  @running.value
end

#start_webserver(port) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/logstash/webserver.rb', line 75

def start_webserver(port)
  # wrap any output that puma could generate into a wrapped logger
  # use the puma namespace to override STDERR, STDOUT in that scope.
  Puma::STDERR.logger = logger
  Puma::STDOUT.logger = logger

  io_wrapped_logger = LogStash::IOWrappedLogger.new(logger)

  app = LogStash::Api::RackApp.app(logger, agent, http_environment)

  events = ::Puma::Events.new(io_wrapped_logger, io_wrapped_logger)

  @server = ::Puma::Server.new(app, events)
  @server.add_tcp_listener(http_host, port)

  logger.info("Successfully started Logstash API endpoint", :port => @port)

  @server.run.join
end

#stop(options = {}) ⇒ Object



70
71
72
73
# File 'lib/logstash/webserver.rb', line 70

def stop(options={})
  @running.make_false
  @server.stop(true) if @server
end