Skip to content

Instantly share code, notes, and snippets.

@wiseleyb
Created April 16, 2014 20:05
Show Gist options
  • Save wiseleyb/10927473 to your computer and use it in GitHub Desktop.
Save wiseleyb/10927473 to your computer and use it in GitHub Desktop.
def to_h_time(seconds_diff)
hours = seconds_diff / 3600
seconds_diff -= hours * 3600
minutes = seconds_diff / 60
seconds_diff -= minutes * 60
seconds = seconds_diff
"#{hours.to_i.to_s.rjust(2, '0')}:#{minutes.to_i.to_s.rjust(2, '0')}:#{seconds.to_i.to_s.rjust(2, '0')}"
end
def status(step, steps)
@start_time ||= Time.now
puts "#{step}/#{steps} #{((step/steps.to_f) * 100).to_i}% Time left #{to_h_time(((Time.now - @start_time)/(step+1)) * steps)}"
end
def run_update(klass, sql_template, split_size: 5_000, sleep_time: 0.5, steps_between_sleep: 3)
ac_max_id = 820828449 # klass.maximum(:id)
steps = (ac_max_id / (split_size * steps_between_sleep)) + 1
hack = 0
id_count = 0
steps.times do |i|
status(i, steps)
steps_between_sleep.times do
sql = sql_template.
gsub('{from_id}', (id_count * split_size).to_s).
gsub('{to_id}', ((id_count * split_size) + split_size).to_s)
puts " #{sql}"
id_count += 1
end
sleep(0.5)
hack += 1
break if hack > 100
end
end
@start_time = Time.now
run_update(ActionCredit, "update action_credits set asdf where id >= {from_id} and id < {to_id};")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment