Created
March 22, 2012 16:36
-
-
Save tobi/2159436 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
trap("INT") { $done = true } | |
class Forever | |
def every(timespan, &block) | |
@threads ||= [] | |
thread = Thread.new do | |
last_run = 0 | |
while !$done | |
now = Time.now.to_i | |
if now - last_run > timespan | |
block.call | |
last_run = now | |
end | |
sleep 1 | |
end | |
end | |
@threads.push(thread) | |
end | |
def run | |
@threads.collect(&:join) | |
end | |
end | |
require 'rubygems' | |
require 'active_support/all' | |
forever = Forever.new | |
forever.every 5.seconds do | |
puts 'still running' | |
end | |
forever.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment