Browse Source

Fixes #3653 - Freshdesk import fails with timeout error.

Martin Gruner 3 years ago
parent
commit
718899da18

+ 5 - 0
app/jobs/application_job.rb

@@ -7,6 +7,11 @@ class ApplicationJob < ActiveJob::Base
 
   ActiveJob::Logging::LogSubscriber.detach_from :active_job
 
+  # See config/initializers/delayed_jobs_timeout_per_job.rb for details.
+  def self.max_run_time
+    4.hours
+  end
+
   # Automatically retry jobs that encountered a deadlock
   # retry_on ActiveRecord::Deadlocked
 

+ 6 - 0
app/jobs/async_import_job.rb

@@ -1,6 +1,12 @@
 # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
 
 class AsyncImportJob < ApplicationJob
+
+  # See config/initializers/delayed_jobs_timeout_per_job.rb for details.
+  def self.max_run_time
+    7.days
+  end
+
   def perform(import_job)
     import_job.start
   end

+ 25 - 0
config/initializers/delayed_jobs_timeout_per_job.rb

@@ -0,0 +1,25 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+# Workaround for ActiveJob not supporting per-job timeouts with delayed_job.
+#
+# delayed_job does support this (https://github.com/collectiveidea/delayed_job#custom-jobs),
+#   but since ActiveJob's adapter places a JobWrapper class around the jobs, it fails to work.
+#
+# Solve this by delegating that method to the actual job class instead.
+
+# Set the maximum possible max_run_time for any job to a high value, and set a sensible default
+#   in ApplicationJob.max_run_time. Then specific jobs like AsyncImportJob can override this with a
+#   higher value.
+Delayed::Worker.max_run_time = 7.days
+
+module ActiveJob
+  module QueueAdapters
+    class DelayedJobAdapter
+      class JobWrapper
+        def max_run_time
+          job_data['job_class'].constantize.max_run_time
+        end
+      end
+    end
+  end
+end

+ 2 - 2
lib/sequencer/unit/import/freshdesk/requester.rb

@@ -15,7 +15,7 @@ class Sequencer
               return response if response.is_a? Net::HTTPOK
 
               handle_error response, iteration
-            rescue => e
+            rescue Net::HTTPClientError => e
               handle_exception e, iteration
             end
           end
@@ -34,7 +34,7 @@ class Sequencer
 
           def handle_exception(e, iteration)
             logger.error e
-            logger.info "Sleeping 10 seconds after #{e.name} and retry (##{iteration + 1}/10)."
+            logger.info "Sleeping 10 seconds after #{e.class.name} and retry (##{iteration + 1}/10)."
             sleep 10
           end