client.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Client
  3. def initialize(client_id, node_id)
  4. @client_id = client_id
  5. @node_id = node_id
  6. log '---client start ws connection---'
  7. fetch
  8. log '---client exiting ws connection---'
  9. end
  10. def fetch
  11. backends = [
  12. 'Sessions::Backend::TicketOverviewList',
  13. 'Sessions::Backend::ActivityStream',
  14. ]
  15. asset_lookup = {}
  16. backend_pool = []
  17. user_id_last_run = nil
  18. user_updated_at_last_run = nil
  19. loop_count = 0
  20. loop do
  21. ActiveSupport::CurrentAttributes.clear_all
  22. # check if session still exists
  23. return if !Sessions.session_exists?(@client_id)
  24. # get connection user
  25. session_data = Sessions.get(@client_id)
  26. return if !session_data
  27. return if !session_data[:user]
  28. return if !session_data[:user]['id']
  29. user = User.lookup(id: session_data[:user]['id'])
  30. return if !user
  31. UserInfo.current_user_id = user.id
  32. # init new backends
  33. if user_id_last_run != user.id
  34. user_id_last_run = user.id
  35. asset_lookup = {}
  36. # release old objects
  37. backend_pool.collect! do
  38. nil
  39. end
  40. # create new pool
  41. backend_pool = []
  42. backends.each do |backend|
  43. item = backend.constantize.new(user, asset_lookup, self, @client_id)
  44. backend_pool.push item
  45. end
  46. # update user if required
  47. elsif user_updated_at_last_run != user.updated_at
  48. user_updated_at_last_run = user.updated_at
  49. log "---client - updating user #{user.id} - #{user_updated_at_last_run}"
  50. backend_pool.each do |backend|
  51. backend.user = user
  52. end
  53. end
  54. loop_count += 1
  55. log "---client - looking for data of user #{user.id}"
  56. # push messages from backends
  57. backend_pool.each(&:push)
  58. log '---/client-'
  59. # start faster in the beginnig
  60. if loop_count < 20
  61. sleep 1
  62. else
  63. sleep 2.2
  64. end
  65. end
  66. end
  67. # send update to browser
  68. def send(data) # rubocop:disable Zammad/ForbidDefSend
  69. Sessions.send(@client_id, data)
  70. end
  71. def log(msg)
  72. Rails.logger.debug { "client(#{@node_id}.#{@client_id}) #{msg}" }
  73. end
  74. end