Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Trying to run a cron job for the last 60 days of data

ruby rails cron job

  • Please log in to reply
No replies to this topic

#1 kkonovalov

kkonovalov

    Passenger

  • Members
  • 1 posts

Posted 29 July 2014 - 03:07 AM

I have a monthly file that gets sent out to my users, however I forgot
to add a cron job for the month of June.

How would I edit this script to give me the last 60 days of data? I
tried replacing the 7.days with 60 and 120 but it still outputted the
last 30 days only.


task :reinhart_csv => :environment do
job = JobStatus.find_by_command("rake reinhart_csv")
job.update_attribute(:last_step, "Started")

out_file = "reinhart_orders.csv"
reporting_date = Date.today - 7.days
report = Reporting.reinhart_csv(reporting_date)
name_regex = /Card (.*?)$/
%x[cat /dev/null > #{ out_file }]

job.update_attribute(:last_step, "Ran Database query, defined files")
CSV.open(out_file,"w") do |csv|
csv << report[:csv_header]
report[:result].each do |row|
name = row.TF_DESC.to_s.strip.match(name_regex)

if row.SHIP_VIA.strip == "UPS Ground"
ship_via = "Ground"
turnaround = "5"
elsif row.SHIP_VIA.strip == "Q"
ship_via = "2nd Day Air"
turnaround = "2"
elsif row.SHIP_VIA.strip == "Next Day Air" || row.SHIP_VIA.strip
== "Overnight"
ship_via = "Next Day Air"
turnaround = "1"
else
ship_via = row.SHIP_VIA.strip.gsub(',', ' ')
turnaround = "0"
end


unless name.nil?
name = name[1]
else
name = row.TF_DESC
end

line = nil,"Unknown","C5448",nil,nil,"Reinhart Food
Service","0",nil,"Black",
nil,nil,nil,"#{ row.ORDER_DATE }","#{
row.ORDER_NUM.to_s.strip.gsub(',', ' ') +
row.PO_CODE.to_s.strip.gsub(',', ' ') }",
"APPROVED",nil,nil,"#{ row.ORIG_QTY_ORDERED}",nil,"#{
row.ITEM_CODE.strip }",nil,
"#{ row.TF_DESC.to_s.strip.gsub(',', ' ') }",nil,"#{
row.ITEM_CODE.to_s.strip.gsub(',', ' ') }",nil,nil,"#{
row.ITEM_CODE.to_s.strip.gsub(',', '') }",nil,"#{ turnaround }",
nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,"USA",nil,nil,nil,"Commercial
Address",nil,
"SHIPPING DEPT.","1400 S WOLF RD",nil,"60090","Reinhart
Food Service","WHEELING",
"IL","8472157300","1400S60090","ReinhartCards","USA",nil,nil,nil,nil,
"#{ row.ADDRESS_2.to_s.strip.gsub(',', ' ')
}",nil,nil,"NORTHERN PRINT NETWORK",nil,nil,nil,nil,nil,nil,"#{
name.strip.gsub(',', ' ') }",
nil,nil,nil,nil,nil,nil,"#{
row.ORIG_QTY_ORDERED.to_s.strip.gsub(',', ' ') }",nil,"#{
row.ATTENTION.to_s.strip.gsub(',', ' ') }","#{
row.ADDRESS_2.to_s.strip.gsub(',', ' ').gsub('-',' ') }",
nil,"#{ row.CITY.to_s.strip.gsub(',', ' ') }","#{
row.TF_STATE.to_s.strip.gsub(',', ' ') }","Address Below","#{
row.ZIP_CODE.to_s.strip.gsub(',', ' ') }",nil,nil,nil,nil,
"#{ ship_via }",nil,"#{ row.ADDRESS_1.to_s.strip.gsub(',',
' ') }",nil
csv << line
end
end
job.update_attribute(:last_step, "Output file generated")
ReportMailer.reinhart_csv(reporting_date, out_file).deliver
job.update_attribute(:last_step, "SUCCESS!")
job.update_attribute(:last_run, Time.now)
end






Also tagged with one or more of these keywords: ruby, rails, cron job

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users