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 %>