async.rb 1.5 KB

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