Class: PerfectQueue::Multiprocess::ForkProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/perfectqueue/multiprocess/fork_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(runner, config) ⇒ ForkProcessor



23
24
25
26
27
28
29
30
31
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 23

def initialize(runner, config)
  @runner = runner

  require 'fcntl'
  @stop = false
  @cpm = nil

  restart(false, config)
end

Instance Method Details

#joinObject



91
92
93
94
95
96
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 91

def join
  while !try_join
    sleep (@child_kill_interval+1) / 2  # TODO
  end
  self
end

#keepaliveObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 53

def keepalive
  if @stop
    try_join
    return
  end

  if c = @cpm
    begin
      # receive heartbeat
      keptalive = c.check_heartbeat(@child_heartbeat_limit)
      unless keptalive
        @log.error "Heartbeat broke out. Restarting child process."
        c.start_killing(false)
      end
    rescue EOFError
      @log.error "Heartbeat pipe is closed. Restarting child process."
      c.start_killing(true)
    rescue
      @log.error "Unknown error: #{$!.class}: #{$!}. Restarting child process."
      $!.backtrace.each {|bt| @log.warn "\t#{bt}" }
      c.start_killing(false)
    end

    try_join
  end

  unless @cpm
    begin
      @cpm = fork_child
    rescue
      @log.error "Failed to fork child process: #{$!.class}: #{$!}"
      $!.backtrace.each {|bt| @log.warn "\t#{bt}" }
    end
  end

  nil
end

#logrotatedObject



98
99
100
101
102
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 98

def logrotated
  if c = @cpm
    c.send_signal(:CONT)
  end
end

#restart(immediate, config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 33

def restart(immediate, config)
  @child_heartbeat_limit = config[:child_heartbeat_limit] || 10.0
  @child_kill_interval = config[:child_kill_interval] || 2.0
  @child_graceful_kill_limit = config[:child_graceful_kill_limit] || nil
  @log = config[:logger]
  @config = config  # for child process

  if c = @cpm
    c.start_killing(immediate)
  end
end

#stop(immediate) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/perfectqueue/multiprocess/fork_processor.rb', line 45

def stop(immediate)
  @stop = true
  if c = @cpm
    c.start_killing(immediate)
  end
  self
end