Class: LlmCostTracker::Ingestion::LeaseClaim

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/ingestion/lease_claim.rb

Constant Summary collapse

LEASE_NAME =
"default"

Instance Method Summary collapse

Constructor Details

#initialize(identity:, seconds:) ⇒ LeaseClaim

Returns a new instance of LeaseClaim.



8
9
10
11
# File 'lib/llm_cost_tracker/ingestion/lease_claim.rb', line 8

def initialize(identity:, seconds:)
  @identity = identity
  @seconds = seconds
end

Instance Method Details

#acquireObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/llm_cost_tracker/ingestion/lease_claim.rb', line 13

def acquire
  now = Time.now.utc
  LlmCostTracker::Ingestion::Lease.transaction do
    lease = LlmCostTracker::Ingestion::Lease.lock.find_by(name: LEASE_NAME)
    lease ||= LlmCostTracker::Ingestion::Lease.create!(name: LEASE_NAME)
    next false unless available?(lease, now)

    lease.update!(locked_by: identity, locked_until: now + seconds)
    true
  end
rescue ActiveRecord::RecordNotUnique
  false
end