commit 7f2af9ac8bc70ff41b0dc64eccd068118abe19f2
parent f4adc029a972c56d22def1e903dae7d7be89e0b0
Author: William Morgan <wmorgan-sup@masanjin.net>
Date: Wed, 4 Jun 2008 11:53:15 -0700
exception cleanup: synchronize access, and require fastthread
Diffstat:
2 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -5,6 +5,7 @@ require 'ncurses'
require 'curses'
require 'fileutils'
require 'trollop'
+require 'fastthread'
require "sup"
BIN_VERSION = "git"
@@ -21,7 +22,6 @@ EOS
exit(-1)
end
-$exceptions = []
$opts = Trollop::options do
version "sup v#{Redwood::VERSION}"
banner <<EOS
@@ -224,7 +224,7 @@ begin
SearchResultsMode.spawn_from_query $opts[:search]
end
- until $exceptions.nonempty? || SuicideManager.die?
+ until Redwood::exceptions.nonempty? || SuicideManager.die?
c = Ncurses.nonblocking_getch
next unless c
bm.erase_flash
@@ -305,7 +305,7 @@ begin
bm.kill_all_buffers if SuicideManager.die?
rescue Exception => e
- $exceptions << [e, "main"]
+ Redwood::record_exception e, "main"
ensure
unless $opts[:no_threads]
PollManager.stop if PollManager.instantiated?
@@ -321,7 +321,7 @@ ensure
Redwood::log "I've been ordered to commit seppuku. I obey!"
end
- if $exceptions.empty?
+ if Redwood::exceptions.empty?
Redwood::log "no fatal errors. good job, william."
Index.save
else
@@ -331,9 +331,9 @@ ensure
Index.unlock
end
-unless $exceptions.empty?
+unless Redwood::exceptions.empty?
File.open(File.join(BASE_DIR, "exception-log.txt"), "w") do |f|
- $exceptions.each do |e, name|
+ Redwood::exceptions.each do |e, name|
f.puts "--- #{e.class.name} from thread: #{name}"
f.puts e.message, e.backtrace
end
@@ -350,7 +350,7 @@ Sincerely,
William
----------------------------------------------------------------
EOS
- $exceptions.each do |e, name|
+ Redwood::exceptions.each do |e, name|
puts "--- #{e.class.name} from thread: #{name}"
puts e.message, e.backtrace
end
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -50,7 +50,18 @@ module Redwood
YAML_DOMAIN = "masanjin.net"
YAML_DATE = "2006-10-01"
-## record exceptions thrown in threads nicely
+ ## record exceptions thrown in threads nicely
+ @exceptions = []
+ @exception_mutex = Mutex.new
+
+ attr_reader :exceptions
+ def record_exception e, name
+ @exception_mutex.synchronize do
+ @exceptions ||= []
+ @exceptions << [e, name]
+ end
+ end
+
def reporting_thread name
if $opts[:no_threads]
yield
@@ -59,14 +70,13 @@ module Redwood
begin
yield
rescue Exception => e
- $exceptions ||= []
- $exceptions << [e, name]
- raise
+ record_exception e, name
end
end
end
end
- module_function :reporting_thread
+
+ module_function :reporting_thread, :record_exception, :exceptions
## one-stop shop for yamliciousness
def save_yaml_obj object, fn, safe=false