From 57dea7ad93bb15491cd33281d933ca4ef6440e45 Mon Sep 17 00:00:00 2001 From: William Morgan Date: Thu, 6 Aug 2009 21:33:19 -0400 Subject: [PATCH] refactor index locking interaction and replace suicidemanager Since SIGTERM now works, we have a better way of killing a remote process than SuicideManager. So replace that with a $die global. Also refactor the interactive locking code into a separate module so as not to pollute index.rb with such trivial concerns. --- Manifest.txt | 2 +- bin/sup | 37 +++---------------- bin/sup-add | 2 +- bin/sup-sync | 2 +- bin/sup-sync-back | 2 +- bin/sup-tweak-labels | 4 +- lib/sup.rb | 3 +- lib/sup/index.rb | 38 +------------------ lib/sup/interactive-lock.rb | 74 +++++++++++++++++++++++++++++++++++++ lib/sup/suicide.rb | 37 ------------------- lib/sup/util.rb | 1 + 11 files changed, 91 insertions(+), 111 deletions(-) create mode 100644 lib/sup/interactive-lock.rb delete mode 100644 lib/sup/suicide.rb diff --git a/Manifest.txt b/Manifest.txt index be633d7..09d867e 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -32,6 +32,7 @@ lib/sup/index.rb lib/sup/keymap.rb lib/sup/label.rb lib/sup/logger.rb +lib/sup/interactive-lock.rb lib/sup/maildir.rb lib/sup/mbox.rb lib/sup/mbox/loader.rb @@ -67,7 +68,6 @@ lib/sup/poll.rb lib/sup/rfc2047.rb lib/sup/sent.rb lib/sup/source.rb -lib/sup/suicide.rb lib/sup/tagger.rb lib/sup/textfield.rb lib/sup/thread.rb diff --git a/bin/sup b/bin/sup index b87c112..c6556e3 100755 --- a/bin/sup +++ b/bin/sup @@ -130,37 +130,14 @@ end module_function :start_cursing, :stop_cursing Index.new -begin - Index.lock -rescue Index::LockError => e - require 'highline' - - h = HighLine.new - h.wrap_at = :auto - h.say Index.fancy_lock_error_message_for(e) - - case h.ask("Should I ask that process to kill itself? ") - when /^\s*y(es)?\s*$/i - h.say "Ok, suggesting seppuku..." - FileUtils.touch Redwood::SUICIDE_FN - sleep SuicideManager::DELAY * 2 - FileUtils.rm_f Redwood::SUICIDE_FN - h.say "Let's try that again." - retry - else - h.say < e @@ -319,13 +295,12 @@ begin bm.draw_screen end - bm.kill_all_buffers if SuicideManager.die? + bm.kill_all_buffers if $die rescue Exception => e Redwood::record_exception e, "main" ensure unless $opts[:no_threads] PollManager.stop if PollManager.instantiated? - SuicideManager.stop if PollManager.instantiated? Index.stop_lock_update_thread end @@ -335,7 +310,7 @@ ensure stop_cursing Redwood::log "stopped cursing" - if SuicideManager.instantiated? && SuicideManager.die? + if $die Redwood::log "I've been ordered to commit seppuku. I obey!" end diff --git a/bin/sup-add b/bin/sup-add index 3ab7c4d..64d2890 100755 --- a/bin/sup-add +++ b/bin/sup-add @@ -79,7 +79,7 @@ $terminal.wrap_at = :auto Redwood::start index = Redwood::Index.new -index.lock_or_die +index.lock_interactively or exit begin Redwood::SourceManager.load_sources diff --git a/bin/sup-sync b/bin/sup-sync index 8e37c74..62e1b67 100755 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -111,7 +111,7 @@ restored_state = end seen = {} -index.lock_or_die +index.lock_interactively or exit begin index.load diff --git a/bin/sup-sync-back b/bin/sup-sync-back index 56ac4eb..6e66c6b 100755 --- a/bin/sup-sync-back +++ b/bin/sup-sync-back @@ -66,7 +66,7 @@ end Redwood::start index = Redwood::Index.new -index.lock_or_die +index.lock_interactively or exit deleted_fp, spam_fp = nil unless opts[:dry_run] diff --git a/bin/sup-tweak-labels b/bin/sup-tweak-labels index 8ae5c26..eac300e 100755 --- a/bin/sup-tweak-labels +++ b/bin/sup-tweak-labels @@ -58,10 +58,12 @@ add_labels = (opts[:add] || "").split(",").map { |l| l.intern }.uniq remove_labels = (opts[:remove] || "").split(",").map { |l| l.intern }.uniq Trollop::die "nothing to do: no labels to add or remove" if add_labels.empty? && remove_labels.empty? +Trollop::die "no sources specified" if ARGV.empty? Redwood::start +index = Redwood::Index.new +index.lock_interactively or exit begin - index = Redwood::Index.new index.load source_ids = diff --git a/lib/sup.rb b/lib/sup.rb index 54de73f..cfa93fc 100644 --- a/lib/sup.rb +++ b/lib/sup.rb @@ -114,7 +114,6 @@ module Redwood Redwood::DraftManager.new Redwood::DRAFT_DIR Redwood::UpdateManager.new Redwood::PollManager.new - Redwood::SuicideManager.new Redwood::SUICIDE_FN Redwood::CryptoManager.new Redwood::UndoManager.new Redwood::SourceManager.new @@ -256,7 +255,6 @@ end ## now everything else (which can feel free to call Redwood::log at load time) require "sup/update" -require "sup/suicide" require "sup/message-chunks" require "sup/message" require "sup/source" @@ -266,6 +264,7 @@ require "sup/imap" require "sup/person" require "sup/account" require "sup/thread" +require "sup/interactive-lock" require "sup/index" require "sup/textfield" require "sup/colormap" diff --git a/lib/sup/index.rb b/lib/sup/index.rb index fb46eb0..54ec843 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -13,6 +13,8 @@ end module Redwood class BaseIndex + include InteractiveLock + class LockError < StandardError def initialize h @h = h @@ -54,42 +56,6 @@ class BaseIndex @lock_update_thread = nil end - def possibly_pluralize number_of, kind - "#{number_of} #{kind}" + - if number_of == 1 then "" else "s" end - end - - def fancy_lock_error_message_for e - secs = (Time.now - e.mtime).to_i - mins = secs / 60 - time = - if mins == 0 - possibly_pluralize secs , "second" - else - possibly_pluralize mins, "minute" - end - - < e - $stderr.puts fancy_lock_error_message_for(e) - $stderr.puts < e + stream.puts < 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 + FileUtils.rm e.path + + stream.puts "Let's try that one more time." + begin + Index.lock + true + rescue Index::LockError => e + end + end + end + end + + stream.puts "Sorry, couldn't unlock the index." unless success + success + end + end +end + +end diff --git a/lib/sup/suicide.rb b/lib/sup/suicide.rb deleted file mode 100644 index 53f31a5..0000000 --- a/lib/sup/suicide.rb +++ /dev/null @@ -1,37 +0,0 @@ -module Redwood - -class SuicideManager - include Singleton - - DELAY = 5 - - def initialize fn - @fn = fn - @die = false - @thread = nil - self.class.i_am_the_instance self - FileUtils.rm_f @fn - end - - bool_reader :die - def please_die!; @die = true end - - def start - @thread = Redwood::reporting_thread("suicide watch") do - while true - sleep DELAY - if File.exists? @fn - FileUtils.rm_f @fn - @die = true - end - end - end - end - - def stop - @thread.kill if @thread - @thread = nil - end -end - -end diff --git a/lib/sup/util.rb b/lib/sup/util.rb index 3f2c901..f71094e 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -24,6 +24,7 @@ class Lockfile def lockinfo_on_disk h = load_lock_id IO.read(path) h['mtime'] = File.mtime path + h['path'] = path h end -- 2.45.2