Class: CustomerCreditSyncWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::IterableJob, Sidekiq::Job
Defined in:
app/workers/customer_credit_sync_worker.rb

Overview

Recomputes each customer's available credit from their credit limit and
locked credit. Scheduled nightly as synchronize_credit_limits
(config/sidekiq_production_schedule.yml); can also be invoked with a specific
set of customer ids for a targeted re-sync.

Uses Sidekiq::IterableJob so progress is checkpointed after every customer —
a mid-run deploy or worker restart resumes from the last processed customer
instead of re-sweeping the whole table from scratch. CustomerFinancials#sync_credit_limit
is a pure recompute (no external calls), so re-processing a customer on resume
is idempotent.

Examples:

Nightly full sweep (scheduled, no args)

CustomerCreditSyncWorker.perform_async

Targeted re-sync

CustomerCreditSyncWorker.perform_async([customer_id, ...])

Instance Method Summary collapse

Instance Method Details

#build_enumerator(customer_ids = [], cursor:) ⇒ Enumerator

Build the resumable enumerator over customers to sync.

Parameters:

  • customer_ids (Array<Integer>) (defaults to: [])

    optional subset; empty syncs every
    customer with a positive credit limit

  • cursor (Object, nil)

    Sidekiq-managed resume cursor

Returns:

  • (Enumerator)

    yields (customer, cursor) pairs



29
30
31
32
33
# File 'app/workers/customer_credit_sync_worker.rb', line 29

def build_enumerator(customer_ids = [], cursor:)
  scope = Customer.where(Customer[:credit_limit].gt(0))
  scope = scope.where(id: customer_ids) if customer_ids.present?
  active_record_records_enumerator(scope, cursor: cursor)
end

#each_iteration(customer, *_args) ⇒ void

This method returns an undefined value.

Parameters:

  • customer (Customer)

    the record for this iteration

  • _args (Array<Object>)

    extra perform_async args forwarded by
    Sidekiq::IterableJob; unused here



39
40
41
42
# File 'app/workers/customer_credit_sync_worker.rb', line 39

def each_iteration(customer, *_args)
  customer.sync_credit_limit
  @synced_count = @synced_count.to_i + 1
end

#on_completeObject



44
45
46
# File 'app/workers/customer_credit_sync_worker.rb', line 44

def on_complete
  Rails.logger.info("[CustomerCreditSyncWorker] Synced credit for #{@synced_count.to_i} customer(s) this run")
end