here is my code
def get_usage host = Rails.application.secrets.sonet_sftp_host account = Rails.application.secrets.sonet_sftp_account result_repo = "/home/jpmb_mobile/commreport/volume" Net::SFTP.start(host, account) do |sftp| sftp.dir.glob(result_repo, "REPORT_MOBILE_VOLUME*").each do |file| puts file.name date = file.name.gsub(/REPORT_MOBILE_VOLUME_/, '')[0..7].to_date time = file.name.gsub(/REPORT_MOBILE_VOLUME_/, '')[8..9] last_usage = SonetUsage.last next if last_usage.present? and ( last_usage.usage_date > date or (last_usage.usage_date == date and last_usage.usage_time.to_i >= time.to_i)) file_data = sftp.download!("#{result_repo}/#{file.name}") CSV.parse(file_data, headers: false, col_sep: ",") do |row| tel_no = '0'+ row[0].gsub(/^81/, '') today_usage_high = row[1].to_i yesterday_usage_high = row[2].to_i before_yesterday_usage_high = row[3].to_i today_usage_low = row[4].to_i - row[1].to_i yesterday_usage_low = row[5].to_i - row[2].to_i before_yesterday_usage_low = row[6].to_i - row[3].to_i puts tel_no card = SonetCard.where(tel_no: tel_no, status: [:activating, :stop, :cancelled, :cancelling]).last if card.nil? puts "not found!" next end before_yesterday_records = SonetUsage.where(sonet_card: card, usage_date: date - 2.days) yesterday_records = SonetUsage.where(sonet_card: card, usage_date: date - 1.days) today_records = SonetUsage.where(sonet_card: card, usage_date: date) if (before_yesterday_records.empty? or before_yesterday_records.last.usage_time != "24") and (before_yesterday_usage_high != 0 or before_yesterday_usage_low != 0) by_usage = SonetUsage.find_or_create_by(sonet_card: card, usage_date: date - 2.days, usage_time: "24") by_usage.update(total_bytes_high: before_yesterday_usage_high - before_yesterday_records.try("sum", :total_bytes_high).to_i, total_bytes_low: before_yesterday_usage_low - before_yesterday_records.try("sum", :total_bytes_low).to_i) end if (yesterday_records.empty? or yesterday_records.last.usage_time != "24") and (yesterday_usage_high != 0 or yesterday_usage_low != 0) y_usage = SonetUsage.find_or_create_by(sonet_card: card, usage_date: date - 1.days, usage_time: "24") y_usage.update(total_bytes_low: yesterday_usage_low - yesterday_records.try("sum", :total_bytes_low).to_i, total_bytes_high: yesterday_usage_high - yesterday_records.try("sum", :total_bytes_high).to_i) end if time != "00" and ( today_usage_high != 0 or today_usage_low != 0 ) and (today_records.empty? or today_records.last.usage_time != time) t_usage = SonetUsage.find_or_create_by(sonet_card: card, usage_date: date, usage_time: time) t_usage.update(total_bytes_low: today_usage_low - today_records.try("sum", :total_bytes_low).to_i, total_bytes_high: today_usage_high - today_records.try("sum", :total_bytes_high).to_i) end end end end end
Am getting this IOError: closed stream for SFTP Download in Ruby on Rails constantly,I don't have any idea how to fix this.I even tried to give StringIO.new but it is not working as expectedHow can I complete my action.Please help?