Module: Ace::Core::Atoms::ProcessTerminator

Defined in:
lib/ace/core/atoms/process_terminator.rb

Overview

Pure functions for process termination with graceful fallback to force kill

Class Method Summary collapse

Class Method Details

.terminate(pid, grace_period: 0.1) ⇒ Boolean

Terminate a process gracefully, then forcefully if needed

Sends SIGTERM first to allow graceful shutdown, then SIGKILL if process still exists after a brief wait. Silently handles cases where the process has already terminated or is inaccessible.

Parameters:

  • pid (Integer, nil)

    Process ID to terminate

  • grace_period (Float) (defaults to: 0.1)

    Seconds to wait between TERM and KILL (default: 0.1)

Returns:

  • (Boolean)

    true if termination was attempted, false if pid was nil



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ace/core/atoms/process_terminator.rb', line 19

def terminate(pid, grace_period: 0.1)
  return false unless pid

  begin
    # Try graceful termination first (SIGTERM)
    Process.kill("TERM", pid)
    # Give it a moment to terminate
    sleep(grace_period)
    # Check if still running and force kill if needed
    Process.kill(0, pid) # Check if process exists
    Process.kill("KILL", pid)
  rescue Errno::ESRCH, Errno::EPERM
    # Process already terminated or we don't have permission - that's fine
  end

  true
end