Class: CampaignDeliveryWorker

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

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



9
10
11
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
# File 'app/workers/campaign_delivery_worker.rb', line 9

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).each do |campaign_delivery|
      if campaign_delivery.pending?
        campaign_delivery.queue_for_transmission
      end
      if campaign_delivery.queued?
        campaign_delivery.generate_communication
      end
    end
  else # enqueue some
    campaign_deliveries.in_batches(of: 100) do |relation|
      campaign_delivery_ids = relation.pluck(:id)
      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').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