Class: RQ::Refresher

Inherits:
Object
  • Object
show all
Defined in:
lib/rq/refresher.rb

Overview

the job of the Refresher is to maintain a lease on a file that has been locked. the method is simply to touch the file at a certain interval thereby keeping it ‘fresh’. a separate process, vs. a thread, is used for this task to eliminate any chance that the ruby interpreter might put all threads to sleep for some blocking tasks, like fcntl based locks which are used heavily in RQ, resulting in a a prematurely stale lockfile

Constant Summary collapse

SIGNALS =

–{{{

%w(SIGTERM SIGINT SIGKILL)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, refresh_rate = 8) ⇒ Refresher

Returns a new instance of Refresher.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rq/refresher.rb', line 21

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/rq/refresher.rb', line 18

def path
  @path
end

#pidObject (readonly)

Returns the value of attribute pid.



19
20
21
# File 'lib/rq/refresher.rb', line 19

def pid
  @pid
end

#refresh_rateObject (readonly)

Returns the value of attribute refresh_rate.



20
21
22
# File 'lib/rq/refresher.rb', line 20

def refresh_rate
  @refresh_rate
end

Instance Method Details

#killObject

–}}}



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rq/refresher.rb', line 52

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