Class: SpreeCmCommissioner::Seeds::UserUsernames

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/seeds/user_usernames.rb

Instance Method Summary collapse

Instance Method Details

#call(batch_size: 1000) ⇒ Object



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
       = SpreeCmCommissioner::Users::Username::Generate.call(user: user)

      if user.update_column(:login, ) # rubocop:disable Rails/SkipsModelValidations
        updated_count += 1

        # Log progress every 100 users to avoid log spam
        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