Email Digest Background Process

Curated ago by @jeremysmithco

Description

This set of Sidekiq workers queries a set of email digest subscribers and performs a fan-out to send email to each subscriber as a separate job.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class DigestEnqueuer
  include Sidekiq::Worker

  def perform(digest_id)
    digest = Digest.find(digest_id)
    return if digest.blank?
    return if digest.sent?

    subscription_ids = digest.subscriptions.sendable.pluck(:id)
    subscription_ids.each do |subscription_id|
      DigestSender.perform_async(digest.id, subscription_id)
    end

    digest.update(sent_at: Time.current)
  end
end

This worker loads the digest, checks if it’s already been sent, and then performs the fan-out to send the digest to each subscription. Finally, it marks the digest as sent.