prometheus_exporter icon indicating copy to clipboard operation
prometheus_exporter copied to clipboard

Not possible to fork PrometheusExporter::Server::WebServer.new

Open restebanez opened this issue 4 years ago • 1 comments

I've been trying to run PrometheusExporter::Server::WebServer.new as a child process but it exists immediately:

require 'prometheus_exporter'
require 'prometheus_exporter/server'

pid = fork do
  server = PrometheusExporter::Server::WebServer.new({port: 9394, verbose: true})
  server.start
end
Process.wait2

Webrick allows to be forked no problem:

require 'webrick'
pid = fork do
  server = WEBrick::HTTPServer.new({:Port =>9394})
  server.start
end
Process.wait2

A workaround to this, as documented here (https://github.com/discourse/prometheus_exporter#multi-process-mode) is to use the binary version:

fork do
  Kernel.exec('bundle exec prometheus_exporter --prefix ""') # exec replaces the current process with the shell execution
end

However that looks like a fully external process.

To make it work I just have to modify the start method https://github.com/discourse/prometheus_exporter/blob/5ba1d48620b8dc58b7428f8f8905ec7febdde9d5/lib/prometheus_exporter/server/web_server.rb#L106-L114 with

  def start
     @server.start
  end

I propose to add the above method with the name foreground_start so we can have both versions, the foreground (regular start) and the background (thread) one.

  def foreground_start
     @server.start
  end

restebanez avatar Feb 01 '21 18:02 restebanez