1
0

email_scheduler.rb 599 B

12345678910111213141516171819202122232425
  1. # frozen_string_literal: true
  2. class Scheduler::EmailScheduler
  3. include Sidekiq::Worker
  4. sidekiq_options unique: :until_executed, retry: 0
  5. FREQUENCY = 7.days.freeze
  6. SIGN_IN_OFFSET = 1.day.freeze
  7. def perform
  8. eligible_users.reorder(nil).find_each do |user|
  9. next unless user.allows_digest_emails?
  10. DigestMailerWorker.perform_async(user.id)
  11. end
  12. end
  13. private
  14. def eligible_users
  15. User.emailable
  16. .where('current_sign_in_at < ?', (FREQUENCY + SIGN_IN_OFFSET).ago)
  17. .where('last_emailed_at IS NULL OR last_emailed_at < ?', FREQUENCY.ago)
  18. end
  19. end