Sha256: 65809f6d2f09181154b5d1bbbe016e15efe9e56a6720c8552c7ba9110e08446a

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

unless defined? $__rq_refresher__
  module RQ 
#{{{
    LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
      defined? LIBDIR

    class Refresher
#{{{
      SIGNALS = %w(SIGTERM SIGINT SIGKILL)
      attr :path
      attr :pid
      attr :refresh_rate
      def initialize path, refresh_rate = 8
#{{{
        @path = path
        File::stat path
        @refresh_rate = Float refresh_rate
        @pipe = IO::pipe
        if((@pid = Util::fork))
          @pipe.last.close
          @pipe = @pipe.first
          @thread = Thread::new{loop{@pipe.gets}}
          Process::detach @pid
        else
          begin
            pid = Process::pid
            ppid = Process::ppid
            $0 = "#{ path }.refresher.#{ pid }"
            SIGNALS.each{|sig| trap(sig){ raise }}
            @pipe.first.close
            @pipe = @pipe.last
            loop do
              FileUtils::touch @path
              sleep @refresh_rate
              Process::kill 0, ppid
              @pipe.puts pid
            end
          rescue Exception => e
            exit!
          end
        end
#}}}
      end
      def kill
#{{{
        begin
          @thread.kill rescue nil
          @pipe.close rescue nil
          SIGNALS.each{|sig| Process::kill sig, @pid rescue nil}
        ensure
=begin
          n = 42
          dead = false
          begin
            n.times do |i|
              Process::kill 0, @pid
              sleep 1
            end
          rescue Errno::ESRCH
            dead = true
          end
          raise "runaway refresher <#{ @pid }> must be killed!" unless dead
=end
        end
#}}}
      end
#}}}
    end # class Refresher
#}}}
  end # module RQ
$__rq_refresher__ = __FILE__ 
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rq-0.1.7 lib/rq-0.1.7/refresher.rb