Skip to content

Commit

Permalink
Have daemon cleanup PID file at exit
Browse files Browse the repository at this point in the history
  • Loading branch information
jrothrock committed Apr 26, 2024
1 parent d3bd8fd commit 87fb72b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/scout_apm/logging/monitor/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/scout_apm/logging/monitor_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions spec/scout_apm_logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 87fb72b

Please sign in to comment.