Class: Xeno::Dedup

Inherits:
ApplicationRecord show all
Defined in:
app/models/xeno/dedup.rb

Overview

The at-least-once dedup ledger. Redelivered work (a cron tick fired twice, a Slack retry storm) claims its (scope, key) once; every other delivery loses the unique-index race and can read what the winner recorded in metadata.

Class Method Summary collapse

Class Method Details

.claim(scope, key, metadata: {}) ⇒ Object

Returns the freshly claimed row, or nil when the key was already claimed. Safe inside enclosing transactions (savepoint), safe under concurrency (the unique index decides).



12
13
14
15
16
17
18
# File 'app/models/xeno/dedup.rb', line 12

def self.claim(scope, key, metadata: {})
  transaction(requires_new: true) do
    create!(scope: scope, key: key, metadata: )
  end
rescue ActiveRecord::RecordNotUnique
  nil
end

.existing(scope, key) ⇒ Object



20
21
22
# File 'app/models/xeno/dedup.rb', line 20

def self.existing(scope, key)
  find_by(scope: scope, key: key)
end