Timeout commands if they run for longer than three seconds
parent
e6d32c5cab
commit
fb94656f49
|
@ -0,0 +1,3 @@
|
|||
export OUR_NICK=your
|
||||
export OUR_CHANNELS='#bots'
|
||||
export OUR_CMDS_DIR=/town/our
|
|
@ -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
19
our.rb
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue