Skip to content

Instantly share code, notes, and snippets.

@tobi
Created March 22, 2012 16:36
Show Gist options
  • Save tobi/2159436 to your computer and use it in GitHub Desktop.
Save tobi/2159436 to your computer and use it in GitHub Desktop.

Revisions

  1. Tobias Lütke created this gist Mar 22, 2012.
    37 changes: 37 additions & 0 deletions forever.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    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