forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.rb
549 lines (390 loc) · 10.2 KB
/
sessions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
module Sessions
@store = case Rails.application.config.websocket_session_store
when :redis then Sessions::Store::Redis.new
else Sessions::Store::File.new
end
# create global vars for threads
@@client_threads = {} # rubocop:disable Style/ClassVars
=begin
start new session
Sessions.create(client_id, session_data, { type: 'websocket' })
returns
true|false
=end
def self.create(client_id, session, meta)
# collect session data
meta[:last_ping] = Time.now.utc.to_i
data = {
user: session,
meta: meta,
}
content = data.to_json
@store.create(client_id, content)
# send update to browser
return if !session || session['id'].blank?
send(
client_id,
{
event: 'ws:login',
data: { success: true },
}
)
end
=begin
list of all session
client_ids = Sessions.sessions
returns
['4711', '4712']
=end
def self.sessions
@store.sessions
end
=begin
list of all session
Sessions.session_exists?(client_id)
returns
true|false
=end
def self.session_exists?(client_id)
@store.session_exists?(client_id)
end
=begin
list of all session with data
client_ids_with_data = Sessions.list
returns
{
'4711' => {
user: {
'id' => 123,
},
meta: {
type: 'websocket',
last_ping: time_of_last_ping,
}
},
'4712' => {
user: {
'id' => 124,
},
meta: {
type: 'ajax',
last_ping: time_of_last_ping,
}
},
}
=end
def self.list
client_ids = sessions
session_list = {}
client_ids.each do |client_id|
data = get(client_id)
next if !data
session_list[client_id] = data
end
session_list
end
=begin
destroy session
Sessions.destroy(client_id)
returns
true|false
=end
def self.destroy(client_id)
@store.destroy(client_id)
end
=begin
destroy idle session
list_of_client_ids = Sessions.destroy_idle_sessions
returns
['4711', '4712']
=end
def self.destroy_idle_sessions(idle_time_in_sec = 240)
list_of_closed_sessions = []
clients = Sessions.list
clients.each do |client_id, client|
if !client[:meta] || !client[:meta][:last_ping] || (client[:meta][:last_ping].to_i + idle_time_in_sec) < Time.now.utc.to_i
list_of_closed_sessions.push client_id
Sessions.destroy(client_id)
end
end
list_of_closed_sessions
end
=begin
touch session
Sessions.touch(client_id)
returns
true|false
=end
def self.touch(client_id)
data = get(client_id)
return false if !data
data[:meta][:last_ping] = Time.now.utc.to_i
@store.set(client_id, data)
true
end
=begin
get session data
data = Sessions.get(client_id)
returns
{
user: {
'id' => 123,
},
meta: {
type: 'websocket',
last_ping: time_of_last_ping,
}
}
=end
def self.get(client_id)
@store.get client_id
end
=begin
send message to client
Sessions.send(client_id_of_recipient, data)
returns
true|false
=end
def self.send(client_id, data)
@store.send_data(client_id, data)
end
=begin
send message to recipient client
Sessions.send_to(user_id, data)
e. g.
Sessions.send_to(user_id, {
event: 'session_takeover',
data: {
taskbar_id: 12312
},
})
returns
true|false
=end
def self.send_to(user_id, data)
# list all current clients
client_list = sessions
client_list.each do |client_id|
session = Sessions.get(client_id)
next if !session
next if !session[:user]
next if !session[:user]['id']
next if session[:user]['id'].to_i != user_id.to_i
Sessions.send(client_id, data)
end
true
end
=begin
send message to all authenticated client
Sessions.broadcast(data)
returns
[array_with_client_ids_of_recipients]
broadcase also to not authenticated client
Sessions.broadcast(data, 'public') # public|authenticated
broadcase also not to sender
Sessions.broadcast(data, 'public', sender_user_id)
=end
def self.broadcast(data, recipient = 'authenticated', sender_user_id = nil)
# list all current clients
recipients = []
client_list = sessions
client_list.each do |client_id|
session = Sessions.get(client_id)
next if !session
if recipient != 'public'
next if session[:user].blank?
next if session[:user]['id'].blank?
end
next if sender_user_id && session[:user] && session[:user]['id'] && session[:user]['id'].to_i == sender_user_id.to_i
Sessions.send(client_id, data)
recipients.push client_id
end
recipients
end
=begin
get messages for client
messages = Sessions.queue(client_id_of_recipient)
returns
[
{
key1 => 'some data of message 1',
key2 => 'some data of message 1',
},
{
key1 => 'some data of message 2',
key2 => 'some data of message 2',
},
]
=end
def self.queue(client_id)
@store.queue(client_id)
end
=begin
remove all session and spool messages
Sessions.cleanup
=end
def self.cleanup
@store.cleanup
end
=begin
Zammad previously used a spooling mechanism for session mechanism.
The code to clean-up such data is still here, even though the mechanism
itself was removed in the meantime.
Sessions.spool_delete
=end
def self.spool_delete
@store.clear_spool
end
def self.jobs(node_id = nil)
# dispatch sessions
if node_id.blank? && ENV['ZAMMAD_SESSION_JOBS_CONCURRENT'].to_i.positive?
previous_nodes_sessions = Sessions::Node.stats
if previous_nodes_sessions.present?
log('info', "Cleaning up previous Sessions::Node sessions: #{previous_nodes_sessions}")
Sessions::Node.cleanup
end
dispatcher_pid = Process.pid
node_count = ENV['ZAMMAD_SESSION_JOBS_CONCURRENT'].to_i
node_pids = []
(1..node_count).each do |worker_node_id|
node_pids << fork do
title = "Zammad Session Jobs Node ##{worker_node_id}: dispatch_pid:#{dispatcher_pid} -> worker_pid:#{Process.pid}"
$PROGRAM_NAME = title
log('info', "#{title} started.")
::Sessions.jobs(worker_node_id)
sleep node_count
rescue Interrupt
nil
end
end
Signal.trap 'SIGTERM' do
node_pids.each do |node_pid|
Process.kill 'TERM', node_pid
end
Process.waitall
raise SignalException, 'SIGTERM'
end
# dispatch client_ids to nodes
loop do
# nodes
nodes_stats = Sessions::Node.stats
client_ids = sessions
client_ids.each do |client_id|
# ask nodes for nodes
next if nodes_stats[client_id]
# assign to node
Sessions::Node.session_assigne(client_id)
sleep 1
end
sleep 1
end
end
Thread.abort_on_exception = true
loop do
if node_id
# register node
Sessions::Node.register(node_id)
# watch for assigned sessions
client_ids = Sessions::Node.sessions_by(node_id)
else
client_ids = sessions
end
client_ids.each do |client_id|
# connection already open, ignore
next if @@client_threads[client_id]
# get current user
session_data = Sessions.get(client_id)
next if session_data.blank?
next if session_data[:user].blank?
next if session_data[:user]['id'].blank?
user = User.lookup(id: session_data[:user]['id'])
next if user.blank?
# start client thread
next if @@client_threads[client_id].present?
@@client_threads[client_id] = true
@@client_threads[client_id] = Thread.new do
thread_client(client_id, 0, Time.now.utc, node_id)
@@client_threads[client_id] = nil
log('debug', "close client (#{client_id}) thread")
if ActiveRecord::Base.connection.owner == Thread.current
ActiveRecord::Base.connection.close
end
end
sleep 1
end
sleep 1
end
end
=begin
check if thread for client_id is running
Sessions.thread_client_exists?(client_id)
returns
thread
=end
def self.thread_client_exists?(client_id)
@@client_threads[client_id]
end
=begin
start client for browser
Sessions.thread_client(client_id)
returns
thread
=end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc, node_id)
log('debug', "LOOP #{node_id}.#{client_id} - #{try_count}")
begin
Sessions::Client.new(client_id, node_id)
rescue => e
log('error', "thread_client #{client_id} exited with error #{e.inspect}")
log('error', e.backtrace.join("\n "))
sleep 10
begin
ActiveRecord::Base.connection_pool.release_connection
rescue => e
log('error', "Can't reconnect to database #{e.inspect}")
end
try_run_max = 10
try_count += 1
# reset error counter if to old
if try_run_time + (60 * 5) < Time.now.utc
try_count = 0
end
try_run_time = Time.now.utc
# restart job again
if try_run_max > try_count
thread_client(client_id, try_count, try_run_time, node_id)
end
raise "STOP thread_client for client #{node_id}.#{client_id} after #{try_run_max} tries"
end
log('debug', "/LOOP #{node_id}.#{client_id} - #{try_count}")
end
def self.symbolize_keys(hash)
hash.each_with_object({}) do |(key, value), result|
new_key = case key
when String then key.to_sym
else key
end
new_value = case value
when Hash then symbolize_keys(value)
else value
end
result[new_key] = new_value
end
end
# we use it in rails and non rails context
def self.log(level, message)
if defined?(Rails)
case level
when 'debug'
Rails.logger.debug { message }
when 'info'
Rails.logger.info message
else
Rails.logger.error message
end
return
end
puts "#{Time.now.utc.iso8601}:#{level} #{message}" # rubocop:disable Rails/Output
end
end