jeudi 22 septembre 2016

Why is find_in_batches running again after completion in Rails 3.2, ruby 1.9.3?

How to do I mass update using find_in_batches when there are around 1 million records where it should take a condition and update those records alone.

# For testing in my local, here are the DB stats,
# Total no of users => 2000,
# Total no of users with name field having nil value => 1,
# Total no of users with name field present => 1999,
# Objective=> Use find_in_batches to run a batch generation of name field for all users who do not have name field.
# Expected Result => Should generate name field for that 1 record and exit from batch_update code
# Obtained Result => Its updating that 1 user's name and then again its running for all 2000 users.

In production this could lot of trouble as I have million records.

I have tried this out:

 task :add_name_to_all_users => :environment do
   i=0
   puts "started at :#{Time.now}|Batch Size: 12"
   Name.where("name is null").find_in_batches(batch_size: 12) do |names|
     sleep(2)
     names.each {|n| n.save!; i+=1;} 
     puts "updated #{i} records at: #{Time.now}"
   end
   puts "Completed the Task at: #{Time.now}"
 end

This is what I get in console,

started at :2016-09-22 10:33:18 +0530|Batch Size: 12
updated 1 records at: 2016-09-22 10:33:21 +0530
Completed the Task at: 2016-09-22 10:33:21 +0530
started at :2016-09-22 10:33:21 +0530|Batch Size: 12
updated 12 records at: 2016-09-22 10:33:27 +0530
updated 24 records at: 2016-09-22 10:33:33 +0530

My concern: Why is it starting again after this line:

Completed the Task at: 2016-09-22 10:33:21 +0530

The above code works but it keeps running till it has looped through all the records. The where method is not working for me. And the loop is starting again after updating that one record which had name as nil.

Aucun commentaire:

Enregistrer un commentaire