rails rollback updates when task fails
- by ash34
Hi,
I have the following "generate_report" method being called from a rake task, which gets a hash as an input, that contains the reported hours spent by each user on a task and outputs the data as a .csv report.
desc "Task reporting"
task :report, [:inp_dt] => [:environment] do |t, args|
h = select_data(args.inp_dt) /* not shown here */
generate_report(h)
end
def generate_report(h)
out_dir = File.dirname(__FILE__) + '/../../output'
myfile = "#{out_dir}" + "/monthly_#{Date.today.strftime("%m%d%Y")}.csv"
writer = CSV.open(myfile, 'w')
h.each do |h,v|
v.each do |key,val|
writer << val
end
end
writer.close
end
where h =
{:BILL=>{:PROJA=>["CYR", "00876", "2", 24], :PROJB=>["EPR", "00876", "2", 16]}, :JANE=>{:PROJA=>["TRB", "049576", "2", 16]}}
I would like to set/update a 'processed' flag for each reported transaction and only commit the update when the file is written correctly or rollback the updates when the task fails. How can I accomplish this.
thanks,
ash