Created
August 25, 2016 22:00
-
-
Save austra/451d0a527adf0d7fbf165701d7f65932 to your computer and use it in GitHub Desktop.
Kill irb sessions after x minutes of inactivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add some methods to IRB::Context and IRB::Irb | |
# for easier timeout implementation. | |
class IRB::Irb | |
def status | |
@signal_status | |
end | |
end | |
class IRB::Context | |
attr_reader :irb | |
attr_reader :line_no | |
def is_evaluating? | |
self.irb.status == :IN_EVAL | |
end | |
end | |
# Implement an auto exit timer thread. Timeout is given in seconds. | |
module IRB | |
def self.auto_exit_after(timeout = 60) | |
Thread.new { | |
context = IRB.conf[:MAIN_CONTEXT] | |
last_input = Time.now | |
last_line = context.line_no | |
loop { | |
sleep 10 | |
# Check if irb is running a command | |
if context.is_evaluating? | |
# Reset the input time and ignore any timeouts | |
last_input = Time.now | |
next | |
end | |
# Check for new input | |
if last_line != context.line_no | |
# Got new input | |
last_line = context.line_no | |
last_input = Time.now | |
next | |
end | |
# No new input, check if idle time exceeded | |
if Time.now - last_input > timeout | |
$stderr.puts "\n** IRB exiting due to idle timeout. Goodbye..." | |
Process.kill("KILL", Process.pid) | |
end | |
} | |
} | |
end | |
end | |
Thread.new do | |
IRB.auto_exit_after(3600) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment