6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/services/spree_cm_commissioner/seeds/user_usernames.rb', line 6
def call(batch_size: 1000)
users = Spree::User.all
total_count = users.count
updated_count = 0
failed_count = 0
current_index = 0
Rails.logger.debug { "\nš Starting login update for #{total_count} users (batch size: #{batch_size})..." }
start_time = Time.current
users.find_each(batch_size: batch_size) do |user|
current_index += 1
begin
new_login = SpreeCmCommissioner::Users::Username::Generate.call(user: user)
if user.update_column(:login, new_login) updated_count += 1
if (current_index % 100).zero? || current_index == total_count
elapsed = (Time.current - start_time).round(2)
rate = (current_index / elapsed).round(2)
Rails.logger.debug { "[#{current_index}/#{total_count}] ā Updated #{updated_count}, Failed #{failed_count} (#{rate} users/sec)" }
end
else
failed_count += 1
Rails.logger.warn { "[#{current_index}/#{total_count}] ā User ##{user.id}: Failed to update" }
end
rescue StandardError => e
failed_count += 1
Rails.logger.error { "[#{current_index}/#{total_count}] ā User ##{user.id}: #{e.message}" }
end
end
elapsed = (Time.current - start_time).round(2)
Rails.logger.debug { "\nā
Completed in #{elapsed}s! Updated #{updated_count}/#{total_count} users (Failed: #{failed_count})" }
end
|