diff --git a/lib/scout_apm/logging/monitor/monitor.rb b/lib/scout_apm/logging/monitor/monitor.rb index 532d4e0..5f620dd 100644 --- a/lib/scout_apm/logging/monitor/monitor.rb +++ b/lib/scout_apm/logging/monitor/monitor.rb @@ -8,11 +8,21 @@ module ScoutApm module Logging class Monitor def run + add_exit_handler + loop do sleep 1 puts "Running..." end end + + private + + def add_exit_handler + at_exit do + File.delete(ScoutApm::Logging::MonitorManager::PID_FILE) + end + end end end end diff --git a/lib/scout_apm/logging/monitor_manager.rb b/lib/scout_apm/logging/monitor_manager.rb index 57490a5..8006769 100644 --- a/lib/scout_apm/logging/monitor_manager.rb +++ b/lib/scout_apm/logging/monitor_manager.rb @@ -12,6 +12,8 @@ def self.setup! def self.create_daemon return if File.exist? PID_FILE + # TODO: Investigate behavior of orphaned processes and copy on write behavior. + # Do we get all of the parent's memory? We may want to spawn the daemon. child_process = Process.fork do ScoutApm::Logging::Monitor.new.run end diff --git a/spec/scout_apm_logging_spec.rb b/spec/scout_apm_logging_spec.rb index dabd948..8cd2506 100644 --- a/spec/scout_apm_logging_spec.rb +++ b/spec/scout_apm_logging_spec.rb @@ -10,7 +10,7 @@ make_basic_app end - it "spawns the daemon process" do + it "checks lifecycle of the daemon process" do # Check if the PID file exists expect(File.exist?(ScoutApm::Logging::MonitorManager::PID_FILE)).to be_truthy @@ -19,11 +19,10 @@ # Check if the process with the stored PID is running expect(Process.kill(0, pid)).to be_truthy - end - after(:all) do - process = File.read(ScoutApm::Logging::MonitorManager::PID_FILE).to_i - Process.kill('TERM', process) - File.delete(ScoutApm::Logging::MonitorManager::PID_FILE) + # Kill the process and ensure PID file clean up + Process.kill('TERM', pid) + sleep 1 # Give the process time to exit + expect(File.exist?(ScoutApm::Logging::MonitorManager::PID_FILE)).to be_falsey end end