Class: LogStash::WebServer
- Inherits:
-
Object
- Object
- LogStash::WebServer
- 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
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#binder ⇒ Object
readonly
Returns the value of attribute binder.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#http_environment ⇒ Object
readonly
Returns the value of attribute http_environment.
-
#http_host ⇒ Object
readonly
Returns the value of attribute http_host.
-
#http_ports ⇒ Object
readonly
Returns the value of attribute http_ports.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#runner ⇒ Object
readonly
Returns the value of attribute runner.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #address ⇒ Object
-
#initialize(logger, agent, options = {}) ⇒ WebServer
constructor
A new instance of WebServer.
- #run ⇒ Object
- #running! ⇒ Object
- #running? ⇒ Boolean
- #start_webserver(port) ⇒ Object
- #stop(options = {}) ⇒ Object
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, ={}) @logger = logger @agent = agent @http_host = [:http_host] || DEFAULT_HOST @http_ports = [:http_ports] || DEFAULT_PORTS @http_environment = [:http_environment] || DEFAULT_ENVIRONMENT @options = {} @status = nil @running = Concurrent::AtomicBoolean.new(false) end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def agent @agent end |
#binder ⇒ Object (readonly)
Returns the value of attribute binder.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def binder @binder end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def config @config end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def events @events end |
#http_environment ⇒ Object (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_host ⇒ Object (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_ports ⇒ Object (readonly)
Returns the value of attribute http_ports.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def http_ports @http_ports end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def @options end |
#runner ⇒ Object (readonly)
Returns the value of attribute runner.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def runner @runner end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
13 14 15 |
# File 'lib/logstash/webserver.rb', line 13 def status @status end |
Instance Method Details
#address ⇒ Object
66 67 68 |
# File 'lib/logstash/webserver.rb', line 66 def address "#{http_host}:#{@port}" end |
#run ⇒ Object
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
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(={}) @running.make_false @server.stop(true) if @server end |