Class: Assistant::TechnicalSupport::ResponseGroundingAssessmentLease
- Inherits:
-
Object
- Object
- Assistant::TechnicalSupport::ResponseGroundingAssessmentLease
- Includes:
- Service
- Defined in:
- app/services/assistant/technical_support/response_grounding_assessment_lease.rb
Overview
Atomically claims one pending grounding assessment before provider work.
The bounded lease lets cleanup recover a worker lost to a deploy or crash.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- PENDING_REASON =
'awaiting_async_assessment'- PROCESSING_REASON =
'assessment_in_progress'- LEASE_DURATION =
2.minutes
Class Method Summary collapse
-
.active?(assessment) ⇒ Boolean
Reports whether a processing lease is still within its ownership window.
-
.expired?(value) ⇒ Boolean
Reports whether a persisted lease has expired or cannot be parsed.
-
.finalize(assistant_message:, lease_token:, assessment:) ⇒ Hash
Finalizes only when the caller still owns the current processing lease.
Instance Method Summary collapse
-
#call ⇒ Result
Claims a pending or expired assessment under the message row lock.
Methods included from Service
#attributes_used, #build_result, call, #logger
Methods included from Service::Initializer
Class Method Details
.active?(assessment) ⇒ Boolean
Reports whether a processing lease is still within its ownership window.
59 60 61 62 |
# File 'app/services/assistant/technical_support/response_grounding_assessment_lease.rb', line 59 def active?(assessment) data = assessment.to_h.stringify_keys data['reason'] == PROCESSING_REASON && !expired?(data['lease_expires_at']) end |
.expired?(value) ⇒ Boolean
Reports whether a persisted lease has expired or cannot be parsed.
84 85 86 87 88 89 90 |
# File 'app/services/assistant/technical_support/response_grounding_assessment_lease.rb', line 84 def expired?(value) return true if value.blank? Time.iso8601(value) <= Time.current rescue ArgumentError, TypeError true end |
.finalize(assistant_message:, lease_token:, assessment:) ⇒ Hash
Finalizes only when the caller still owns the current processing lease.
70 71 72 73 74 75 76 77 78 |
# File 'app/services/assistant/technical_support/response_grounding_assessment_lease.rb', line 70 def finalize(assistant_message:, lease_token:, assessment:) .with_lock do current = .reload.grounding_assessment.to_h.stringify_keys next current unless owned_by?(current, lease_token) .update_column(:grounding_assessment, assessment) assessment end end |
Instance Method Details
#call ⇒ Result
Claims a pending or expired assessment under the message row lock.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/assistant/technical_support/response_grounding_assessment_lease.rb', line 35 def call claimed_assessment = nil .with_lock do .reload current = .grounding_assessment.to_h.stringify_keys next unless claimable?(current) claimed_assessment = current.merge(lease_attributes) .update_column(:grounding_assessment, claimed_assessment) end Result.new( claimed: claimed_assessment.present?, assessment: claimed_assessment, lease_token: claimed_assessment&.fetch('lease_token', nil) ) end |