client.rb 2.1 KB

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