]> git.cworth.org Git - sup/commitdiff
Merge branch 'locking-refactor'
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Tue, 25 Aug 2009 13:51:15 +0000 (09:51 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Tue, 25 Aug 2009 13:51:38 +0000 (09:51 -0400)
Conflicts:
bin/sup
bin/sup-sync-back
bin/sup-tweak-labels
lib/sup.rb
lib/sup/suicide.rb

1  2 
bin/sup
bin/sup-add
bin/sup-sync
bin/sup-sync-back
bin/sup-tweak-labels
lib/sup.rb
lib/sup/index.rb
lib/sup/util.rb

diff --cc bin/sup
index 8a377f78149f5a4b17a56e06036fcae96e2b3fb6,c6556e35b5766f387816d3668f711272b330999a..bbb6c1711a7d5f580530a95859b4853649bf69c9
+++ b/bin/sup
@@@ -130,32 -129,8 +130,8 @@@ def stop_cursin
  end
  module_function :start_cursing, :stop_cursing
  
 -Index.new
 +Index.init
- 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 <<EOS
- Ok, giving up. If the process crashed and left a stale lockfile, you
- can fix this by manually deleting #{Index.lockfile}.
- EOS
-     exit
-   end
- end
+ Index.lock_interactively or exit
  
  begin
    Redwood::start
@@@ -342,12 -308,10 +317,12 @@@ ensur
  
    Redwood::finish
    stop_cursing
 -  Redwood::log "stopped cursing"
 +  Redwood::Logger.remove_all_sinks!
 +  Redwood::Logger.add_sink $stderr, false
 +  debug "stopped cursing"
  
-   if SuicideManager.instantiated? && SuicideManager.die?
+   if $die
 -    Redwood::log "I've been ordered to commit seppuku. I obey!"
 +    info "I've been ordered to commit seppuku. I obey!"
    end
  
    if Redwood::exceptions.empty?
diff --cc bin/sup-add
index 8f7010ee3f4dc06aa9051006ab08a4f45a42bec9,64d2890596cd4d5465d57bbc7e8aafdeb2c0c2a5..e27a0ebf6ff446a9347d4fd8937653ab45b97db8
@@@ -77,9 -77,9 +77,9 @@@ en
  
  $terminal.wrap_at = :auto
  Redwood::start
 -index = Redwood::Index.new
 +index = Redwood::Index.init
  
- index.lock_or_die
+ index.lock_interactively or exit
  
  begin
    Redwood::SourceManager.load_sources
diff --cc bin/sup-sync
index b743c1c13e3cdf9f73d6d24a5c88882985c30681,62e1b67799f9133ca6d5ff07b97875d4f62fbbd2..2aa00c3720bcd0dbbce60499e5d14a70e01ae418
@@@ -95,24 -93,25 +95,24 @@@ target = [:new, :changed, :all, :restor
  op = [:asis, :restore, :discard].find { |x| opts[x] } || :asis
  
  Redwood::start
 -index = Redwood::Index.new
 -
 -restored_state =
 -  if opts[:restore]
 -    dump = {}
 -    $stderr.puts "Loading state dump from #{opts[:restore]}..."
 -    IO.foreach opts[:restore] do |l|
 -      l =~ /^(\S+) \((.*?)\)$/ or raise "Can't read dump line: #{l.inspect}"
 -      mid, labels = $1, $2
 -      dump[mid] = labels.symbolistize
 -    end
 -    $stderr.puts "Read #{dump.size} entries from dump file."
 -    dump
 -  else
 -    {}
 +index = Redwood::Index.init
 +
 +restored_state = if opts[:restore]
 +  dump = {}
 +  $stderr.puts "Loading state dump from #{opts[:restore]}..."
 +  IO.foreach opts[:restore] do |l|
 +    l =~ /^(\S+) \((.*?)\)$/ or raise "Can't read dump line: #{l.inspect}"
 +    mid, labels = $1, $2
 +    dump[mid] = labels.to_set_of_symbols
    end
 +  $stderr.puts "Read #{dump.size} entries from dump file."
 +  dump
 +else
 +  {}
 +end
  
  seen = {}
- index.lock_or_die
+ index.lock_interactively or exit
  begin
    index.load
  
index 4d76f17f587fb75e4294b4a39c4bf52f0d5280c8,6e66c6b646519afb1b5865a40e336520768ce64d..6298c97c3e6190e8704ff2cd1333843f09511fea
@@@ -65,8 -65,8 +65,8 @@@ EO
  end
  
  Redwood::start
 -index = Redwood::Index.new
 +index = Redwood::Index.init
- index.lock_or_die
+ index.lock_interactively or exit
  
  deleted_fp, spam_fp = nil
  unless opts[:dry_run]
index 138f7e1738c7c6029ceb40003716d71763be53ab,eac300ebf866e90ef1f7ac1328713e87126331d6..90f6a57ddb0cbebd037f4a74dd1692a191de522e
@@@ -54,22 -54,25 +54,24 @@@ EO
  end
  opts[:verbose] = true if opts[:very_verbose]
  
 -add_labels = (opts[:add] || "").split(",").map { |l| l.intern }.uniq
 -remove_labels = (opts[:remove] || "").split(",").map { |l| l.intern }.uniq
 +add_labels = opts[:add].to_set_of_symbols ","
 +remove_labels = opts[:remove].to_set_of_symbols ","
  
  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 = Redwood::Index.init
+ index.lock_interactively or exit
  begin
-   index = Redwood::Index.init
    index.load
  
 -  source_ids = 
 -    if opts[:all_sources]
 -      Redwood::SourceManager.sources
 -    else
 -      ARGV.map do |uri|
 -        Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
 -      end
 +  source_ids = if opts[:all_sources]
 +    Redwood::SourceManager.sources
 +  else
 +    ARGV.map do |uri|
 +      Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
 +    end
    end.map { |s| s.id }
    Trollop::die "nothing to do: no sources" if source_ids.empty?
  
diff --cc lib/sup.rb
index 43daa7e7be19b0ba12c83bec1d67c5f50d0b6d94,cfa93fc6608c13c6c0dd508c0c99267f6bdcb1f6..16c2b3f3bbf802d6e713714a48aecbde41ad8dc2
@@@ -119,17 -107,16 +119,16 @@@ module Redwoo
    end
  
    def start
 -    Redwood::SentManager.new $config[:sent_source] || 'sup://sent'
 -    Redwood::ContactManager.new Redwood::CONTACT_FN
 -    Redwood::LabelManager.new Redwood::LABEL_FN
 -    Redwood::AccountManager.new $config[:accounts]
 -    Redwood::DraftManager.new Redwood::DRAFT_DIR
 -    Redwood::UpdateManager.new
 -    Redwood::PollManager.new
 -    Redwood::CryptoManager.new
 -    Redwood::UndoManager.new
 -    Redwood::SourceManager.new
 +    Redwood::SentManager.init $config[:sent_source] || 'sup://sent'
 +    Redwood::ContactManager.init Redwood::CONTACT_FN
 +    Redwood::LabelManager.init Redwood::LABEL_FN
 +    Redwood::AccountManager.init $config[:accounts]
 +    Redwood::DraftManager.init Redwood::DRAFT_DIR
 +    Redwood::UpdateManager.init
 +    Redwood::PollManager.init
-     Redwood::SuicideManager.init Redwood::SUICIDE_FN
 +    Redwood::CryptoManager.init
 +    Redwood::UndoManager.init
 +    Redwood::SourceManager.init
    end
  
    def finish
@@@ -258,14 -253,8 +257,13 @@@ include Redwood::LogsStuf
      $encoding = "UTF-8"
    end
  
 -## now everything else (which can feel free to call Redwood::log at load time)
 +require "sup/buffer"
 +require "sup/keymap"
 +require "sup/mode"
 +require "sup/modes/scroll-mode"
 +require "sup/modes/text-mode"
 +require "sup/modes/log-mode"
  require "sup/update"
- require "sup/suicide"
  require "sup/message-chunks"
  require "sup/message"
  require "sup/source"
index dfaeee819c1db70f410de829f001ccf21c19280c,54ec84325224bce3a0ff75dbd695daf42c3e2b98..ff03f195f6bca74aefdb23eedecc01c2172ee39c
@@@ -53,45 -56,9 +55,9 @@@ class BaseInde
      @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
-     <<EOS
- Error: the sup 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 #{time} ago.
- EOS
-   end
-   def lock_or_die
-     begin
-       lock
-     rescue LockError => e
-       $stderr.puts fancy_lock_error_message_for(e)
-       $stderr.puts <<EOS
- You can wait for the process to finish, or, if it crashed and left a
- stale lock file behind, you can manually delete #{@lock.path}.
- EOS
-       exit
-     end
-   end
    def unlock
      if @lock && @lock.locked?
 -      Redwood::log "unlocking #{lockfile}..."
 +      debug "unlocking #{lockfile}..."
        @lock.unlock
      end
    end
diff --cc lib/sup/util.rb
Simple merge