Skip to content

Instantly share code, notes, and snippets.

@kylev
Created March 13, 2012 18:52
Show Gist options
  • Save kylev/2030717 to your computer and use it in GitHub Desktop.
Save kylev/2030717 to your computer and use it in GitHub Desktop.
timed_loop_batch(0.25, 1000, 100) do |batch_size|
changed = GiantMetric.connection.update_sql("DELETE FROM giant_metrics WHERE created_at <= #{1.month.ago} LIMIT #{batch_size}")
break if changed < batch_size
end
def timed_batch_loop(target_secs, start_size, step, min_size=1, max_size=nil)
current = start_size
loop do
start_time = Time.now
yield current
if Time.now - start_time > target_secs
current = [min_size, current - step].max
else
if max_size
current = [max_size, current + step].min
else
current += step
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment