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

Wrap first sync in a common job

Sometimes, when the first job is executed milliseconds after the insert, 
the db would not return it.
This way, we can safely perform another attempt.
This commit is contained in:
Colin Darie 2018-07-05 17:54:21 +02:00
parent 3de56c09cc
commit 717cd3fac3
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
4 changed files with 70 additions and 4 deletions

View file

@ -1,2 +1,22 @@
class ApplicationJob < ActiveJob::Base
# from http://api.rubyonrails.org/classes/ActiveJob/Core.html
attr_writer :attempt_number
def attempt_number
@attempt_number ||= 0
end
def serialize
super.merge("attempt_number" => attempt_number + 1)
end
def deserialize(job_data)
super
self.attempt_number = job_data["attempt_number"]
end
rescue_from(Timeout::Error) do
raise if attempt_number > 5 # rubocop:disable Style/SignalException
retry_job(wait: 10)
end
end

18
app/jobs/resync_job.rb Normal file
View file

@ -0,0 +1,18 @@
class ResyncJob < ApplicationJob
queue_as :default
rescue_from ActiveRecord::RecordNotFound do
raise if attempt_number == 5 # rubocop:disable Style/SignalException
# at job creation, the db could not return immediately the check
# we have to wait a few (milli)-seconds
retry_job(wait: attempt_number**2)
end
def perform(check_id)
check = Check.find(check_id)
WhoisSyncJob.perform_later(check_id) if check.domain?
SSLSyncJob.perform_later(check_id) if check.ssl?
end
end

View file

@ -100,10 +100,7 @@ class Check < ApplicationRecord
return unless active?
return unless saved_changes.key?("domain")
# small delay before first sync, otherwise on fast system
# the db won't return the check immediately after insert
WhoisSyncJob.perform_later(id, delay: 1.second) if domain?
SSLSyncJob.perform_later(id, delay: 1.second) if ssl?
ResyncJob.perform_later(id)
end
def reset_notifications

View file

@ -0,0 +1,31 @@
require "test_helper"
class ReyncJobTest < ActiveJob::TestCase
test "enqueue a whois sync job" do
check = create(:check, :domain)
assert_enqueued_with(job: WhoisSyncJob, args: [check.id]) do
ResyncJob.perform_now(check.id)
end
end
test "enqueue a ssl sync job" do
check = create(:check, :ssl)
assert_enqueued_with(job: SSLSyncJob, args: [check.id]) do
ResyncJob.perform_now(check.id)
end
end
test "re enqueued job 5 times when check was not found" do
assert_enqueued_jobs 0
perform_enqueued_jobs(only: ResyncJob) do
assert_raise ActiveRecord::RecordNotFound do
ResyncJob.perform_now(9999)
end
end
assert_performed_jobs 5, only: ResyncJob
end
end