2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2013-09-21 22:40:19 +00:00
|
|
|
class Sessions::Client
|
2013-09-29 23:46:11 +00:00
|
|
|
|
2017-12-04 07:00:00 +00:00
|
|
|
def initialize(client_id, node_id)
|
2013-09-21 22:40:19 +00:00
|
|
|
@client_id = client_id
|
2017-12-04 07:00:00 +00:00
|
|
|
@node_id = node_id
|
2015-05-07 12:10:38 +00:00
|
|
|
log '---client start ws connection---'
|
|
|
|
fetch
|
|
|
|
log '---client exiting ws connection---'
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
backends = [
|
|
|
|
'Sessions::Backend::TicketOverviewList',
|
|
|
|
'Sessions::Backend::ActivityStream',
|
|
|
|
]
|
|
|
|
|
2018-06-29 17:04:39 +00:00
|
|
|
asset_lookup = {}
|
|
|
|
backend_pool = []
|
|
|
|
user_id_last_run = nil
|
|
|
|
user_updated_at_last_run = nil
|
|
|
|
loop_count = 0
|
2015-05-05 14:10:06 +00:00
|
|
|
loop do
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2017-12-04 07:00:00 +00:00
|
|
|
# check if session still exists
|
|
|
|
return if !Sessions.session_exists?(@client_id)
|
|
|
|
|
2013-09-21 22:40:19 +00:00
|
|
|
# get connection user
|
2016-03-03 01:51:24 +00:00
|
|
|
session_data = Sessions.get(@client_id)
|
2013-09-21 22:40:19 +00:00
|
|
|
return if !session_data
|
|
|
|
return if !session_data[:user]
|
2014-10-13 23:58:21 +00:00
|
|
|
return if !session_data[:user]['id']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
user = User.lookup(id: session_data[:user]['id'])
|
2013-09-21 22:40:19 +00:00
|
|
|
return if !user
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# init new backends
|
|
|
|
if user_id_last_run != user.id
|
2014-07-13 23:29:29 +00:00
|
|
|
user_id_last_run = user.id
|
2016-03-03 08:56:13 +00:00
|
|
|
asset_lookup = {}
|
2014-07-13 23:29:29 +00:00
|
|
|
|
|
|
|
# release old objects
|
2017-10-01 12:25:52 +00:00
|
|
|
backend_pool.collect! do
|
2015-05-07 09:49:46 +00:00
|
|
|
nil
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-07-13 23:29:29 +00:00
|
|
|
|
|
|
|
# create new pool
|
2014-07-13 18:52:32 +00:00
|
|
|
backend_pool = []
|
2017-10-01 12:25:52 +00:00
|
|
|
backends.each do |backend|
|
2016-03-03 08:56:13 +00:00
|
|
|
item = backend.constantize.new(user, asset_lookup, self, @client_id)
|
2014-07-13 18:52:32 +00:00
|
|
|
backend_pool.push item
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2018-06-29 17:04:39 +00:00
|
|
|
# update user if required
|
|
|
|
elsif user_updated_at_last_run != user.updated_at
|
|
|
|
user_updated_at_last_run = user.updated_at
|
|
|
|
|
|
|
|
log "---client - updating user #{user.id} - #{user_updated_at_last_run}"
|
|
|
|
backend_pool.each do |backend|
|
|
|
|
backend.user = user
|
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
2013-09-21 22:40:19 +00:00
|
|
|
|
|
|
|
loop_count += 1
|
2015-05-07 12:10:38 +00:00
|
|
|
log "---client - looking for data of user #{user.id}"
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# push messages from backends
|
2015-05-07 10:25:16 +00:00
|
|
|
backend_pool.each(&:push)
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
log '---/client-'
|
2013-09-21 22:40:19 +00:00
|
|
|
|
|
|
|
# start faster in the beginnig
|
|
|
|
if loop_count < 20
|
|
|
|
sleep 1
|
2018-01-24 12:55:15 +00:00
|
|
|
else
|
|
|
|
sleep 2.2
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# send update to browser
|
2016-03-03 01:51:24 +00:00
|
|
|
def send(data)
|
|
|
|
Sessions.send(@client_id, data)
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2016-03-03 01:51:24 +00:00
|
|
|
def log(msg)
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "client(#{@node_id}.#{@client_id}) #{msg}" }
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|