sessions.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. module Sessions
  2. # get application root directory
  3. @root = Dir.pwd.to_s
  4. if @root.blank? || @root == '/'
  5. @root = Rails.root
  6. end
  7. # get working directories
  8. @path = "#{@root}/tmp/websocket_#{Rails.env}"
  9. # create global vars for threads
  10. @@client_threads = {} # rubocop:disable Style/ClassVars
  11. =begin
  12. start new session
  13. Sessions.create(client_id, session_data, { type: 'websocket' })
  14. returns
  15. true|false
  16. =end
  17. def self.create(client_id, session, meta)
  18. path = "#{@path}/#{client_id}"
  19. path_tmp = "#{@path}/tmp/#{client_id}"
  20. session_file = "#{path_tmp}/session"
  21. # collect session data
  22. meta[:last_ping] = Time.now.utc.to_i
  23. data = {
  24. user: session,
  25. meta: meta,
  26. }
  27. content = data.to_json
  28. # store session data in session file
  29. FileUtils.mkpath path_tmp
  30. File.open(session_file, 'wb') do |file|
  31. file.write content
  32. end
  33. # destroy old session if needed
  34. if File.exist?(path)
  35. Sessions.destroy(client_id)
  36. end
  37. # move to destination directory
  38. FileUtils.mv(path_tmp, path)
  39. # send update to browser
  40. return if !session || session['id'].blank?
  41. send(
  42. client_id,
  43. {
  44. event: 'ws:login',
  45. data: { success: true },
  46. }
  47. )
  48. end
  49. =begin
  50. list of all session
  51. client_ids = Sessions.sessions
  52. returns
  53. ['4711', '4712']
  54. =end
  55. def self.sessions
  56. path = "#{@path}/"
  57. # just make sure that spool path exists
  58. if !File.exist?(path)
  59. FileUtils.mkpath path
  60. end
  61. data = []
  62. Dir.foreach(path) do |entry|
  63. next if entry == '.'
  64. next if entry == '..'
  65. next if entry == 'tmp'
  66. next if entry == 'spool'
  67. data.push entry.to_s
  68. end
  69. data
  70. end
  71. =begin
  72. list of all session
  73. Sessions.session_exists?(client_id)
  74. returns
  75. true|false
  76. =end
  77. def self.session_exists?(client_id)
  78. session_dir = "#{@path}/#{client_id}"
  79. return false if !File.exist?(session_dir)
  80. session_file = "#{session_dir}/session"
  81. return false if !File.exist?(session_file)
  82. true
  83. end
  84. =begin
  85. list of all session with data
  86. client_ids_with_data = Sessions.list
  87. returns
  88. {
  89. '4711' => {
  90. user: {
  91. 'id' => 123,
  92. },
  93. meta: {
  94. type: 'websocket',
  95. last_ping: time_of_last_ping,
  96. }
  97. },
  98. '4712' => {
  99. user: {
  100. 'id' => 124,
  101. },
  102. meta: {
  103. type: 'ajax',
  104. last_ping: time_of_last_ping,
  105. }
  106. },
  107. }
  108. =end
  109. def self.list
  110. client_ids = sessions
  111. session_list = {}
  112. client_ids.each do |client_id|
  113. data = get(client_id)
  114. next if !data
  115. session_list[client_id] = data
  116. end
  117. session_list
  118. end
  119. =begin
  120. destroy session
  121. Sessions.destroy(client_id)
  122. returns
  123. true|false
  124. =end
  125. def self.destroy(client_id)
  126. path = "#{@path}/#{client_id}"
  127. FileUtils.rm_rf path
  128. end
  129. =begin
  130. destroy idle session
  131. list_of_client_ids = Sessions.destroy_idle_sessions
  132. returns
  133. ['4711', '4712']
  134. =end
  135. def self.destroy_idle_sessions(idle_time_in_sec = 240)
  136. list_of_closed_sessions = []
  137. clients = Sessions.list
  138. clients.each do |client_id, client|
  139. if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.now.utc.to_i
  140. list_of_closed_sessions.push client_id
  141. Sessions.destroy(client_id)
  142. end
  143. end
  144. list_of_closed_sessions
  145. end
  146. =begin
  147. touch session
  148. Sessions.touch(client_id)
  149. returns
  150. true|false
  151. =end
  152. def self.touch(client_id)
  153. data = get(client_id)
  154. return false if !data
  155. path = "#{@path}/#{client_id}"
  156. data[:meta][:last_ping] = Time.now.utc.to_i
  157. File.open("#{path}/session", 'wb' ) do |file|
  158. file.flock(File::LOCK_EX)
  159. file.write data.to_json
  160. file.flock(File::LOCK_UN)
  161. end
  162. true
  163. end
  164. =begin
  165. get session data
  166. data = Sessions.get(client_id)
  167. returns
  168. {
  169. user: {
  170. 'id' => 123,
  171. },
  172. meta: {
  173. type: 'websocket',
  174. last_ping: time_of_last_ping,
  175. }
  176. }
  177. =end
  178. def self.get(client_id)
  179. session_dir = "#{@path}/#{client_id}"
  180. session_file = "#{session_dir}/session"
  181. data = nil
  182. # if no session dir exists, session got destoried
  183. if !File.exist?(session_dir)
  184. destroy(client_id)
  185. log('debug', "missing session directory #{session_dir} for '#{client_id}', remove session.")
  186. return
  187. end
  188. # if only session file is missing, then it's an error behavior
  189. if !File.exist?(session_file)
  190. destroy(client_id)
  191. log('error', "missing session file for '#{client_id}', remove session.")
  192. return
  193. end
  194. begin
  195. File.open(session_file, 'rb') do |file|
  196. file.flock(File::LOCK_SH)
  197. all = file.read
  198. file.flock(File::LOCK_UN)
  199. data_json = JSON.parse(all)
  200. if data_json
  201. data = symbolize_keys(data_json)
  202. data[:user] = data_json['user'] # for compat. reasons
  203. end
  204. end
  205. rescue => e
  206. log('error', e.inspect)
  207. destroy(client_id)
  208. log('error', "error in reading/parsing session file '#{session_file}', remove session.")
  209. return
  210. end
  211. data
  212. end
  213. =begin
  214. send message to client
  215. Sessions.send(client_id_of_recipient, data)
  216. returns
  217. true|false
  218. =end
  219. def self.send(client_id, data)
  220. path = "#{@path}/#{client_id}/"
  221. filename = "send-#{Time.now.utc.to_f}"
  222. location = "#{path}#{filename}"
  223. check = true
  224. count = 0
  225. while check
  226. if File.exist?(location)
  227. count += 1
  228. location = "#{path}#{filename}-#{count}"
  229. else
  230. check = false
  231. end
  232. end
  233. return false if !File.directory? path
  234. begin
  235. File.open(location, 'wb') do |file|
  236. file.flock(File::LOCK_EX)
  237. file.write data.to_json
  238. file.flock(File::LOCK_UN)
  239. file.close
  240. end
  241. rescue => e
  242. log('error', e.inspect)
  243. log('error', "error in writing message file '#{location}'")
  244. return false
  245. end
  246. true
  247. end
  248. =begin
  249. send message to recipient client
  250. Sessions.send_to(user_id, data)
  251. returns
  252. true|false
  253. =end
  254. def self.send_to(user_id, data)
  255. # list all current clients
  256. client_list = sessions
  257. client_list.each do |client_id|
  258. session = Sessions.get(client_id)
  259. next if !session
  260. next if !session[:user]
  261. next if !session[:user]['id']
  262. next if session[:user]['id'].to_i != user_id.to_i
  263. Sessions.send(client_id, data)
  264. end
  265. true
  266. end
  267. =begin
  268. send message to all authenticated client
  269. Sessions.broadcast(data)
  270. returns
  271. [array_with_client_ids_of_recipients]
  272. broadcase also to not authenticated client
  273. Sessions.broadcast(data, 'public') # public|authenticated
  274. broadcase also not to sender
  275. Sessions.broadcast(data, 'public', sender_user_id)
  276. =end
  277. def self.broadcast(data, recipient = 'authenticated', sender_user_id = nil)
  278. # list all current clients
  279. recipients = []
  280. client_list = sessions
  281. client_list.each do |client_id|
  282. session = Sessions.get(client_id)
  283. next if !session
  284. if recipient != 'public'
  285. next if session[:user].blank?
  286. next if session[:user]['id'].blank?
  287. end
  288. if sender_user_id
  289. next if session[:user] && session[:user]['id'] && session[:user]['id'].to_i == sender_user_id.to_i
  290. end
  291. Sessions.send(client_id, data)
  292. recipients.push client_id
  293. end
  294. recipients
  295. end
  296. =begin
  297. get messages for client
  298. messages = Sessions.queue(client_id_of_recipient)
  299. returns
  300. [
  301. {
  302. key1 => 'some data of message 1',
  303. key2 => 'some data of message 1',
  304. },
  305. {
  306. key1 => 'some data of message 2',
  307. key2 => 'some data of message 2',
  308. },
  309. ]
  310. =end
  311. def self.queue(client_id)
  312. path = "#{@path}/#{client_id}/"
  313. data = []
  314. files = []
  315. Dir.foreach(path) do |entry|
  316. next if entry == '.'
  317. next if entry == '..'
  318. files.push entry
  319. end
  320. files.sort.each do |entry|
  321. filename = "#{path}/#{entry}"
  322. next if entry !~ /^send/
  323. message = Sessions.queue_file_read(path, entry)
  324. next if !message
  325. data.push message
  326. end
  327. data
  328. end
  329. def self.queue_file_read(path, filename)
  330. location = "#{path}#{filename}"
  331. message = ''
  332. File.open(location, 'rb') do |file|
  333. file.flock(File::LOCK_EX)
  334. message = file.read
  335. file.flock(File::LOCK_UN)
  336. end
  337. File.delete(location)
  338. return if message.blank?
  339. begin
  340. return JSON.parse(message)
  341. rescue => e
  342. log('error', "can't parse queue message: #{message}, #{e.inspect}")
  343. return
  344. end
  345. end
  346. =begin
  347. remove all session and spool messages
  348. Sessions.cleanup
  349. =end
  350. def self.cleanup
  351. return true if !File.exist?(@path)
  352. FileUtils.rm_rf @path
  353. true
  354. end
  355. def self.spool_create(data)
  356. msg = JSON.generate(data)
  357. path = "#{@path}/spool/"
  358. FileUtils.mkpath path
  359. data = {
  360. msg: msg,
  361. timestamp: Time.now.utc.to_i,
  362. }
  363. file_path = "#{path}/#{Time.now.utc.to_f}-#{rand(99_999)}"
  364. File.open(file_path, 'wb') do |file|
  365. file.flock(File::LOCK_EX)
  366. file.write data.to_json
  367. file.flock(File::LOCK_UN)
  368. end
  369. end
  370. def self.spool_list(timestamp, current_user_id)
  371. path = "#{@path}/spool/"
  372. FileUtils.mkpath path
  373. data = []
  374. to_delete = []
  375. files = []
  376. Dir.foreach(path) do |entry|
  377. next if entry == '.'
  378. next if entry == '..'
  379. files.push entry
  380. end
  381. files.sort.each do |entry|
  382. filename = "#{path}/#{entry}"
  383. next if !File.exist?(filename)
  384. File.open(filename, 'rb') do |file|
  385. file.flock(File::LOCK_SH)
  386. message = file.read
  387. file.flock(File::LOCK_UN)
  388. begin
  389. spool = JSON.parse(message)
  390. message_parsed = JSON.parse(spool['msg'])
  391. rescue => e
  392. log('error', "can't parse spool message: #{message}, #{e.inspect}")
  393. to_delete.push "#{path}/#{entry}"
  394. next
  395. end
  396. # ignore message older then 48h
  397. if spool['timestamp'] + (2 * 86_400) < Time.now.utc.to_i
  398. to_delete.push "#{path}/#{entry}"
  399. next
  400. end
  401. # add spool attribute to push spool info to clients
  402. message_parsed['spool'] = true
  403. # only send not already now messages
  404. if !timestamp || timestamp < spool['timestamp']
  405. # spool to recipient list
  406. if message_parsed['recipient'] && message_parsed['recipient']['user_id']
  407. message_parsed['recipient']['user_id'].each do |user_id|
  408. next if current_user_id != user_id
  409. message = message_parsed
  410. if message_parsed['event'] == 'broadcast'
  411. message = message_parsed['data']
  412. end
  413. item = {
  414. type: 'direct',
  415. message: message,
  416. }
  417. data.push item
  418. end
  419. # spool to every client
  420. else
  421. message = message_parsed
  422. if message_parsed['event'] == 'broadcast'
  423. message = message_parsed['data']
  424. end
  425. item = {
  426. type: 'broadcast',
  427. message: message,
  428. }
  429. data.push item
  430. end
  431. end
  432. end
  433. end
  434. to_delete.each do |file|
  435. File.delete(file)
  436. end
  437. data
  438. end
  439. def self.jobs(node_id = nil)
  440. # just make sure that spool path exists
  441. if !File.exist?(@path)
  442. FileUtils.mkpath @path
  443. end
  444. # dispatch sessions
  445. if node_id&.zero?
  446. loop do
  447. # nodes
  448. nodes_stats = Sessions::Node.stats
  449. client_ids = sessions
  450. client_ids.each do |client_id|
  451. # ask nodes for nodes
  452. next if nodes_stats[client_id]
  453. # assigne to node
  454. Sessions::Node.session_assigne(client_id)
  455. sleep 1
  456. end
  457. sleep 1
  458. end
  459. end
  460. Thread.abort_on_exception = true
  461. loop do
  462. if node_id
  463. # register node
  464. Sessions::Node.register(node_id)
  465. # watch for assigned sessions
  466. client_ids = Sessions::Node.sessions_by(node_id)
  467. else
  468. client_ids = sessions
  469. end
  470. client_ids.each do |client_id|
  471. # connection already open, ignore
  472. next if @@client_threads[client_id]
  473. # get current user
  474. session_data = Sessions.get(client_id)
  475. next if session_data.blank?
  476. next if session_data[:user].blank?
  477. next if session_data[:user]['id'].blank?
  478. user = User.lookup(id: session_data[:user]['id'])
  479. next if user.blank?
  480. # start client thread
  481. next if @@client_threads[client_id].present?
  482. @@client_threads[client_id] = true
  483. @@client_threads[client_id] = Thread.new do
  484. thread_client(client_id, 0, Time.now.utc, node_id)
  485. @@client_threads[client_id] = nil
  486. log('debug', "close client (#{client_id}) thread")
  487. if ActiveRecord::Base.connection.owner == Thread.current
  488. ActiveRecord::Base.connection.close
  489. end
  490. end
  491. sleep 1
  492. end
  493. sleep 1
  494. end
  495. end
  496. =begin
  497. check if thread for client_id is running
  498. Sessions.thread_client_exists?(client_id)
  499. returns
  500. thread
  501. =end
  502. def self.thread_client_exists?(client_id)
  503. @@client_threads[client_id]
  504. end
  505. =begin
  506. start client for browser
  507. Sessions.thread_client(client_id)
  508. returns
  509. thread
  510. =end
  511. def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc, node_id)
  512. log('debug', "LOOP #{node_id}.#{client_id} - #{try_count}")
  513. begin
  514. Sessions::Client.new(client_id, node_id)
  515. rescue => e
  516. log('error', "thread_client #{client_id} exited with error #{e.inspect}")
  517. log('error', e.backtrace.join("\n ") )
  518. sleep 10
  519. begin
  520. ActiveRecord::Base.connection_pool.release_connection
  521. rescue => e
  522. log('error', "Can't reconnect to database #{e.inspect}")
  523. end
  524. try_run_max = 10
  525. try_count += 1
  526. # reset error counter if to old
  527. if try_run_time + ( 60 * 5 ) < Time.now.utc
  528. try_count = 0
  529. end
  530. try_run_time = Time.now.utc
  531. # restart job again
  532. if try_run_max > try_count
  533. thread_client(client_id, try_count, try_run_time, node_id)
  534. end
  535. raise "STOP thread_client for client #{node_id}.#{client_id} after #{try_run_max} tries"
  536. end
  537. log('debug', "/LOOP #{node_id}.#{client_id} - #{try_count}")
  538. end
  539. def self.symbolize_keys(hash)
  540. hash.each_with_object({}) do |(key, value), result|
  541. new_key = case key
  542. when String then key.to_sym
  543. else key
  544. end
  545. new_value = case value
  546. when Hash then symbolize_keys(value)
  547. else value
  548. end
  549. result[new_key] = new_value
  550. end
  551. end
  552. # we use it in rails and non rails context
  553. def self.log(level, message)
  554. if defined?(Rails)
  555. if level == 'debug'
  556. Rails.logger.debug { message }
  557. elsif level == 'notice'
  558. Rails.logger.notice message
  559. else
  560. Rails.logger.error message
  561. end
  562. return
  563. end
  564. puts "#{Time.now.utc.iso8601}:#{level} #{message}" # rubocop:disable Rails/Output
  565. end
  566. end