Skip to content

Instantly share code, notes, and snippets.

@grantr
Created October 23, 2011 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantr/1307586 to your computer and use it in GitHub Desktop.
Save grantr/1307586 to your computer and use it in GitHub Desktop.
celluloid timers using Kernel.select
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