hyltqa
Last Updated: February 25, 2016
·
229
· beck03076

Batch Processing

def process(p,store)
  begin  
    cat_img_list = p.fetch_image_paths    
    p.catalog_images = cat_img_list unless cat_img_list.empty?    
    p.save!    
    @s_count += 1        
  rescue Exception => e    
    @errors[p[:product_id]] = e          
    @f_count += 1    
    print "x(#{p[:product_id]})"    
  end    
end  


def batch_process(products,store,batch_size = 100,start_from = 1,alert = 10)
  @s_count = 0  
  @f_count = 0  
  @errors = {}  
  print "Started batch process =>"  
  products.each_slice(batch_size).with_index {|(*batch),batch_index|  
   batch.each_with_index do |p,item_index|    
      process(p,store) if batch_index + 1 >= start_from      
      print (((item_index + 1) % alert).zero? ? (item_index + 1) : ".")            
   end        
   puts "\n== #{(batch_index + 1).ordinalize} Batch Over - Success Count - #{@s_count} - Failure Count - #{@f_count} =="
  }  
  puts "\nGod bless you, @errors hash contains all your errors"  
end