Created
February 13, 2014 22:29
-
-
Save richardking/8985277 to your computer and use it in GitHub Desktop.
delete with stagger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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