integration_base.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. # This base class handles regular integrations.
  4. # It provides generic interfaces for settings and active state.
  5. # It ensures that all requirements for a regular integration are met before a import can start.
  6. # It handles the case of a background worker interruption.
  7. #
  8. # It's required to implement the +start_import+ method which only has to start the import.
  9. class IntegrationBase < Import::Base
  10. def self.inherited(subclass)
  11. super
  12. subclass.extend(Forwardable)
  13. # delegate instance methods to the generic class implementations
  14. subclass.delegate %i[identifier active? config display_name] => subclass
  15. end
  16. # Defines the integration identifier used for
  17. # automatic config lookup and error message generation.
  18. #
  19. # @example
  20. # Import::Ldap.identifier
  21. # #=> "Ldap"
  22. #
  23. # return [String]
  24. def self.identifier
  25. name.split('::').last
  26. end
  27. # Provides the name that is used in texts visible to the user.
  28. #
  29. # @example
  30. # Import::Exchange.display_name
  31. # #=> "Exchange"
  32. #
  33. # return [String]
  34. def self.display_name
  35. identifier
  36. end
  37. # Checks if the integration is active.
  38. #
  39. # @example
  40. # Import::Ldap.active?
  41. # #=> true
  42. #
  43. # return [Boolean]
  44. def self.active?
  45. Setting.get("#{identifier.downcase}_integration") || false
  46. end
  47. # Provides the integration configuration.
  48. #
  49. # return [Hash] the configuration
  50. def self.config
  51. Setting.get("#{identifier.downcase}_config") || {}
  52. end
  53. # Stores the integration configuration.
  54. #
  55. # return [nil]
  56. def self.config=(value)
  57. Setting.set("#{identifier.downcase}_config", value)
  58. end
  59. # Checks if the integration is activated and configured.
  60. # Otherwise it won't get queued since it will display
  61. # an error which is confusing and wrong.
  62. #
  63. # @example
  64. # Import::Exchange.queueable?
  65. # #=> true
  66. #
  67. # return [Boolean]
  68. def self.queueable?
  69. active? && config.present?
  70. end
  71. # Starts a live or dry run import.
  72. #
  73. # @example
  74. # instance = Import::Ldap.new(import_job)
  75. #
  76. # @raise [RuntimeError] Raised if an import should start but the integration is disabled
  77. #
  78. # return [nil]
  79. def start
  80. return if !requirements_completed?
  81. start_import
  82. end
  83. # Gets called when the background worker gets (re-)started and an ImportJob was still
  84. # in the queue. The job will always get restarted to avoid the gap till the next
  85. # run triggered by the background worker. The result will get updated to inform the user
  86. # in the agent interface result view.
  87. #
  88. # @example
  89. # instance = Import::Ldap.new(import_job)
  90. # instance.reschedule?(delayed_job)
  91. # #=> true
  92. #
  93. # return [true]
  94. def reschedule?(_delayed_job)
  95. inform('Restarting due to scheduler restart.')
  96. true
  97. end
  98. private
  99. def start_import
  100. raise "Missing implementation of method '#{__method__}' for #{self.class.name}"
  101. end
  102. def requirements_completed?
  103. return true if @import_job.dry_run
  104. if !active?
  105. message = "Sync cancelled. #{display_name} integration deactivated. Activate via the switch."
  106. end
  107. return true if !message
  108. inform(message)
  109. false
  110. end
  111. def inform(message)
  112. @import_job.update!(result: {
  113. info: message
  114. })
  115. end
  116. end
  117. end