Class: AudiencePartyBackfillWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Sidekiq::Status::Worker
Defined in:
app/workers/audience_party_backfill_worker.rb

Overview

On-demand: resolve and persist the customer (party) for each audience_member on a
static email list by matching email_address → Contact (implicit customer) →
Customer, most-recent-wins. Triggered from the audience_member-list page; progress
surfaces in the global Jobs tracker via Sidekiq::Status.

Thin wrapper around Audience::PartyBackfill. Idempotent — only fills
audience_members whose customer_id is still NULL — so it is safe to re-run.

Instance Method Summary collapse

Instance Method Details

#perform(audience_id) ⇒ Object

Parameters:

  • audience_id (Integer)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/workers/audience_party_backfill_worker.rb', line 17

def perform(audience_id)
  list = Audience.find_by(id: audience_id)
  return store(info_message: 'List not found.') unless list

  # Send the user back to the list when the job finishes (the /jobs page
  # auto-redirects here on completion); applies to the guard path too.
  store redirect_to: "/audiences/#{list.id}"

  return store(info_message: '⏭️ Party backfill runs on static email lists only.') unless list.list_type == 'static'

  total 2
  at 1, 'Matching audience_members to contacts and customers by email…'

  result = Audience::PartyBackfill.new(list).call
  list.update_columns(party_backfilled_at: Time.current)

  at 2, 'Done'
  store(
    candidates:   result.candidates,
    resolved:     result.resolved,
    unmatched:    result.unmatched,
    info_message: "Matched #{result.resolved} of #{result.candidates} unresolved audience_member(s); " \
                  "#{result.unmatched} had no contact or customer with that email."
  )

  Rails.event.notify('audience.party_backfill',
                      audience_id: list.id,
                      candidates:         result.candidates,
                      resolved:           result.resolved,
                      unmatched:          result.unmatched)
end