async.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 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. data: current_state,
  41. result: 'import_done',
  42. }
  43. Rails.cache.write('import:state', result, expires_in: 10.hours)
  44. Setting.set('import_mode', false)
  45. Setting.set('system_init_done', true)
  46. end
  47. def status_bg
  48. state = Rails.cache.read('import:state')
  49. return state if state
  50. {
  51. message: 'not running',
  52. }
  53. end
  54. end
  55. end
  56. end