base.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Sequencer::Unit::Import::Zendesk::SubSequence::Base
  3. module ClassMethods
  4. def resource_klass
  5. @resource_klass ||= name.split('::').last.singularize
  6. end
  7. end
  8. def self.included(base)
  9. base.extend(ClassMethods)
  10. base.uses :dry_run, :import_job, :field_map
  11. end
  12. private
  13. def default_params
  14. {
  15. dry_run: dry_run,
  16. import_job: import_job,
  17. field_map: field_map,
  18. }
  19. end
  20. def resource_klass
  21. # base.instance_delegate [:resource_klass] => base
  22. # doesn't work since we are included and then inherited
  23. # there might be multiple inherited hooks which overwrite
  24. # each other :/
  25. self.class.resource_klass
  26. end
  27. def sequence_name
  28. "Import::Zendesk::#{resource_klass}"
  29. end
  30. def resource_iteration(&)
  31. resource_collection.public_send(resource_iteration_method, &)
  32. rescue ZendeskAPI::Error::NetworkError, Faraday::SSLError => e
  33. return if expected_exception?(e)
  34. raise if !retry_exception?(e)
  35. raise if (fail_count ||= 1) > 10
  36. logger.error e
  37. logger.info "Sleeping 10 seconds after ZendeskAPI::Error::NetworkError and retry (##{fail_count}/10)."
  38. sleep 10
  39. fail_count += 1
  40. retry
  41. end
  42. # #2262 Zendesk-Import fails for User & Organizations when 403 "access" denied
  43. def expected_exception?(e)
  44. status = e.response&.status.to_s
  45. return false if !status || status != '403'
  46. %w[UserField OrganizationField].include?(resource_klass)
  47. end
  48. def retry_exception?(e)
  49. e.is_a?(Faraday::SSLError) || !(200..399).cover?(e&.response&.status)
  50. end
  51. def resource_collection
  52. @resource_collection ||= collection_provider.public_send(resource_collection_attribute)
  53. end
  54. def resource_iteration_method
  55. :all!
  56. end
  57. def resource_collection_attribute
  58. @resource_collection_attribute ||= resource_klass.pluralize.underscore
  59. end
  60. end