Discourse Digest Mailer (EnqueueDigestEmails) Monkey Patch

OOTB, Discourse sets the EnqueueDigestEmails sidekiq job interval to 30 minutes; but I have monkey patched this for the following reasons:

  • There is less load sending out 10 versus 60 at a time (or 50 versus 300, for example).
  • The admin email->sent log file is easier to read at a glance.
  • When we rebuild the container, the job always runs “on startup” so this lower number mitigated the initial job digest run to a much smaller number (since I could not figure out how to get the Discourse config to skip the initial running the job after the container rebuilds).

These are very small advantages, so I don’t recommend most people monkey patch the digest job for this small subjective advantage; but I prefer a 5 minute digest job interval over a 30 minute interval, and after testing for a few days, the mp works great and so, we are going to stay with the every 5 minutes interval.

# frozen_string_literal: true

module Jobs
  EnqueueDigestEmails.class_eval do
    every 5.minutes

    def execute(args)
      return if SiteSetting.disable_digest_emails? || SiteSetting.private_email? || SiteSetting.disable_emails == "yes"
      users = target_user_ids

      users.each do |user_id|
        ::Jobs.enqueue(:user_email, type: :digest, user_id: user_id)
      end
    end

    def target_user_ids
      # Users who want to receive digest email within their chosen digest email frequency
      query = User
        .real
        .activated
        .not_suspended
        .where(staged: false)
        .joins(:user_option, :user_stat, :user_emails)
        .where("user_options.email_digests")
        .where("user_stats.bounce_score < #{SiteSetting.bounce_score_threshold}")
        .where("user_emails.primary")
        .where("COALESCE(last_emailed_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * user_options.digest_after_minutes)")
        .where("COALESCE(user_stats.digest_attempted_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * user_options.digest_after_minutes)")
        .where("COALESCE(last_seen_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 MINUTE'::INTERVAL * user_options.digest_after_minutes)")
        .where("COALESCE(last_seen_at, '2010-01-01') >= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * #{SiteSetting.suppress_digest_email_after_days})")
        .order("user_stats.digest_attempted_at ASC NULLS FIRST")

      # If the site requires approval, make sure the user is approved
      query = query.where("approved OR moderator OR admin") if SiteSetting.must_approve_users?

      query = query.pluck(:id)

      limit = GlobalSetting.max_digests_enqueued_per_30_mins_per_site

      query = query[0, limit]
    end
  end
end

See Also:

See Also:

This topic was automatically closed 0 minutes after the last reply. New replies are no longer allowed.