From: Derek Smith Date: 2009-10-27T03:56:57+09:00 Subject: Re: file open failure test > > http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html > > Kind regards > > robert OK so in relation, I want to have a method for whose responsibility is logging runtime errors such as failures to open or run OS commands. However, when I intentionally made it fail, like putting in a non OS command it does not log? Any help on this or best practices? thx! For example: #!/usr/bin/env ruby -w require 'date' require 'ftools' require 'fileutils' ENV['PATH'] = '/usr/bin:/usr/sbin:/sbin:/bin:/usr/local/bin:/usr/local/vrep' VERSIONS = 14.to_i DB_DIR = "/usr/local/vrep/prod/db" DB_BKUP1 = Dir.glob("#{DB_DIR}/*.gz") DB_BKUP = "#{DB_DIR}/prod_db_bkup_" RUNLOG = "/var/log/prod_db_bkup.log" DBF = Dir.glob("#{DB_DIR}/*.sqlite3") unless File.exists?( "#{RUNLOG}" ) FileUtils.touch( "#{RUNLOG}" ) \ || warn("Touch file failed for /var/log/prod_db_bkup.log!") end d = Date.today t = Time.now h = Hash.new h["Sun"] = 0 h["Mon"] = 1 h["Tue"] = 2 h["Wed"] = 3 h["Thr"] = 4 h["Fri"] = 5 h["Sat"] = 6 def log_mtd(arg) File.open( "#{RUNLOG}", "a" ) do |file| file.puts(arg) end end def db_bkup(arg) Dir.chdir( "#{DB_DIR}" ) \ || die_mtd("Change_Dir_Failed_To_#{DB_DIR}_Dump_Will_Fail!") if DBF.length.to_i < 1 die_mtd("#{DBF}_File_Is_Missing_Dump_Will_Not_Run.+(d.to_s).+(t.hour.to_s)") else %x(echo ".dump"|sqlite3 development.sqlite3 |gzip -c > "#{DB_BKUP.+(arg)}") \ || die_mtd("SQL_Dump_Cmd_Failed.+(d.to_s).+(t.hour.to_s)") end end if DB_BKUP1.length.to_i >= 1 File.open( "#{RUNLOG}", "a" ) do |file| h.each_pair do |key, value| if d.wday == value file.puts '+' 65.times do file.print "=" end file.puts "\n","Beginning day: #{key} DB backup at #{d.to_s} #{t.hour}:#{ t.min}","\n" file.puts "Executing command echo .dump|sqlite3 development.sqlite3 |gzip -c","\n" 65.times do file.print "=" end file.puts "\n",'+' db_bkup(key .+(d.to_s) .+(t.hour.to_s) .+(t.min.to_s)) end end end || die_mtd("#{RUNLOG}_Did_Not_Open_.+(d.to_s).+(t.hour.to_s).+(t.min.to_s)") end -- Posted via http://www.ruby-forum.com/.