async.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. module Async
  5. # rubocop:disable Style/ModuleFunction
  6. extend self
  7. def start_bg
  8. Setting.reload
  9. Import::OTRS::Requester.connection_test
  10. # start thread to observe current state
  11. status_update_thread = Thread.new do
  12. loop do
  13. result = {
  14. data: current_state,
  15. result: 'in_progress',
  16. }
  17. Rails.cache.write('import:state', result, expires_in: 10.minutes)
  18. sleep 8
  19. end
  20. end
  21. sleep 2
  22. # start import data
  23. begin
  24. Import::OTRS.start
  25. rescue => e
  26. status_update_thread.exit
  27. status_update_thread.join
  28. Rails.logger.error e
  29. result = {
  30. message: e.message,
  31. result: 'error',
  32. }
  33. Rails.cache.write('import:state', result, expires_in: 10.hours)
  34. return false
  35. end
  36. sleep 16 # wait until new finished import state is on client
  37. status_update_thread.exit
  38. status_update_thread.join
  39. result = {
  40. result: 'import_done',
  41. }
  42. Rails.cache.write('import:state', result, expires_in: 10.hours)
  43. Setting.set('system_init_done', true)
  44. Setting.set('import_mode', false)
  45. end
  46. def status_bg
  47. state = Rails.cache.read('import:state')
  48. return state if state
  49. {
  50. message: 'not running',
  51. }
  52. end
  53. end
  54. end
  55. end