Class: LlmCostTracker::Storage::ActiveRecordIngestorLease

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/storage/active_record_ingestor_lease.rb

Constant Summary collapse

LEASE_NAME =
"default"

Instance Method Summary collapse

Constructor Details

#initialize(identity:, seconds:) ⇒ ActiveRecordIngestorLease

Returns a new instance of ActiveRecordIngestorLease.



10
11
12
13
# File 'lib/llm_cost_tracker/storage/active_record_ingestor_lease.rb', line 10

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

Instance Method Details

#acquireObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm_cost_tracker/storage/active_record_ingestor_lease.rb', line 15

def acquire
  now = Time.now.utc
  LlmCostTracker::IngestorLease.transaction do
    lease = LlmCostTracker::IngestorLease.lock.find_by(name: LEASE_NAME)
    lease ||= LlmCostTracker::IngestorLease.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