Class: SpreeCmCommissioner::Integrations::CleanupSyncSessions

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

Constant Summary collapse

DEFAULT_CUTOFF_DAYS =
30
BATCH_SIZE =
500

Instance Method Summary collapse

Instance Method Details

#call(cutoff_days: DEFAULT_CUTOFF_DAYS, batch_size: BATCH_SIZE) ⇒ Object

Deletes IntegrationSyncSession records older than the cutoff (default: 30 days). Runs in batches to avoid long-running single queries.



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/integrations/cleanup_sync_sessions.rb', line 11

def call(cutoff_days: DEFAULT_CUTOFF_DAYS, batch_size: BATCH_SIZE)
  cutoff_time = cutoff_days.days.ago
  deleted_count = 0

  SpreeCmCommissioner::IntegrationSyncSession
    .where('created_at < ?', cutoff_time)
    .in_batches(of: batch_size) do |relation|
      deleted_count += relation.delete_all
    end

  CmAppLogger.log(
    label: 'SpreeCmCommissioner::Integrations::CleanupSyncSessions#call completed',
    data: {
      cutoff_days: cutoff_days,
      batch_size: batch_size,
      deleted_count: deleted_count
    }
  )

  success(deleted_count: deleted_count, cutoff_days: cutoff_days)
rescue StandardError => e
  CmAppLogger.error(
    label: 'SpreeCmCommissioner::Integrations::CleanupSyncSessions#call failed',
    data: {
      cutoff_days: cutoff_days,
      batch_size: batch_size,
      error_class: e.class.name,
      error_message: e.message,
      backtrace: e.backtrace&.first(5)&.join("\n")
    }
  )

  failure(nil, e.message)
end