Skip to content

Instantly share code, notes, and snippets.

@tobi
Created March 22, 2012 16:36
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 tobi/2159436 to your computer and use it in GitHub Desktop.
Save tobi/2159436 to your computer and use it in GitHub Desktop.
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