Skip to content

Commit

Permalink
Fixes #3653 - Freshdesk import fails with timeout error.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgruner committed Jul 14, 2021
1 parent ee62e47 commit 718899d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/jobs/application_job.rb
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions 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
Expand Down
25 changes: 25 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions lib/sequencer/unit/import/freshdesk/requester.rb
Expand Up @@ -15,7 +15,7 @@ def request(api_path:, params: nil)
return response if response.is_a? Net::HTTPOK

handle_error response, iteration
rescue => e
rescue Net::HTTPClientError => e
handle_exception e, iteration
end
end
Expand All @@ -34,7 +34,7 @@ def handle_error(response, iteration)

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

Expand Down

0 comments on commit 718899d

Please sign in to comment.