Class: DockerToolkit::Watcher
- Inherits:
-
Object
- Object
- DockerToolkit::Watcher
- Defined in:
- lib/docker_toolkit/watcher.rb
Instance Method Summary collapse
- #add(*cmd) ⇒ Object
- #error(msg) ⇒ Object
- #exec ⇒ Object
-
#initialize ⇒ Watcher
constructor
A new instance of Watcher.
- #log(msg) ⇒ Object
- #stop_all ⇒ Object
- #synchro_readline(io, out) ⇒ Object
Constructor Details
#initialize ⇒ Watcher
Returns a new instance of Watcher.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/docker_toolkit/watcher.rb', line 9 def initialize @lock = Mutex.new @procs = [] @code = 0 @crashed = false @threads = [] @who = nil @stopping = false end |
Instance Method Details
#add(*cmd) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/docker_toolkit/watcher.rb', line 77 def add(*cmd) cmd = cmd.flatten.map{|c| c.to_s.strip}.reject(&:empty?) process = ChildProcess.build(*cmd) rerr, werr = IO.pipe rout, wout = IO.pipe process.io.stdout = wout process.io.stderr = werr process.duplex = true = { cmd: cmd, process: process, stdout: rout, stderr: rerr, stdin: process.io.stdin } @threads << Thread.new([:stdout], STDOUT) do |io, out| loop do break unless synchro_readline(io, out) end end @threads << Thread.new([:stderr], STDERR) do |io, out| loop do break unless synchro_readline(io, out) end end log "Starting #{meta[:cmd]}" [:pid] = [:process].start.pid @procs.push() end |
#error(msg) ⇒ Object
127 128 129 |
# File 'lib/docker_toolkit/watcher.rb', line 127 def error(msg) STDERR.puts "[watcher]: Error: #{msg}" end |
#exec ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/docker_toolkit/watcher.rb', line 131 def exec %w[EXIT QUIT].each do |sig| trap(sig) do stop_all end end %w[INT TERM].each do |sig| trap(sig) do log "Catch #{sig}: try exits gracefully.." stop_all end end trap('CLD') do |*_args| unhandled = @procs.reject do || [:handled] || ![:process].exited? end unhandled.any? do || log 'Child finished' log " Process[#{meta[:pid]}]: #{meta[:cmd]}" log " status: #{meta[:process].crashed? ? 'crashed' : 'exited'}" log " code: #{meta[:process].exit_code}" [:handled] = true begin [:stdin].close rescue StandardError nil end begin [:stdout].close rescue StandardError nil end begin [:stderr].close rescue StandardError nil end if !@crashed && [:process].crashed? @crashed = true @code = [:process].exit_code @who = [:cmd] end unless @stopping log 'Try exits gracefully..' stop_all end true end end begin yield(self) rescue StandardError => e @crashed = true @code = 1 error e.inspect error e.backtrace.last(20).join("\n") log 'Try exits gracefully..' stop_all end @threads.map(&:join) @code = [@code || 0, 1].max if @crashed exit(@code || 0) end |
#log(msg) ⇒ Object
123 124 125 |
# File 'lib/docker_toolkit/watcher.rb', line 123 def log(msg) puts "[watcher]: #{msg}" end |
#stop_all ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/docker_toolkit/watcher.rb', line 20 def stop_all @stopping = true terminating = @procs.reject do || [:handled] || [:process].exited? end terminating.each do || begin [:stdin].close rescue StandardError nil end ::Process.kill 'TERM', [:process].pid end terminating.each do || begin [:process].poll_for_exit(5) rescue ChildProcess::TimeoutError [:process].stop(1) [:stdout].close [:stderr].close end end unless @crashed = @procs.detect do || [:process].crashed? end if @crashed = true @code = [:process].exit_code @who = [:cmd] end end @procs.each do || begin [:stdout].close rescue StandardError nil end begin [:stderr].close rescue StandardError nil end end Thread.new do sleep 2 @threads.each(&:terminate) end end |
#synchro_readline(io, out) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/docker_toolkit/watcher.rb', line 115 def synchro_readline(io, out) str = io.gets @lock.synchronize{ out.puts str } true rescue StandardError => e false end |