otrs.rb 4.8 KB

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