Class: CMDHandle
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(cmd, timeout = nil, &blk) ⇒ CMDHandle
constructor
A new instance of CMDHandle.
- #run ⇒ Object
Constructor Details
#initialize(cmd, timeout = nil, &blk) ⇒ CMDHandle
Returns a new instance of CMDHandle.
166 167 168 169 170 |
# File 'lib/base/backup.rb', line 166 def initialize(cmd, timeout=nil, &blk) @cmd = cmd @timeout = timeout @errback = blk end |
Class Method Details
.execute(cmd, timeout = nil, *args) ⇒ Object
200 201 202 203 204 |
# File 'lib/base/backup.rb', line 200 def self.execute(cmd, timeout = nil, *args) errb = args.pop if args.last.is_a? Proc instance = self.new(cmd, timeout, &errb) instance.run end |
Instance Method Details
#run ⇒ Object
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 |
# File 'lib/base/backup.rb', line 172 def run pid = fork if pid # parent process success = false begin success = Timeout::timeout(@timeout) do Process.waitpid(pid) value = $?.exitstatus @errback.call(@cmd, value, "No message.") if value != 0 && @errback return value == 0 end rescue Timeout::Error Process.detach(pid) Process.kill("KILL", pid) @errback.call(@cmd, -1, "Killed due to timeout.") if @errback return false end else begin # child process exec(@cmd) rescue => e exit! end end end |