commit dbd5944c65e9947f4f4e5fdbcd32258cfcc784d7
parent 6119bb9b65371c8fa7a9e0895b35be1207822aa1
Author: Rich Lane <rlane@club.cc.cmu.edu>
Date: Tue, 29 Dec 2009 17:38:03 -0800
async thread indexing
Diffstat:
3 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -152,6 +152,7 @@ Index.lock_interactively or exit
begin
Redwood::start
Index.load
+ Index.start_sync_worker unless $opts[:no_threads]
$die = false
trap("TERM") { |x| $die = true }
@@ -340,6 +341,7 @@ ensure
HookManager.run "shutdown"
+ Index.stop_sync_worker
Redwood::finish
stop_cursing
Redwood::Logger.remove_all_sinks!
diff --git a/lib/sup/index.rb b/lib/sup/index.rb
@@ -28,6 +28,8 @@ class BaseIndex
def initialize dir=BASE_DIR
@dir = dir
@lock = Lockfile.new lockfile, :retries => 0, :max_age => nil
+ @sync_worker = nil
+ @sync_queue = Queue.new
end
def lockfile; File.join @dir, "lock" end
@@ -175,10 +177,32 @@ class BaseIndex
def save_thread t
t.each_dirty_message do |m|
- update_message_state m
+ if @sync_worker
+ @sync_queue << m
+ else
+ update_message_state m
+ end
m.clear_dirty
end
end
+
+ def start_sync_worker
+ @sync_worker = Redwood::reporting_thread('index sync') { run_sync_worker }
+ end
+
+ def stop_sync_worker
+ return unless worker = @sync_worker
+ @sync_worker = nil
+ @sync_queue << :die
+ worker.join
+ end
+
+ def run_sync_worker
+ while m = @sync_queue.deq
+ return if m == :die
+ update_message_state m
+ end
+ end
end
index_name = ENV['SUP_INDEX'] || $config[:index] || DEFAULT_INDEX
diff --git a/lib/sup/modes/thread-index-mode.rb b/lib/sup/modes/thread-index-mode.rb
@@ -477,7 +477,7 @@ EOS
BufferManager.say("Saving threads...") do |say_id|
dirty_threads.each_with_index do |t, i|
BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
- Index.save_thread t
+ Index.save_thread_async t
end
end
end