From: William Morgan Date: Wed, 4 Jun 2008 18:53:15 +0000 (-0700) Subject: exception cleanup: synchronize access, and require fastthread X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=7f2af9ac8bc70ff41b0dc64eccd068118abe19f2;p=sup exception cleanup: synchronize access, and require fastthread --- diff --git a/bin/sup b/bin/sup index 723b1ed..eb45461 100644 --- 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 < 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 index 9e90267..5211eec 100644 --- 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