Added cleanups for chats.
This commit is contained in:
parent
0ec02aaace
commit
9c28ca285d
3 changed files with 94 additions and 0 deletions
|
@ -115,4 +115,55 @@ class Chat < ApplicationModel
|
|||
def self.seads_available(diff = 2.minutes)
|
||||
seads_total(diff) - active_chat_count
|
||||
end
|
||||
|
||||
=begin
|
||||
|
||||
cleanup old chat messages
|
||||
|
||||
Chat.cleanup
|
||||
|
||||
optional you can parse the max oldest chat entries
|
||||
|
||||
Chat.cleanup(3.months)
|
||||
|
||||
=end
|
||||
|
||||
def self.cleanup(diff = 3.months)
|
||||
Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session|
|
||||
Chat::Message.where(chat_session_id: chat_session.id).delete_all
|
||||
chat_session.destroy
|
||||
}
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
=begin
|
||||
|
||||
close chat sessions where participients are offline
|
||||
|
||||
Chat.cleanup_close
|
||||
|
||||
optional you can parse the max oldest chat sessions
|
||||
|
||||
Chat.cleanup_close(5.minutes)
|
||||
|
||||
=end
|
||||
|
||||
def self.cleanup_close(diff = 5.minutes)
|
||||
Chat::Session.where.not(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session|
|
||||
return if chat_session.recipients_active?
|
||||
chat_session.state = 'closed'
|
||||
chat_session.save
|
||||
message = {
|
||||
event: 'chat_session_closed',
|
||||
data: {
|
||||
session_id: chat_session.session_id,
|
||||
realname: 'System',
|
||||
},
|
||||
}
|
||||
chat_session.send_to_recipients(message)
|
||||
}
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
|
25
db/migrate/20151210000001_update_chat.rb
Normal file
25
db/migrate/20151210000001_update_chat.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
class UpdateChat < ActiveRecord::Migration
|
||||
def up
|
||||
Scheduler.create_or_update(
|
||||
name: 'Closed chat sessions where participients are offline.',
|
||||
method: 'Chat.cleanup_close',
|
||||
period: 60 * 15,
|
||||
prio: 2,
|
||||
active: true,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
Scheduler.create_or_update(
|
||||
name: 'Cleanup closed sessions.',
|
||||
method: 'Chat.cleanup',
|
||||
period: 5.days,
|
||||
prio: 2,
|
||||
active: true,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
end
|
||||
|
||||
def down
|
||||
end
|
||||
end
|
18
db/seeds.rb
18
db/seeds.rb
|
@ -3269,6 +3269,24 @@ Scheduler.create_or_update(
|
|||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
Scheduler.create_or_update(
|
||||
name: 'Closed chat sessions where participients are offline.',
|
||||
method: 'Chat.cleanup_close',
|
||||
period: 60 * 15,
|
||||
prio: 2,
|
||||
active: true,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
Scheduler.create_or_update(
|
||||
name: 'Cleanup closed sessions.',
|
||||
method: 'Chat.cleanup',
|
||||
period: 5.days,
|
||||
prio: 2,
|
||||
active: true,
|
||||
updated_by_id: 1,
|
||||
created_by_id: 1,
|
||||
)
|
||||
|
||||
# install locales and translations
|
||||
Locale.create_if_not_exists(
|
||||
|
|
Loading…
Reference in a new issue