module Conscriptor
this will include hierarchical context ONLY when you log. so you can add parents and grandparents of a lesson, and only when you call “info” will those print out.
Allows you to track layers of context, e.g. multiple levels of iteration schools -> conference_reports -> widgets.
Historical context: there's a bunch of code in lesson importing that was managing hierarchical logging before this code was born, and we could probably use this here.
Constants
- VERSION
Public Instance Methods
app_backtrace(exception)
click to toggle source
# File lib/conscriptor.rb, line 43 def app_backtrace(exception) bc = ActiveSupport::BacktraceCleaner.new bc.clean(exception.backtrace) end
catch_exceptions(message='') { || ... }
click to toggle source
# File lib/conscriptor.rb, line 53 def catch_exceptions(message='') yield rescue StandardError print message.red print_backtrace end
clog()
click to toggle source
# File lib/conscriptor.rb, line 12 def clog @clog ||= Conscriptor::HierarchicalContextLogger.new end
commit_or_rollback()
click to toggle source
# File lib/conscriptor.rb, line 60 def commit_or_rollback puts "\nCommit? (y/n)" if $stdin.gets.chomp == 'y' puts 'Committing.' else puts 'Rolling Back.' raise ActiveRecord::Rollback end end
counts()
click to toggle source
# File lib/conscriptor.rb, line 16 def counts $counts ||= Conscriptor::EventCounter.new # rubocop:disable Style/GlobalVars end
inc_timer(by: 1)
click to toggle source
# File lib/conscriptor.rb, line 95 def inc_timer(by: 1) @progress.inc(by: by) end
num_lines(file)
click to toggle source
lines in a file
# File lib/conscriptor.rb, line 100 def num_lines(file) `wc -l < #{file}`.to_i end
print_error()
click to toggle source
# File lib/conscriptor.rb, line 48 def print_error puts $ERROR_INFO.message.red puts app_backtrace($ERROR_INFO).map { |b| " #{b}" }.join("\n").red end
print_times()
click to toggle source
# File lib/conscriptor.rb, line 82 def print_times puts 'Printing Times'.bold @times.each do |name, times| puts " #{name} (#{times.count} times) : #{times.sum / times.count}s (avg)" end end
safe_transaction() { || ... }
click to toggle source
# File lib/conscriptor.rb, line 20 def safe_transaction ActiveRecord::Base.transaction do begin yield say "job's done" rescue StandardError say 'nope' raise end counts.dump commit_or_rollback end end
save_even_with_errors(obj)
click to toggle source
save an active record, even if it has errors
# File lib/conscriptor.rb, line 36 def save_even_with_errors(obj) return if obj.save counts.record_and_print('!'.yellow, "Error saving: #{obj.errors.full_messages.join(',')}") obj.save validate: false end
start_timer(name: nil, total: nil, report_every: nil, logger: nil)
click to toggle source
usage: start_timer
total: Lesson.count, report_every: 100
# File lib/conscriptor.rb, line 91 def start_timer(name: nil, total: nil, report_every: nil, logger: nil) @progress = Conscriptor::ProgressReporter.new(name: name, total: total, report_every: report_every, logger: logger) end
time(name) { || ... }
click to toggle source
# File lib/conscriptor.rb, line 71 def time(name) start = Time.now retval = nil begin retval = yield ensure ((@times ||= {})[name] ||= []) << Time.now - start end retval end