PERF: Do not enqueue digest emails when attempted recently (PR #10849)

Previously, Jobs::EnqueueDigestEmails would enqueue a digest job for every user, even if there are no topics to send. The digest job would exit, no email would send, and last_emailed_at would not change. 30 minutes later, Jobs::EnqueueDigestEmails would run again and re-enqueue jobs for the same users.

120fa8ad introduced a temporary mitigation for this issue, by randomly selecting a subset of those users each time.

This commit adds a new digest_attempted_at column to the user_stats table. This column is updated every time a digest job completes for a user. Using this, we can avoid scheduling digest jobs for the same user every 30 minutes. This also removes the random user selection in 120fa8ad, and instead prioritizes users who had digests attempted the longest time ago.


TIL/FYI - a shorter alternative:

        UserStat.where(user_id: args[:user_id]).touch_all(:digest_attempted_at)

:kissing: some nice sqls there!

Looks like this will also change the updated_at column… which I don’t think we want. I think maybe update_all is a little more explicit/obvious? (Confirmed by the fact that touch_all is a “TIL” for both of us? :wink: )

It would bump updated_at column… if UserStat had one! :trollface: