Class: Archfiend::SubprocessLoop

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Archfiend::SharedLoop::Runnable
Defined in:
lib/archfiend/subprocess_loop.rb

Direct Known Subclasses

FooSubprocessLoop

Constant Summary collapse

EXCEPTION_DELAY =

Seconds to sleep for after rescuing recoverable exception

1

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Archfiend::SharedLoop::Runnable

#log_exception, #run

Class Attribute Details

.subclassesObject (readonly)

Returns the value of attribute subclasses.



13
14
15
# File 'lib/archfiend/subprocess_loop.rb', line 13

def subclasses
  @subclasses
end

.subprocess_pidsObject (readonly)

Returns the value of attribute subprocess_pids.



13
14
15
# File 'lib/archfiend/subprocess_loop.rb', line 13

def subprocess_pids
  @subprocess_pids
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



10
11
12
# File 'lib/archfiend/subprocess_loop.rb', line 10

def app
  @app
end

Class Method Details

.inherited(child_class) ⇒ Object



15
16
17
18
19
# File 'lib/archfiend/subprocess_loop.rb', line 15

def inherited(child_class)
  @subclasses ||= []
  @subclasses.push(child_class)
  @subprocess_pids ||= [] # rubocop:disable Naming/MemoizedInstanceVariableName
end

.kill_allObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/archfiend/subprocess_loop.rb', line 34

def kill_all
  return if !@subprocess_pids || @subprocess_pids.empty?

  @subprocess_pids.each do |spid|
    begin
      Process.kill('TERM', spid)
    rescue Errno::ESRCH, Errno::EPERM # rubocop:disable Lint/HandleExceptions
      # The list might contain some stale pids, if any of subprocesses terminated early
    end
  end
end

.start_all(app) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/archfiend/subprocess_loop.rb', line 21

def start_all(app)
  (subclasses || []).each do |subclass|
    process_id = fork do
      instance = subclass.new
      instance.app = app
      app.logger.info "Starting subprocess #{subclass}"
      instance.run
    end
    @subprocess_pids << process_id
    Process.detach(process_id)
  end
end