From 1da85d1e041eb7b1a998f52e104cc65807c045ba Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Wed, 18 Jan 2012 11:00:01 -0500 Subject: [PATCH] add debug logging and find questions in batches of 3 --- lib/audit_logger.rb | 6 ++++++ lib/tasks/test_api.rake | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 lib/audit_logger.rb diff --git a/lib/audit_logger.rb b/lib/audit_logger.rb new file mode 100644 index 0000000..02a7e2a --- /dev/null +++ b/lib/audit_logger.rb @@ -0,0 +1,6 @@ +class AuditLogger < Logger + def format_message(severity, timestamp, progname, msg) + pid, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{Process::pid}"`.chomp.split(/\s+/).map {|s| s.strip.to_i} + "[#{timestamp.to_formatted_s(:db)}] [#{severity}] [#{size} KB] #{msg}\n" + end +end diff --git a/lib/tasks/test_api.rake b/lib/tasks/test_api.rake index fcb111e..65a653c 100644 --- a/lib/tasks/test_api.rake +++ b/lib/tasks/test_api.rake @@ -100,14 +100,16 @@ namespace :test_api do desc "Description here" task(:question_vote_consistency => :environment) do - questions = Question.find(:all) errors = [] successes = [] - questions.each do |question| + Question.find_each(:batch_size => 3) do |question| + debug("Starting tasks for question #{question.id}") @question_tasks.each do |taskname, description| + debug("Starting task #{taskname} for question #{question.id}") message, error_occurred = send(taskname, question) + debug("Completed task #{taskname} for question #{question.id}") if error_occurred errors << message else @@ -115,6 +117,7 @@ namespace :test_api do end end + debug("Starting choices tasks for question #{question.id}") question.choices.each do |choice| @choice_tasks.each do |taskname, description| message, error_occurred = send(taskname, choice) @@ -125,11 +128,14 @@ namespace :test_api do end end end + debug("Completed choices tasks for question #{question.id}") end @global_tasks.each do |taskname, description| + debug("Starting global task #{taskname}") message, error_occurred = send(taskname) + debug("Completed global task #{taskname}") if error_occurred errors << message else @@ -137,7 +143,7 @@ namespace :test_api do end end - email_text = "Conducted the following tests on API data and found the following results\n" + "For each of the #{questions.length} questions in the database: \n" + email_text = "Conducted the following tests on API data and found the following results\n" + "For each of the #{Question.all.count} questions in the database: \n" errors.each do |e| email_text += " Test FAILED:\n" + e + "\n" end @@ -512,3 +518,11 @@ def cleanup_args(args) end a end + +def debug(message) + return unless ENV['debug'] == 'true' + if defined?(Rails) + logger = AuditLogger.new(STDOUT) + logger.info(message) + end +end -- libgit2 0.21.2