Rails 如何检查是否已向用户发送过账号过期预警邮件
两类可直接落地的去重实现方案
方案1:给用户表加专用发送标记(改动最小,适配单类通知场景)
- 先通过数据库迁移给用户表新增
warned_inactive_atdatetime类型字段,搭配普通索引加快查询速度:
# db/migrate/[时间戳]_add_warned_inactive_at_to_users.rb class AddWarnedInactiveAtToUsers < ActiveRecord::Migration[7.0] def change add_column :users, :warned_inactive_at, :datetime add_index :users, :warned_inactive_at end end
- 在User模型里补充查询作用域,筛选出还没收到过本次停用预警的用户:
# app/models/user.rb scope :havent_signed_in_for_n_days, ->(n) { where("last_sign_in_at < ?", n.days.ago) } scope :not_warned_inactive, -> { where(warned_inactive_at: nil) }
- 调整原有服务类逻辑,发完邮件立刻给用户打对应标记:
class DeactivateOldEmailsService def run to_deactivate = User.havent_signed_in_for_n_days(97) to_deactivate.each { |user| user.update!(active: false) } # 仅捞取90天未登录、且从未收到过本次停用预警的用户 to_warn = User.havent_signed_in_for_n_days(90).not_warned_inactive to_warn.each do |user| InactiveEmailsMailer.account_will_expire(user).deliver_later # 发送后立即标记,下次定时任务执行时不会重复捞取该用户 user.update!(warned_inactive_at: Time.current) end end end
- 优化逻辑:如果用户收到预警后重新登录,自动清空标记。这样下次用户再触发连续90天未登录的规则时,还能正常收到预警,比永久布尔标记的灵活性更高:
# app/models/user.rb # 用户登录时间更新时自动重置预警标记 after_update :reset_inactive_warn_flag, if: :saved_change_to_last_sign_in_at? def reset_inactive_warn_flag update(warned_inactive_at: nil) if warned_inactive_at.present? end
这个方案的优势是改动量小,查询时直接命中用户表索引,性能最优,适合当前只有这一类未活跃通知的场景。
方案2:搭建独立通知日志表(扩展性强,适配多类通知场景)
如果后续还要针对用户发送不同节点的未活跃提醒(比如30天未登发通用召回、60天未登发功能引导、90天未登发停用预警),不建议在用户表堆叠大量标记字段,可以单独建一张通知发送记录表统一管理所有通知的发送状态:
- 写迁移生成
user_notification_logs表,存储用户ID、通知类型、发送时间:
# db/migrate/[时间戳]_create_user_notification_logs.rb class CreateUserNotificationLogs < ActiveRecord::Migration[7.0] def change create_table :user_notification_logs do |t| t.references :user, null: false, foreign_key: true t.string :notification_type, null: false t.datetime :sent_at, null: false t.timestamps end add_index :user_notification_logs, [:user_id, :notification_type], unique: true end end
- 建立对应模型关联:
# app/models/user_notification_log.rb class UserNotificationLog < ApplicationRecord belongs_to :user end
- 调整服务类逻辑,排除已经发过同类通知的用户:
class DeactivateOldEmailsService DEACTIVATE_WARN_TYPE = 'inactive_deactivation_warning' def run to_deactivate = User.havent_signed_in_for_n_days(97) to_deactivate.each { |user| user.update!(active: false) } # 捞出所有已经发过停用预警的用户ID,从待发列表中排除 warned_user_ids = UserNotificationLog.where(notification_type: DEACTIVATE_WARN_TYPE).pluck(:user_id) to_warn = User.havent_signed_in_for_n_days(90).where.not(id: warned_user_ids) to_warn.each do |user| InactiveEmailsMailer.account_will_expire(user).deliver_later # 发送后写入通知日志 UserNotificationLog.create!( user: user, notification_type: DEACTIVATE_WARN_TYPE, sent_at: Time.current ) end end end
这个方案的优势是扩展性强,不需要每次新增通知类型就修改用户表结构,适合长期有多种用户通知需求的场景。
落地注意事项
- 不要依赖邮件系统的发信记录做去重判断:邮件发件箱可能存在同步延迟、记录清理、发信通道切换的问题,可靠性远低于业务库内的持久化标记。
- 打标记的时机要放在邮件任务提交成功后立刻执行,不要等后台队列把邮件实际投递完成再标记:避免定时任务重复执行、队列拥堵时出现重复发送的问题。
- 原代码中
messages_for_user_per_employer属于无意义的冗余赋值,可以直接删掉。
内容的提问来源于stack exchange,提问作者Mirror318
相关产品推荐
相关产品推荐

