21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-05-05 02:05:09 +02:00

Ask Job at whois creation (inline in dev)

This commit is contained in:
Colin Darie 2018-05-30 18:16:43 +02:00
parent 1179a10775
commit 53ca6f1f7f
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
5 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1,27 @@
class WhoisSyncJob < ApplicationJob
queue_as :default
rescue_from ActiveRecord::RecordNotFound do; end
attr_reader :check
def perform(check_id)
@check = Check.find(check_id)
response = Whois.ask(check.domain)
return unless response.valid?
update_from_response(response)
check.save!
rescue Whois::DomainNotFoundError
check.active = false
check.save!
end
def update_from_response(response)
check.domain_created_at = response.created_at
check.domain_updated_at = response.updated_at
check.domain_expire_at = response.expire_at
end
end

View file

@ -45,6 +45,8 @@ class Check < ApplicationRecord
validates :comment, length: { maximum: 255 }
validates :vendor, length: { maximum: 255 }
after_save :enqueue_sync
protected
def domain_created_at_past
@ -54,4 +56,11 @@ class Check < ApplicationRecord
def domain_updated_at_past
errors.add(:domain_updated_at, :past) if domain_updated_at.present? && domain_updated_at.future?
end
def enqueue_sync
return unless active?
return unless saved_changes.key?("domain")
WhoisSyncJob.perform_later(id) if domain?
end
end

View file

@ -8,7 +8,7 @@ require_relative "whois/errors"
module Whois
class << self
def ask(domain, system_klass: SystemCommand, logger: NullLogger.new)
Service.new(domain, system_klass, logger: logger).call
Service.new(domain, system_klass: system_klass, logger: logger).call
end
end

View file

@ -67,4 +67,6 @@ Rails.application.configure do
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.active_job.queue_adapter = :inline
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class WhoisSyncJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end