otrs.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. extend Import::Helper
  5. extend Import::OTRS::ImportStats
  6. extend Import::OTRS::Async
  7. extend Import::OTRS::Diff
  8. extend self
  9. # Start import with specific parameters.
  10. # Useful for debug and continuing from breakpoint of last not success import
  11. #
  12. # @example
  13. # Import::OTRS::start() - Nomrmal usage
  14. #
  15. # Import::OTRS::start(thread: 1, offset: 1000) - Run the task in Single-Thread and start from offset 1000
  16. def start(args = {})
  17. log 'Start import...'
  18. checks
  19. prerequisites
  20. base_objects
  21. updateable_objects
  22. customer_user
  23. threaded_import('Ticket', args)
  24. true
  25. end
  26. def connection_test
  27. Import::OTRS::Requester.connection_test
  28. end
  29. private
  30. def checks
  31. check_import_mode
  32. check_system_init_done
  33. connection_test
  34. end
  35. def prerequisites
  36. # make sure to create store type otherwise
  37. # it might lead to race conditions while
  38. # creating it in different import threads
  39. Store::Object.create_if_not_exists(name: 'Ticket::Article')
  40. end
  41. def import(remote_object, args = {})
  42. log "loading #{remote_object}..."
  43. import_action(remote_object, args)
  44. end
  45. def threaded_import(remote_object, args = {})
  46. thread_count = args[:threads] || 8
  47. limit = args[:limit] || 20
  48. start_offset_base = args[:offset] || 0
  49. Thread.abort_on_exception = true
  50. threads = {}
  51. (1..thread_count).each do |thread|
  52. threads[thread] = Thread.new do
  53. # In some environments the Model.reset_column_information
  54. # is not reflected to threads. So an import error message appears.
  55. # Reset needed model column information for each thread.
  56. reset_database_information
  57. Thread.current[:thread_no] = thread
  58. Thread.current[:loop_count] = 0
  59. log "Importing #{remote_object} in steps of #{limit}"
  60. loop do
  61. # get the offset for the current thread and loop count
  62. thread_offset_base = (Thread.current[:thread_no] - 1) * limit
  63. thread_step = thread_count * limit
  64. offset = (Thread.current[:loop_count] * thread_step) + thread_offset_base + start_offset_base
  65. break if !imported?(
  66. remote_object: remote_object,
  67. limit: limit,
  68. offset: offset,
  69. diff: args[:diff]
  70. )
  71. Thread.current[:loop_count] += 1
  72. end
  73. ActiveRecord::Base.connection.close
  74. end
  75. end
  76. (1..thread_count).each do |thread| # rubocop:disable Style/CombinableLoops
  77. threads[thread].join
  78. end
  79. end
  80. def limit_import(remote_object, args = {})
  81. offset = 0
  82. limit = args[:limit] || 20
  83. log "Importing #{remote_object} in steps of #{limit}"
  84. loop do
  85. break if !imported?(
  86. remote_object: remote_object,
  87. limit: limit,
  88. offset: offset,
  89. diff: args[:diff]
  90. )
  91. offset += limit
  92. end
  93. end
  94. def imported?(args)
  95. log "loading #{args[:limit]} #{args[:remote_object]} starting at #{args[:offset]}..."
  96. return false if !import_action(args[:remote_object], limit: args[:limit], offset: args[:offset], diff: args[:diff])
  97. true
  98. end
  99. def import_action(remote_object, args = {})
  100. records = Import::OTRS::Requester.load(remote_object, limit: args[:limit], offset: args[:offset], diff: args[:diff])
  101. if records.blank?
  102. log '... no more work.'
  103. return false
  104. end
  105. factory_class(remote_object).import(records)
  106. end
  107. def factory_class(object)
  108. "Import::OTRS::#{object}Factory".constantize
  109. end
  110. # sync settings
  111. def base_objects
  112. import('SysConfig')
  113. import('DynamicField')
  114. end
  115. def updateable_objects
  116. import('State')
  117. import('Priority')
  118. import('Queue')
  119. import('User')
  120. import('Customer')
  121. end
  122. def customer_user
  123. limit_import('CustomerUser', limit: 50)
  124. end
  125. def reset_database_information
  126. ::Ticket.reset_column_information
  127. end
  128. end
  129. end