discourse/app/jobs/scheduled/clean_up_inactive_users.rb
Dan Ungureanu e23c87c442
FIX: Prevent exception in rescue block.
> Job exception: undefined method `id' for nil:NilClass
> clean_up_inactive_users.rb:25:in `rescue in block in execute'
2019-04-16 01:27:39 +03:00

32 lines
914 B
Ruby

module Jobs
class CleanUpInactiveUsers < Jobs::Scheduled
every 1.day
def execute(args)
return if SiteSetting.clean_up_inactive_users_after_days <= 0
destroyer = UserDestroyer.new(Discourse.system_user)
User.joins("LEFT JOIN posts ON posts.user_id = users.id")
.where(last_posted_at: nil, trust_level: TrustLevel.levels[:newuser])
.where(
"posts.user_id IS NULL AND users.last_seen_at < ?",
SiteSetting.clean_up_inactive_users_after_days.days.ago
)
.limit(1000)
.pluck(:id).each do |id|
begin
user = User.find(id)
destroyer.destroy(user, context: I18n.t("user.destroy_reasons.inactive_user"))
rescue => e
Discourse.handle_job_exception(e,
message: "Cleaning up inactive users",
extra: { user_id: id }
)
end
end
end
end
end