Class: FiberAudit::Runtime::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/runtime/supervisor.rb

Defined Under Namespace

Classes: SystemAdapter

Constant Summary collapse

SIGNALS =
%w[INT TERM HUP QUIT].select { |name| Signal.list.key?(name) }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(command:, environment:, cwd:, adapter: SystemAdapter.new) ⇒ Supervisor

Returns a new instance of Supervisor.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fiber_audit/runtime/supervisor.rb', line 35

def initialize(command:, environment:, cwd:, adapter: SystemAdapter.new)
  validate_command!(command)
  raise RuntimeContractError, 'child environment must be a Hash' unless environment.is_a?(Hash)
  raise RuntimeContractError, 'runtime cwd must be an existing directory' unless File.directory?(cwd)

  @command = command.map { |argument| argument.dup.freeze }.freeze
  @environment = environment.transform_values { |value| value&.dup&.freeze }.freeze
  @cwd = File.expand_path(cwd).freeze
  @adapter = adapter
  @child_pid = nil
end

Instance Method Details

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fiber_audit/runtime/supervisor.rb', line 47

def run
  handlers = {}
  begin
    # Install traps only after spawn so POSIX-spawned children cannot inherit
    # FiberAudit's forwarding handlers and accidentally ignore termination.
    @child_pid = @adapter.spawn(@environment, @command, @cwd)
    install_handlers(handlers)
    _pid, status = wait_for_child
    status_code(status)
  ensure
    restore_handlers(handlers)
    @child_pid = nil
  end
end