I have been banging out feeds for month now. Literally months. So I might as well share some of my rake task code, even though I do plan on refactoring the whole feed piece soon.
The problem is, I finally have enough info about our disparate relationships to finally reconstruct the feeds as different objects using a mix-in module to keep the guts of it straight; up until now I have just refactored the existing function calls to deal with new accounts, etc., but that only works for so long. When I built the original object structure we only had one real 'activator' account, and now we have 5. So finally I have enough info to abstract realistically about what will change (mix-in) and what won't (proprietary objects). I am also excited to start running some cucumber tests against this whole set up except for one problem - I have been bogged down on routine support - support, mind you, not even maintenance - and this is becoming a problem. But I suppose that is a topic for another post.
Here is the task, for one of our 5 main accounts:
task :rerun_us do
#cleared out this task and 'rerun_blackhawk' because they were not used and causing confusion
@accounts = Account.get_blackhawk
#the last 3 days
#@runcodes = x_days_runcodes(3)
@accounts.each do |this_account|
#@runcodes.each do |try_this_code|
try_this_code = "20091206"
begin
#must run cleanup first! DELETE both Sales and Reds AND SUMMARIES
# also delete INBOUND records at the end (both of them!)
Summary.find(:all, :conditions => ["account_id=? and run_code=?", this_account.id, try_this_code]).each do |sum_for_delete|
puts "delete sum #{sum_for_delete.id} \n"
sum_for_delete.destroy
end
if this_account.inbounds.find(:first, :conditions => ["run_code=? and rec_type=?", try_this_code, "Sales"]) then
this_account.inbounds.find(:first, :conditions => ["run_code=? and rec_type=?", try_this_code, "Sales"]).sales.each do |sale_for_delete|
if sale_for_delete.redemption then
puts "delete red #{sale_for_delete.redemption.id} \n"
sale_for_delete.redemption.destroy
end
puts "delete sale #{sale_for_delete.id} \n"
sale_for_delete.destroy
end
this_account.inbounds.find(:all, :conditions => ["run_code=?", try_this_code]).each do |inbound_to_delete|
puts "delete inbound #{inbound_to_delete.id} \n"
inbound_to_delete.destroy
end
end
#cleanup done
puts "cleanup done \n"
message = parse_bh_account_from_file(this_account, this_account.bh_file_code_sal(try_this_code), try_this_code, true, true, false)
puts "read message\n"
if message
puts message
log_message(message, this_account.id, try_this_code)
else
puts "skip existing upload. #{this_account.partner.name} #{try_this_code}"
end
puts "parse reds\n"
message = parse_bh_account_from_file(this_account, this_account.bh_file_code_red(try_this_code), try_this_code, false, true, false)
if message
puts message
log_message(message, this_account.id, try_this_code)
else
puts "skip existing upload. #{this_account.partner.name} #{try_this_code}"
end
rescue => ex
puts "error\n#{ex.message} "
end
#end
end
end
I will proceed to grade my own work.
So as you can see:
1 - I have configured this temporarily to run a single run code. Normally it would run a single range of runcodes as a result of x_days_runcodes(x). I may get a little less than full credit here, for not having fully meta programmed this one.
x_days_runcodes(x) should really be: 3_days_runcodes, 4_days_runcodes, etc all from a single def.
2 - I think I get more or less full credit for moving most of the business logic into the Models (MVC). I think this task reads relatively well.
3 - I think it decently documented.
4 - I think it is possible to do a little better with ActiveRecord. This is some pretty vanilla RDBM kind of stuff, so it would be ripe for some caching and call reduction if it was a high traffic procedure. As a single standing daily cron it has not shown me any problems so far.
Anyway, maybe that will be helpful to somebody. Soon I will write a little about my experiences with New Relic RPM so far.






