Module: Redwood::InteractiveLock

Included in:
Index
Defined in:
lib/sup/interactive_lock.rb

Overview

wrap a nice interactive layer on top of anything that has a #lock method which throws a LockError which responds to #user, #host, #mtim, #pname, and #pid.

Constant Summary collapse

DELAY =

seconds

5

Instance Method Summary collapse

Instance Method Details

#lock_interactively(stream = $stderr) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sup/interactive_lock.rb', line 24

def lock_interactively stream=$stderr
  begin
    Index.lock
  rescue Index::LockError => e
    begin
      Process.kill 0, e.pid.to_i # 0 signal test the existence of PID
      stream.puts <<EOS
Error: the index is locked by another process! User '#{e.user}' on
host '#{e.host}' is running #{e.pname} with pid #{e.pid}.
The process was alive as of at least #{time_ago_in_words e.mtime} ago.

EOS
      stream.print "Should I ask that process to kill itself (y/n)? "
      stream.flush
      if $stdin.gets =~ /^\s*y(es)?\s*$/i
        Process.kill "TERM", e.pid.to_i
        sleep DELAY
        stream.puts "Let's try that again."
        begin
          Index.lock
        rescue Index::LockError => e
          stream.puts "I couldn't lock the index. The lockfile might just be stale."
          stream.print "Should I just remove it and continue? (y/n) "
          stream.flush
          if $stdin.gets =~ /^\s*y(es)?\s*$/i
            begin
              FileUtils.rm e.path
            rescue Errno::ENOENT
              stream.puts "The lockfile doesn't exists. We continue."
            end
            stream.puts "Let's try that one more time."
            begin
              Index.lock
            rescue Index::LockError => e
              stream.puts "I couldn't unlock the index."
              return false
            end
            return true
          end
        end
      end
    rescue Errno::ESRCH # no such process
      stream.puts "I couldn't lock the index. The lockfile might just be stale."
      begin
        FileUtils.rm e.path
      rescue Errno::ENOENT
        stream.puts "The lockfile doesn't exists. We continue."
      end
      stream.puts "Let's try that one more time."
      begin
        sleep DELAY
        Index.lock
      rescue Index::LockError => e
        stream.puts "I couldn't unlock the index."
        return false
      end
      return true
    end
    stream.puts "Sorry, couldn't unlock the index."
    return false
  end
  return true
end

#pluralize(number_of, kind) ⇒ Object



10
# File 'lib/sup/interactive_lock.rb', line 10

def pluralize number_of, kind; "#{number_of} #{kind}" + (number_of == 1 ? "" : "s") end

#time_ago_in_words(time) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/sup/interactive_lock.rb', line 12

def time_ago_in_words time
  secs = (Time.now - time).to_i
  mins = secs / 60
  time = if mins == 0
    pluralize secs, "second"
  else
    pluralize mins, "minute"
  end
end