diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index a009ace..ce4acc2 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -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 diff --git a/app/jobs/resync_job.rb b/app/jobs/resync_job.rb new file mode 100644 index 0000000..70dfc04 --- /dev/null +++ b/app/jobs/resync_job.rb @@ -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 diff --git a/app/models/check.rb b/app/models/check.rb index fa4c2aa..296f57f 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -100,8 +100,7 @@ class Check < ApplicationRecord return unless active? return unless saved_changes.key?("domain") - WhoisSyncJob.perform_later(id) if domain? - SSLSyncJob.perform_later(id) if ssl? + ResyncJob.perform_later(id) end def reset_notifications diff --git a/app/views/checks/_table.html.erb b/app/views/checks/_table.html.erb index fcda912..f4020fa 100644 --- a/app/views/checks/_table.html.erb +++ b/app/views/checks/_table.html.erb @@ -36,7 +36,7 @@ <% end %> - <%= link_to edit_check_path(check) do %> + <%= link_to edit_check_path(check), "data-turbolinks" => false do %> <%== Octicons::Octicon.new("pencil").to_svg %> <% end %> diff --git a/test/jobs/resync_job_test.rb b/test/jobs/resync_job_test.rb new file mode 100644 index 0000000..6c12be2 --- /dev/null +++ b/test/jobs/resync_job_test.rb @@ -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