Skip to content

Commit f46906e

Browse files
authored
FIX: Fork collector demon in a safer way. (#126)
Previously we were using the following pattern to fork a child process ``` if @pid = fork write_pid_file return end <Do something in child process> ``` However, we noticed that if any error occurs in the child process (ie. after the `if @pid = fork` line), the error propogates to the parent process as well causing it to crash. Instead, we can pass `fork` a block instead and that does not cause the error to propogate to the parent process.
1 parent 1008621 commit f46906e

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

‎lib/collector_demon.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ def self.prefix
88
end
99

1010
def run
11-
if @pid = fork
12-
write_pid_file
13-
return
14-
end
11+
@pid =
12+
fork do
13+
collector = File.expand_path("../../bin/collector", __FILE__)
1514

16-
collector = File.expand_path("../../bin/collector", __FILE__)
15+
ENV["RUBY_GLOBAL_METHOD_CACHE_SIZE"] = "2048"
16+
ENV["RUBY_GC_HEAP_INIT_SLOTS"] = "10000"
17+
ENV["PROMETHEUS_EXPORTER_VERSION"] = PrometheusExporter::VERSION
1718

18-
ENV["RUBY_GLOBAL_METHOD_CACHE_SIZE"] = "2048"
19-
ENV["RUBY_GC_HEAP_INIT_SLOTS"] = "10000"
20-
ENV["PROMETHEUS_EXPORTER_VERSION"] = PrometheusExporter::VERSION
19+
exec collector,
20+
GlobalSetting.prometheus_collector_port.to_s,
21+
GlobalSetting.prometheus_webserver_bind,
22+
parent_pid.to_s,
23+
pid_file
24+
end
2125

22-
exec collector,
23-
GlobalSetting.prometheus_collector_port.to_s,
24-
GlobalSetting.prometheus_webserver_bind,
25-
parent_pid.to_s,
26-
pid_file
26+
Process.detach(@pid)
27+
28+
write_pid_file
2729
end
2830
end

0 commit comments

Comments
 (0)