Class: GrafanaReporter::Application::Webservice

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana_reporter/application/webservice.rb

Overview

This class provides the webservice for the reporter application. It does not make use of ‘webrick` or similar, so that it can be used without futher dependencies in conjunction with the standard asciidoctor docker container.

Constant Summary collapse

STATUS =

Array of possible webservice running states

%I[stopped running stopping].freeze

Instance Method Summary collapse

Constructor Details

#initializeWebservice

Returns a new instance of Webservice.



12
13
14
15
# File 'lib/grafana_reporter/application/webservice.rb', line 12

def initialize
  @reports = []
  @status = :stopped
end

Instance Method Details

#run(config) ⇒ Object

Runs the webservice with the given Configuration object.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grafana_reporter/application/webservice.rb', line 18

def run(config)
  @config = config
  @logger = config.logger

  # start webserver
  @server = TCPServer.new(@config.webserver_port)
  @logger.info("Server listening on port #{@config.webserver_port}...")

  @progress_reporter = Thread.new {}

  @status = :running
  begin
    accept_requests_loop
  rescue SystemExit, Interrupt
    @logger.info("Server shutting down.")
    stop!
    retry
  end
  @status = :stopped
end

#running?Boolean

Returns True, if webservice is up and running, false otherwise.

Returns:

  • (Boolean)

    True, if webservice is up and running, false otherwise



45
46
47
# File 'lib/grafana_reporter/application/webservice.rb', line 45

def running?
  @status == :running
end

#stop!Object

Forces stopping the webservice.



50
51
52
53
54
55
56
57
# File 'lib/grafana_reporter/application/webservice.rb', line 50

def stop!
  @status = :stopping

  # invoke a new request, so that the webservice stops.
  socket = TCPSocket.new('localhost', @config.webserver_port)
  socket.send '', 0
  socket.close
end

#stopped?Boolean

Returns True, if webservice is stopped, false otherwise.

Returns:

  • (Boolean)

    True, if webservice is stopped, false otherwise



40
41
42
# File 'lib/grafana_reporter/application/webservice.rb', line 40

def stopped?
  @status == :stopped
end