You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
870 B

  1. # frozen_string_literal: true
  2. class Scheduler::EmailDomainBlockRefreshScheduler
  3. include Sidekiq::Worker
  4. include Redisable
  5. sidekiq_options retry: 0
  6. def perform
  7. Resolv::DNS.open do |dns|
  8. dns.timeouts = 5
  9. EmailDomainBlock.find_each do |email_domain_block|
  10. ips = begin
  11. if ip?(email_domain_block.domain)
  12. [email_domain_block.domain]
  13. else
  14. resources = dns.getresources(email_domain_block.domain, Resolv::DNS::Resource::IN::A).to_a + dns.getresources(email_domain_block.domain, Resolv::DNS::Resource::IN::AAAA).to_a
  15. resources.map { |resource| resource.address.to_s }
  16. end
  17. end
  18. email_domain_block.update(ips: ips, last_refresh_at: Time.now.utc)
  19. end
  20. end
  21. end
  22. def ip?(str)
  23. str =~ Regexp.union([Resolv::IPv4::Regex, Resolv::IPv6::Regex])
  24. end
  25. end