Class: SpreeCmCommissioner::OauthAccessTokens::CleanupExpired

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

Constant Summary collapse

BATCH_SIZE =
500
EXPIRATION_THRESHOLD_DAYS =

The access tokens set by doorkeeper to expire in 1 day (cm-market-server/config/initializers/doorkeeper.rb), so we set the threshold to 90 days to make sure all expired tokens are cleaned up.

90

Instance Method Summary collapse

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/spree_cm_commissioner/oauth_access_tokens/cleanup_expired.rb', line 12

def call
  cutoff_time = EXPIRATION_THRESHOLD_DAYS.days.ago
  total_deleted = 0

  # oauth_access_tokens is a standalone table with no foreign keys or associations
  # pointing to it (checked cm-market-server/db/schema.rb), so it is safe to use
  # delete_all here for faster bulk cleanup.
  Spree::OauthAccessToken
    .where('created_at < ?', cutoff_time)
    .in_batches(of: BATCH_SIZE) do |relation|
    deleted_count = relation.delete_all
    total_deleted += deleted_count
  end

  log_cleanup_result(total_deleted, cutoff_time)
  success(total_deleted: total_deleted, cutoff_time: cutoff_time, batch_size: BATCH_SIZE, expiration_threshold_days: EXPIRATION_THRESHOLD_DAYS)
rescue StandardError => e
  log_error(e)
  failure(nil, e.message)
end