5 ## wrap a nice interactive layer on top of anything that has a #lock method
6 ## which throws a LockError which responds to #user, #host, #mtim, #pname, and
10 def pluralize number_of, kind; "#{number_of} #{kind}" + (number_of == 1 ? "" : "s") end
12 def time_ago_in_words time
13 secs = (Time.now - time).to_i
16 pluralize secs, "second"
18 pluralize mins, "minute"
24 def lock_interactively stream=$stderr
27 rescue Index::LockError => e
29 Error: the index is locked by another process! User '#{e.user}' on
30 host '#{e.host}' is running #{e.pname} with pid #{e.pid}.
31 The process was alive as of at least #{time_ago_in_words e.mtime} ago.
34 stream.print "Should I ask that process to kill itself (y/n)? "
37 success = if $stdin.gets =~ /^\s*y(es)?\s*$/i
38 stream.puts "Ok, trying to kill process..."
41 Process.kill "TERM", e.pid.to_i
43 rescue Errno::ESRCH # no such process
44 stream.puts "Hm, I couldn't kill it."
47 stream.puts "Let's try that again."
50 rescue Index::LockError => e
51 stream.puts "I couldn't lock the index. The lockfile might just be stale."
52 stream.print "Should I just remove it and continue? (y/n) "
55 if $stdin.gets =~ /^\s*y(es)?\s*$/i
58 stream.puts "Let's try that one more time."
62 rescue Index::LockError => e
68 stream.puts "Sorry, couldn't unlock the index." unless success