Created
October 23, 2011 17:05
-
-
Save grantr/1307586 to your computer and use it in GitHub Desktop.
celluloid timers using Kernel.select
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
module Celluloid | |
class Scheduler | |
def initialize | |
read, @write = IO.pipe | |
@pool = TimerPool.new(read) | |
at_exit { stop } | |
end | |
def start | |
@pool.start! | |
end | |
def stop | |
@write.write("t") | |
@pool.terminate | |
end | |
def interrupt | |
@write.write("i") | |
end | |
end | |
class TimerPool | |
include Celluloid | |
def initialize(read) | |
@read = read | |
end | |
def start | |
while alive? | |
#TODO ultimately the timeout would change based on the next timer that should fire | |
v = Kernel.select([@read], nil, nil, 3) | |
if v | |
v = @read.read(1) | |
case v | |
when "i" | |
puts "interrupted" | |
#TODO reconfigure, calculate next timeout | |
when "t" | |
puts "stopped" | |
break | |
else | |
puts "unknown" | |
end | |
else | |
puts "timed out" | |
#TODO fire the signal/call the block | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment