node.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Sessions::Node
  3. @store = case Rails.application.config.websocket_session_store
  4. when :redis then Sessions::Store::Redis.new
  5. else Sessions::Store::File.new
  6. end
  7. def self.session_assigne(client_id, force = false)
  8. # get available nodes
  9. nodes = Sessions::Node.registered
  10. session_count = {}
  11. nodes.each do |node|
  12. count = Sessions::Node.sessions_by(node['node_id'], force).count
  13. session_count[node['node_id']] = count
  14. end
  15. # search for lowest session count
  16. node_id = nil
  17. node_count = nil
  18. session_count.each do |local_node_id, count|
  19. next if !node_count.nil? && count > node_count
  20. node_count = count
  21. node_id = local_node_id
  22. end
  23. # assigne session
  24. Rails.logger.info "Assigne session to node #{node_id} (#{client_id})"
  25. Sessions::Node.sessions_for(node_id, client_id)
  26. # write node status file
  27. node_id
  28. end
  29. def self.cleanup
  30. @store.clear_nodes
  31. end
  32. def self.registered
  33. @store.nodes
  34. end
  35. def self.register(node_id)
  36. data = {
  37. updated_at_human: Time.now.utc,
  38. updated_at: Time.now.utc.to_i,
  39. node_id: node_id.to_s,
  40. pid: $PROCESS_ID,
  41. }
  42. @store.add_node node_id, data
  43. end
  44. def self.stats
  45. sessions = {}
  46. @store.each_node_session do |data|
  47. next if data['client_id'].blank?
  48. sessions[data['client_id']] = data['node_id']
  49. end
  50. sessions
  51. end
  52. def self.sessions_for(node_id, client_id)
  53. # write node status file
  54. data = {
  55. updated_at_human: Time.now.utc,
  56. updated_at: Time.now.utc.to_i,
  57. node_id: node_id.to_s,
  58. client_id: client_id.to_s,
  59. pid: $PROCESS_ID,
  60. }
  61. @store.create_node_session node_id, client_id, data
  62. end
  63. def self.sessions_by(node_id, force = false)
  64. sessions = []
  65. @store.each_session_by_node(node_id) do |data|
  66. next if data['client_id'].blank?
  67. next if !Sessions.session_exists?(data['client_id']) && force == false
  68. sessions.push data['client_id']
  69. end
  70. sessions
  71. end
  72. end