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.
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.