Last active
August 29, 2015 13:57
-
-
Save wiseleyb/9920795 to your computer and use it in GitHub Desktop.
This file contains 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
def run_action_credit_updates(batch, idx) | |
report_size = 10_000 | |
batch.each_with_index do |b, i| | |
if i > 0 && i % report_size == 0 | |
puts "Batch #{idx} : #{i} (#{ 100 - ((report_size/i.to_f) * 100).to_i }%)" | |
end | |
b.set_source_data | |
end | |
puts "Batch #{idx} : Done" | |
end | |
def update_action_credits | |
idx = 0 | |
thecount = 0 | |
threads = [] | |
ActionCredit. | |
joins('LEFT OUTER JOIN action_credit_source_data ' + | |
'ON action_credits.id = action_credit_source_data.action_credit_id'). | |
where('action_credit_source_data.id IS NULL'). | |
where('action_credits.action in ("petition", "petition_signing")'). | |
find_in_batches(batch_size: 200_000) do |batch| | |
thecount += batch.size | |
puts "#{batch.size} / #{thecount}" | |
idx += 1 | |
threads << Thread.new{ run_action_credit_updates(batch, idx) } | |
end | |
threads.each_with_index do |t, idx| | |
puts "Joining thread #{idx}" | |
t.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment