Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Ruby)  »  file open failure test...
Page 1 of 1    

file open failure test...

Author Message
Derek Smith...
Posted: Mon Oct 26, 2009 5:10 am
Guest
Hi All,

I want to test if a file open failed, yet I cannot get this to work.

attempt 1:
File.open( "#{RUNLOG}", "a" ) do |file| or raise StandardError ...


attempt 2
File.open( "#{RUNLOG}", "a" ) do |file|
code here...


or raise StabdardError ...
end


thank you!
--
Posted via http://www.ruby-forum.com/.
 
Derek Smith...
Posted: Mon Oct 26, 2009 10:56 pm
Guest

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/.
 
Simon Krahnke...
Posted: Sun Nov 01, 2009 11:57 pm
Guest
* Robert Klemme <shortcutter at (no spam) googlemail.com> (2009-10-31) schrieb:

Quote:
On 10/31/2009 10:02 AM, Simon Krahnke wrote:
* Robert Klemme <shortcutter at (no spam) googlemail.com> (2009-10-27) schrieb:

Just to play around with ruby 1.9:

DAYS = %w{Sun Mon Tue} # ...

You can construct the hash doing the opposite translation with:

DAYS_TO_I = DAYS.each_with_index.inject({}) { | h, (v, i) | h[v] = i; h }.freeze

Any more simple way to do that?

Not that I'm aware of. With a Hash you could use #invert but that is
not available for Array it seems.

There's Hash#invert? Never saw that before, thank you.

As usual, the Pickaxe is more descriptive than rdoc, the first mentions
that if multiple keys map to the same value, only one of the keys
survives the inversion.

mfg, simon .... l
 
 
Page 1 of 1    
All times are GMT
The time now is Sun Nov 29, 2009 3:11 am