Method: Aggkit::Watcher#exec

Defined in:
lib/aggkit/watcher.rb

#execObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aggkit/watcher.rb', line 91

def exec
  install_signal_handlers(@pipe)

  begin
    yield(self)
  rescue StandardError => e
    @crashed = true
    @code = 1
    error e.inspect
    error e.backtrace.last(20).join("\n")
    log 'Try exits gracefully..'
    terminate_all
    exit!(@code)
  end

  loop_childs
  quit! if @procs.all?(&:handled?)

  Thread.new(@pipe) do |pipe|
    loop do
      begin
        sleep 10
        pipe.puts :gc
      rescue StandardError => e
        log "GC thread exception: #{e.inspect}"
      end
    end
  end

  loop do
    log 'Main loop...'
    case event = @pipe.gets
    when :term
      log '  * Child terminated: try exits gracefully...'
      terminate_all
      loop_childs
      quit!
    when *(EXIT_SIGNALS + TERM_SIGNALS).map(&:to_sym)
      log "  * Catch #{event}: try exits gracefully..."
      terminate_all
      loop_childs
      quit!
    when :child, :gc
      log "  * Main loop event: #{event}"
      loop_childs
      quit! if @procs.all?(&:handled?)
    else
      log "  * Unknown event: #{event.inspect}"
      loop_childs
      quit! if @procs.all?(&:handled?)
    end
  end
end