2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-01-08 15:29:34 +00:00
|
|
|
class Sequencer
|
|
|
|
class Unit
|
|
|
|
module Import
|
|
|
|
module Zendesk
|
|
|
|
module SubSequence
|
|
|
|
module Base
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
def resource_klass
|
|
|
|
@resource_klass ||= name.split('::').last.singularize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
2019-08-08 08:25:37 +00:00
|
|
|
|
2018-01-08 15:29:34 +00:00
|
|
|
base.uses :dry_run, :import_job
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_params
|
|
|
|
{
|
|
|
|
dry_run: dry_run,
|
|
|
|
import_job: import_job,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_klass
|
|
|
|
# base.instance_delegate [:resource_klass] => base
|
|
|
|
# doesn't work since we are included and then inherited
|
|
|
|
# there might be multiple inherited hooks which overwrite
|
|
|
|
# each other :/
|
|
|
|
self.class.resource_klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def sequence_name
|
|
|
|
"Import::Zendesk::#{resource_klass}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_iteration(&block)
|
|
|
|
resource_collection.public_send(resource_iteration_method, &block)
|
2018-11-22 01:33:02 +00:00
|
|
|
rescue ZendeskAPI::Error::NetworkError => e
|
2021-01-21 08:29:59 +00:00
|
|
|
return if expected_exception?(e)
|
|
|
|
raise if !retry_exception?(e)
|
|
|
|
raise if (fail_count ||= 1) > 10
|
2019-08-09 09:08:43 +00:00
|
|
|
|
2021-01-21 08:29:59 +00:00
|
|
|
logger.error e
|
|
|
|
logger.info "Sleeping 10 seconds after ZendeskAPI::Error::NetworkError and retry (##{fail_count}/10)."
|
|
|
|
sleep 10
|
2019-08-09 09:08:43 +00:00
|
|
|
|
2021-01-21 08:29:59 +00:00
|
|
|
fail_count += 1
|
|
|
|
retry
|
|
|
|
end
|
2019-08-09 09:08:43 +00:00
|
|
|
|
2021-01-21 08:29:59 +00:00
|
|
|
# #2262 Zendesk-Import fails for User & Organizations when 403 "access" denied
|
|
|
|
def expected_exception?(e)
|
|
|
|
status = e.response.status.to_s
|
|
|
|
return false if status != '403'
|
2018-12-14 01:59:19 +00:00
|
|
|
|
2021-01-21 08:29:59 +00:00
|
|
|
%w[UserField OrganizationField].include?(resource_klass)
|
|
|
|
end
|
2018-12-14 01:59:19 +00:00
|
|
|
|
2021-01-21 08:29:59 +00:00
|
|
|
def retry_exception?(e)
|
2021-08-11 11:11:14 +00:00
|
|
|
!(200..399).cover? e&.response&.status
|
2018-01-08 15:29:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def resource_collection
|
2021-01-21 07:22:17 +00:00
|
|
|
@resource_collection ||= collection_provider.public_send(resource_collection_attribute)
|
2018-01-08 15:29:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def resource_iteration_method
|
|
|
|
:all!
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_collection_attribute
|
|
|
|
@resource_collection_attribute ||= resource_klass.pluralize.underscore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|