Timeout commands if they run for longer than three seconds

main
Stef Dunlap 2022-08-24 12:27:37 -04:00
parent e6d32c5cab
commit fb94656f49
3 changed files with 16 additions and 8 deletions

3
.envrc 100644
View File

@ -0,0 +1,3 @@
export OUR_NICK=your
export OUR_CHANNELS='#bots'
export OUR_CMDS_DIR=/town/our

View File

@ -8,7 +8,7 @@ WorkingDirectory=/home/kindrobot/spacework/our
ExecStart=/home/kindrobot/spacework/our/our.rb
Environment="OUR_NICK=your"
Environment="OUR_CHANNELS=#bots"
Environment="OUR_CMDS_DIR=/home/kindrobot/spacework/our/cmds/"
Environment="OUR_CMDS_DIR=/town/our"
Restart=always
RestartSec=5
StartLimitInterval=60s

19
our.rb
View File

@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require 'socket'
require 'open3'
require 'socket'
require 'timeout'
# configurable environment variables
nick = ENV['OUR_NICK'] || 'our'
@ -94,13 +95,17 @@ i.hook do |msg|
end
begin
# child = IO.popen [cmd, args, msg.prefix, target]
# out = child.gets
# child.close
Open3.popen2e(cmd, args, msg.prefix, target) {|_, stdout, _|
out = stdout.gets
Open3.popen2e(cmd, args, msg.prefix, target) do |_, stdout, wait_thread|
out = nil
Timeout::timeout(3) do
out = stdout.gets # only interested in the first line of output
stdout.gets until stdout.eof? # make sure process finishes in time allotted
end
i.privmsg target, out if out
}
rescue Timeout::Error
Process.kill("KILL", wait_thread.pid)
i.privmsg target, "[our.rb] command timed out"
end
rescue Exception => e
i.privmsg target, "[our.rb] #{e.to_s}"
end