client.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. user_updated_at_last_run = nil
  20. loop_count = 0
  21. loop do
  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. # init new backends
  32. if user_id_last_run != user.id
  33. user_id_last_run = user.id
  34. asset_lookup = {}
  35. # release old objects
  36. backend_pool.collect! do
  37. nil
  38. end
  39. # create new pool
  40. backend_pool = []
  41. backends.each do |backend|
  42. item = backend.constantize.new(user, asset_lookup, self, @client_id)
  43. backend_pool.push item
  44. end
  45. # update user if required
  46. elsif user_updated_at_last_run != user.updated_at
  47. user_updated_at_last_run = user.updated_at
  48. log "---client - updating user #{user.id} - #{user_updated_at_last_run}"
  49. backend_pool.each do |backend|
  50. backend.user = user
  51. end
  52. end
  53. loop_count += 1
  54. log "---client - looking for data of user #{user.id}"
  55. # push messages from backends
  56. backend_pool.each(&:push)
  57. log '---/client-'
  58. # start faster in the beginnig
  59. if loop_count < 20
  60. sleep 1
  61. else
  62. sleep 2.2
  63. end
  64. end
  65. end
  66. # send update to browser
  67. def send(data)
  68. Sessions.send(@client_id, data)
  69. end
  70. def log(msg)
  71. Rails.logger.debug { "client(#{@node_id}.#{@client_id}) #{msg}" }
  72. end
  73. end