Class: CampaignDeliveryWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/campaign_delivery_worker.rb

Overview

Sidekiq worker: campaign delivery.

Instance Method Summary collapse

Instance Method Details

#perform(options = {}) ⇒ Object

This job performs every 60 minutes with no arguments and queues up the delivery individually

Parameters:

  • options (Hash) (defaults to: {})

    job options (string keys)

Options Hash (options):

  • campaign_delivery_ids (Array<Integer>)

    deliver only these CampaignDelivery rows (omitted: enqueue batches)



12
13
14
15
16
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
# File 'app/workers/campaign_delivery_worker.rb', line 12

def perform(options = {})
  campaign_delivery_ids = options['campaign_delivery_ids']
  campaign_deliveries = CampaignDelivery.where(state: %w[pending queued])
  if campaign_delivery_ids.present?
    campaign_deliveries.where(id: campaign_delivery_ids).includes(campaign_email: :campaign).find_each do |campaign_delivery|
      # Skip deliveries whose campaign email has been paused mid-flight.
      # Rows stay pending/queued and will be picked up on resume.
      next if campaign_delivery.campaign_email&.transmission_paused?

      campaign_delivery.queue_for_transmission if campaign_delivery.pending?
      campaign_delivery.generate_communication if campaign_delivery.queued?
    end
  else # enqueue some
    paused_scope = CampaignEmail.where(state: 'transmission_paused').select(:id)
    enqueue_scope = campaign_deliveries.where.not(campaign_email_id: paused_scope)
    enqueue_scope.in_batches(of: 100) do |relation|
      campaign_delivery_ids = relation.ids
      CampaignDeliveryWorker.perform_async('campaign_delivery_ids' => campaign_delivery_ids)
    end
    # Any CampaignEmail which has no queued or pending emails left, will be marked as transmission_complete
    CampaignEmail.where(state: 'transmission_in_progress').find_each do |ce|
      count = ce.campaign_deliveries.where(state: %w[pending queued]).count
      if count.positive?
        logger.info "Campaign email #{ce.id} has #{count} email deliveries left to complete that are in draft or queued"
      else
        logger.info "Campaign email #{ce.id} has no more email deliveries left to complete that are in draft or queued and will be marked complete"
        ce.transmission_complete
      end
    end

  end
end