Skip to content

Instantly share code, notes, and snippets.

@richardking
Created February 13, 2014 22:29
Show Gist options
  • Select an option

  • Save richardking/8985277 to your computer and use it in GitHub Desktop.

Select an option

Save richardking/8985277 to your computer and use it in GitHub Desktop.
delete with stagger
trap(:SIGHUP) { reload }
@@stagger = 0
def reload
path = "stagger.txt"
if File.exist?(path)
@@stagger = File.read(path).strip.to_i
puts "@@stagger reloaded with #{@@stagger}"
else
puts "no stagger.txt file found, keeping @@stagger at #{@@stagger}"
end
end
def execute(sql)
Tweet.connection.execute(sql)
end
Tweet.find_in_batches(:conditions => ["created_at < ?", 1.month.ago]) do |batch|
puts "batch left #{batch.first.id} right #{batch.last.id}"
ids = batch.collect(&:id).join(',')
execute("DELETE FROM tweet_tags WHERE tweet_id IN (#{ids})")
execute("DELETE FROM tweets WHERE id IN (#{ids})")
if @@stagger > 0
puts("Staggering: delaying for #{@@stagger} seconds before next batch insert")
sleep(@@stagger)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment