Sha256: dfb43d74bb8ee006016231949651d9e0b075035063d7d2deb8f436a579a2723b
Contents?: true
Size: 1.85 KB
Versions: 2
Compression:
Stored size: 1.85 KB
Contents
############################################################################ # The following code is based on code originally copied from # https://gist.github.com/lpar/1032297 # Gist title: lpar/timeout.rb ############################################################################ # Runs a specified shell command in a separate thread. # If it exceeds the given timeout in seconds, kills it. # Returns any output produced by the command (stdout or stderr) as a String. # Uses Kernel.select to wait up to the tick length (in seconds) between # checks on the command's status # # If you've got a cleaner way of doing this, I'd be interested to see it. # If you think you can do it with Ruby's Timeout module, think again. def run_with_timeout(directory,command, timeout, tick) output = '' exit_code=1 begin # Start task in another thread, which spawns a process stdin, stderrout, thread = Open3.popen2e(command, :chdir=>directory) # Get the pid of the spawned process pid = thread[:pid] start = Time.now while (Time.now - start) < timeout and thread.alive? # Wait up to `tick` seconds for output/error data Kernel.select([stderrout], nil, nil, tick) # Try to read the data begin output << stderrout.read_nonblock(BUFFER_SIZE) rescue IO::WaitReadable # A read would block, so loop around for another select rescue EOFError # Command has completed, not really an error... break end end # Give Ruby time to clean up the other thread sleep 1 if thread.alive? # We need to kill the process, because killing the thread leaves # the process alive but detached, annoyingly enough. Process.kill("TERM", pid) else exit_code=thread.value end ensure stdin.close if stdin stderrout.close if stderrout end return [output,exit_code] end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dev_commands-0.0.49 | lib/timeout.rb |
dev_commands-0.0.48 | lib/timeout.rb |