import_job.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ImportJob < ApplicationModel
  3. store :payload
  4. store :result
  5. # Starts the import backend class based on the name attribute.
  6. # Import backend class is initialized with the current instance.
  7. # Logs the start and end time (if ended successfully) and logs
  8. # exceptions into result if they happen.
  9. #
  10. # @example
  11. # import = ImportJob.new(name: 'Import::Ldap', payload: Setting.get('ldap_config'))
  12. # import.start
  13. #
  14. # return [nil]
  15. def start
  16. self.started_at = Time.zone.now
  17. save
  18. name.constantize.new(self)
  19. rescue => e
  20. Rails.logger.error e
  21. # rubocop:disable Style/RedundantSelf
  22. if !self.result.is_a?(Hash)
  23. self.result = {}
  24. end
  25. self.result[:error] = e.message
  26. # rubocop:enable Style/RedundantSelf
  27. ensure
  28. self.finished_at = Time.zone.now
  29. save
  30. end
  31. # Convenience wrapper around the start method for starting (delayed) dry runs.
  32. # Logs the start and end time (if ended successfully) and logs
  33. # exceptions into result if they happen.
  34. # Only one running or pending dry run per backend is possible at the same time.
  35. #
  36. # @param [Hash] params the params used to initialize the ImportJob instance.
  37. # @option params [Boolean] :delay Defines if job should get executed delayed. Default is true.
  38. # @example
  39. # import = ImportJob.dry_run(name: 'Import::Ldap', payload: Setting.get('ldap_config'), delay: false)
  40. #
  41. # return [nil]
  42. def self.dry_run(params)
  43. return if exists?(name: params[:name], dry_run: true, finished_at: nil)
  44. params[:dry_run] = true
  45. instance = create(params.except(:delay))
  46. if params.fetch(:delay, true)
  47. instance.delay.start
  48. else
  49. instance.start
  50. end
  51. end
  52. # Queues and starts all import backends as import jobs.
  53. #
  54. # @example
  55. # ImportJob.start_registered
  56. #
  57. # return [nil]
  58. def self.start_registered
  59. queue_registered
  60. start
  61. end
  62. # Starts all import jobs that have not started yet and are no dry runs.
  63. #
  64. # @example
  65. # ImportJob.start
  66. #
  67. # return [nil]
  68. def self.start
  69. where(started_at: nil, dry_run: false).each(&:start)
  70. end
  71. # Queues all configured import backends from Setting 'import_backends' as import jobs
  72. # that are not yet queued.
  73. #
  74. # @example
  75. # ImportJob.queue_registered
  76. #
  77. # return [nil]
  78. def self.queue_registered
  79. import_backends = Setting.get('import_backends')
  80. return if import_backends.blank?
  81. import_backends.each do |backend|
  82. if !backend_valid?(backend)
  83. Rails.logger.error "Invalid import backend '#{backend}'"
  84. next
  85. end
  86. # skip if no entry exists
  87. # skip if a not finished entry exists
  88. next if ImportJob.exists?(name: backend, finished_at: nil)
  89. ImportJob.create(name: backend)
  90. end
  91. end
  92. # Checks if the given import backend is valid.
  93. #
  94. # @example
  95. # ImportJob.backend_valid?('Import::Ldap')
  96. # # => true
  97. #
  98. # return [Boolean]
  99. def self.backend_valid?(backend)
  100. backend.constantize
  101. true
  102. rescue NameError
  103. false
  104. end
  105. end