Class: PartyResearch::AutoEnricher

Inherits:
Object
  • Object
show all
Defined in:
app/services/party_research/auto_enricher.rb

Constant Summary collapse

DEBOUNCE =
6.hours
SETTLE_DELAY =

Wait this long before the worker runs so writes that follow lead
creation in the same session — name cleanup, profile
classification, contact attachment — commit first. Enriching the
instant a guest hits lead_qualify ran against a still-raw record
(placeholder name, default Homeowner profile, no Contact) and
wasted the run.

10.minutes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(party, force: false) ⇒ AutoEnricher

Returns a new instance of AutoEnricher.



37
38
39
40
# File 'app/services/party_research/auto_enricher.rb', line 37

def initialize(party, force: false)
  @party = party
  @force = force
end

Class Method Details

.enqueue(party, force: false) ⇒ Object

Public entry point. Returns the PartyResearchRun that was
enqueued, or nil when skipped (not enrichable / debounced /
already running / nothing configured).

Parameters:

  • force (Boolean) (defaults to: false)

    bypass the time-based debounce — used when a
    material identity change (e.g. a homeowner ↔ non-homeowner
    reclassification) makes a fresh run worthwhile even within the
    debounce window. A forced run still refuses to stack on an
    in-flight one.



33
34
35
# File 'app/services/party_research/auto_enricher.rb', line 33

def self.enqueue(party, force: false)
  new(party, force: force).enqueue
end

Instance Method Details

#enqueueObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/party_research/auto_enricher.rb', line 42

def enqueue
  return nil unless @party.enrichable_via_research?
  return nil if skip_for_existing_run?

  adapters = adapters_for_auto_run
  return nil if adapters.empty?

  run = PartyResearchRun.create!(party: @party, adapters: adapters)
  jid = PartyEnrichmentWorker.perform_in(SETTLE_DELAY, run.id)
  run.update!(sidekiq_jid: jid)
  run
rescue StandardError => e
  # Auto-enrichment is best-effort — never block the state
  # transition (or whatever else triggered it) on a queueing
  # hiccup.
  Rails.logger.warn("[PartyResearch::AutoEnricher] #{@party.class.name}##{@party.id} failed: #{e.class}: #{e.message}")
  nil
end