otrs.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Rails autoload has some issues with same namend sub-classes
  2. # in the importer folder require AND simultaniuos requiring
  3. # of the same file in different threads so we need to
  4. # require them ourself
  5. require 'import/otrs/ticket'
  6. require 'import/otrs/ticket_factory'
  7. require 'import/otrs/article_customer'
  8. require 'import/otrs/article_customer_factory'
  9. require 'import/otrs/article'
  10. require 'import/otrs/article_factory'
  11. require 'import/otrs/article/attachment_factory'
  12. require 'import/otrs/history'
  13. require 'import/otrs/history_factory'
  14. require 'import/otrs/history/article'
  15. require 'import/otrs/history/move'
  16. require 'import/otrs/history/new_ticket'
  17. require 'import/otrs/history/priority_update'
  18. require 'import/otrs/history/state_update'
  19. require 'store'
  20. require 'store/object'
  21. require 'store/provider/db'
  22. require 'store/provider/file'
  23. module Import
  24. module OTRS
  25. extend Import::Helper
  26. extend Import::OTRS::ImportStats
  27. extend Import::OTRS::Async
  28. extend Import::OTRS::Diff
  29. # rubocop:disable Style/ModuleFunction
  30. extend self
  31. def start
  32. log 'Start import...'
  33. checks
  34. prerequisites
  35. base_objects
  36. updateable_objects
  37. customer_user
  38. threaded_import('Ticket')
  39. true
  40. end
  41. def connection_test
  42. Import::OTRS::Requester.connection_test
  43. end
  44. private
  45. def checks
  46. check_import_mode
  47. check_system_init_done
  48. connection_test
  49. end
  50. def prerequisites
  51. # make sure to create store type otherwise
  52. # it might lead to race conditions while
  53. # creating it in different import threads
  54. Store::Object.create_if_not_exists(name: 'Ticket::Article')
  55. end
  56. def import(remote_object, args = {})
  57. log "loading #{remote_object}..."
  58. import_action(remote_object, args)
  59. end
  60. def threaded_import(remote_object, args = {})
  61. thread_count = args[:threads] || 8
  62. limit = args[:limit] || 20
  63. Thread.abort_on_exception = true
  64. threads = {}
  65. (1..thread_count).each do |thread|
  66. threads[thread] = Thread.new do
  67. # In some environments the Model.reset_column_information
  68. # is not reflected to threads. So an import error message appears.
  69. # Reset needed model column information for each thread.
  70. reset_database_information
  71. Thread.current[:thread_no] = thread
  72. Thread.current[:loop_count] = 0
  73. log "Importing #{remote_object} in steps of #{limit}"
  74. loop do
  75. # get the offset for the current thread and loop count
  76. thread_offset_base = (Thread.current[:thread_no] - 1) * limit
  77. thread_step = thread_count * limit
  78. offset = Thread.current[:loop_count] * thread_step + thread_offset_base
  79. break if !imported?(
  80. remote_object: remote_object,
  81. limit: limit,
  82. offset: offset,
  83. diff: args[:diff]
  84. )
  85. Thread.current[:loop_count] += 1
  86. end
  87. ActiveRecord::Base.connection.close
  88. end
  89. end
  90. (1..thread_count).each do |thread|
  91. threads[thread].join
  92. end
  93. end
  94. def limit_import(remote_object, args = {})
  95. offset = 0
  96. limit = args[:limit] || 20
  97. log "Importing #{remote_object} in steps of #{limit}"
  98. loop do
  99. break if !imported?(
  100. remote_object: remote_object,
  101. limit: limit,
  102. offset: offset,
  103. diff: args[:diff]
  104. )
  105. offset += limit
  106. end
  107. end
  108. def imported?(args)
  109. log "loading #{args[:limit]} #{args[:remote_object]} starting at #{args[:offset]}..."
  110. return false if !import_action(args[:remote_object], limit: args[:limit], offset: args[:offset], diff: args[:diff])
  111. true
  112. end
  113. def import_action(remote_object, args = {})
  114. records = Import::OTRS::Requester.load(remote_object, limit: args[:limit], offset: args[:offset], diff: args[:diff])
  115. if records.blank?
  116. log '... no more work.'
  117. return false
  118. end
  119. factory_class(remote_object).import(records)
  120. end
  121. def factory_class(object)
  122. "Import::OTRS::#{object}Factory".constantize
  123. end
  124. # sync settings
  125. def base_objects
  126. import('SysConfig')
  127. import('DynamicField')
  128. end
  129. def updateable_objects
  130. import('State')
  131. import('Priority')
  132. import('Queue')
  133. import('User')
  134. import('Customer')
  135. end
  136. def customer_user
  137. limit_import('CustomerUser', limit: 50)
  138. end
  139. def reset_database_information
  140. ::Ticket.reset_column_information
  141. end
  142. end
  143. end