ip_cleanup_scheduler.rb 1.0 KB

1234567891011121314151617181920212223242526272829
  1. # frozen_string_literal: true
  2. class Scheduler::IpCleanupScheduler
  3. include Sidekiq::Worker
  4. IP_RETENTION_PERIOD = ENV.fetch('IP_RETENTION_PERIOD', 1.year).to_i.seconds.freeze
  5. SESSION_RETENTION_PERIOD = ENV.fetch('SESSION_RETENTION_PERIOD', 1.year).to_i.seconds.freeze
  6. sidekiq_options retry: 0, lock: :until_executed, lock_ttl: 1.day.to_i
  7. def perform
  8. clean_ip_columns!
  9. clean_expired_ip_blocks!
  10. end
  11. private
  12. def clean_ip_columns!
  13. SessionActivation.where('updated_at < ?', SESSION_RETENTION_PERIOD.ago).in_batches.destroy_all
  14. SessionActivation.where('updated_at < ?', IP_RETENTION_PERIOD.ago).in_batches.update_all(ip: nil)
  15. User.where('current_sign_in_at < ?', IP_RETENTION_PERIOD.ago).in_batches.update_all(sign_up_ip: nil)
  16. LoginActivity.where('created_at < ?', IP_RETENTION_PERIOD.ago).in_batches.destroy_all
  17. Doorkeeper::AccessToken.where('last_used_at < ?', IP_RETENTION_PERIOD.ago).in_batches.update_all(last_used_ip: nil)
  18. end
  19. def clean_expired_ip_blocks!
  20. IpBlock.expired.in_batches.destroy_all
  21. end
  22. end