Can someone take from here? Tests are failing, there is some issue. We have a whole week meeting next week, I am not sure if I will find time to work on this.
def self.expire(conditions = {}, chunk_size = 1000, sleep_time = 0.2)
timerange = conditions[:timerange] || 1.week
status = conditions[:status]
cond = "created_at < \'#{(Time.now.utc - timerange).to_formatted_s(:db)}\'"
cond += " and status = #{status}" unless status.nil?
total_count = 0
report_ids = []
while true
Report.transaction do
report_ids = Report.unscoped.where(cond).reorder('').limit(chunk_size).pluck(:id)
if report_ids.count > 0
log_count = Log.unscoped.where(:report_id => report_ids).reorder('').delete_all
message_count = Message.unscoped.joins(:logs).where("logs.report_id" => report_ids).delete_all
source_count = Source.unscoped.joins(:logs).where("logs.report_id" => report_ids).delete_all
count = Report.unscoped.where(:id => report_ids).delete_all
logger.info "expired #{count} #{to_s.underscore.humanize.pluralize} (M:#{message_count}, S:#{source_count}, L:#{log_count})"
total_count += count
end
end
break if (report_ids.nil? || report_ids.empty?)
sleep sleep_time
end
logger.info "expired #{total_count} total records of #{to_s.underscore.humanize.pluralize}"
count
end
Edit: Repasted better version - still does not work.