rails rollback updates when task fails

Posted by ash34 on Stack Overflow See other posts from Stack Overflow or by ash34
Published on 2010-05-21T20:18:04Z Indexed on 2010/05/21 20:20 UTC
Read the original article Hit count: 230

Filed under:

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

© Stack Overflow or respective owner

Related posts about ruby-on-rails