client.rb 1.8 KB

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