Fixed issue #2353 - Chat does not work due to errors in WebSocket (Database connection). Credits to @d--j for providing the solution.

This commit is contained in:
Martin Edenhofer 2018-11-17 07:34:45 +01:00
parent ff60c57f3e
commit d0a36c36a8
3 changed files with 91 additions and 0 deletions

View file

@ -7,6 +7,7 @@ class Sessions::Event
begin begin
backend = load_adapter(adapter) backend = load_adapter(adapter)
rescue => e rescue => e
Rails.logger.error e
return { event: 'error', data: { error: "No such event #{params[:event]}: #{e.inspect}", payload: params[:payload] } } return { event: 'error', data: { error: "No such event #{params[:event]}: #{e.inspect}", payload: params[:payload] } }
end end
@ -16,6 +17,7 @@ class Sessions::Event
instance.destroy instance.destroy
result result
rescue => e rescue => e
Rails.logger.error e
return { event: 'error', data: { error: e.message, payload: params[:payload] } } return { event: 'error', data: { error: e.message, payload: params[:payload] } }
end end
end end

View file

@ -15,6 +15,10 @@ class Sessions::Event::Base
ActiveRecord::Base.establish_connection ActiveRecord::Base.establish_connection
end end
def self.inherited(subclass)
subclass.instance_variable_set(:@database_connection, @database_connection)
end
def websocket_send(recipient_client_id, data) def websocket_send(recipient_client_id, data)
msg = if data.class != Array msg = if data.class != Array
"[#{data.to_json}]" "[#{data.to_json}]"

View file

@ -39,6 +39,91 @@ class ChatTest < ActiveSupport::TestCase
Setting.set('chat', false) Setting.set('chat', false)
end end
test 'instance_variable test' do
assert_nil(Sessions::Event::Base.instance_variable_get(:@database_connection))
assert_equal(Sessions::Event::ChatBase.instance_variable_get(:@database_connection), true)
assert_equal(Sessions::Event::ChatStatusAgent.instance_variable_get(:@database_connection), true)
end
# check if db connection is available for chat events
# see: https://github.com/zammad/zammad/issues/2353
test 'chat event db connection test' do
class DummyWs
def send(msg)
Rails.logger.info "WS send: #{msg}"
end
end
# with websockets
assert(User.first)
message = Sessions::Event.run(
event: 'login',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {
'123' => {
websocket: DummyWs.new # to simulate a ws connection
}
},
options: {},
)
assert_equal(message, false)
assert_raises(ActiveRecord::ConnectionNotEstablished) do
User.first
end
message = Sessions::Event.run(
event: 'chat_status_customer',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {
'123' => DummyWs.new # to simulate a ws connection
},
options: {},
)
assert_equal(message[:event], 'chat_error')
assert_raises(ActiveRecord::ConnectionNotEstablished) do
User.first
end
# re-establish connection
ActiveRecord::Base.establish_connection
# with ajax long polling
assert(User.first)
message = Sessions::Event.run(
event: 'login',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {},
options: {},
)
assert_equal(message, false)
assert(User.first)
message = Sessions::Event.run(
event: 'chat_status_customer',
payload: {},
session: 123,
remote_ip: '127.0.0.1',
client_id: '123',
clients: {},
options: {},
)
assert_equal(message[:event], 'chat_error')
assert(User.first)
end
test 'default test' do test 'default test' do
chat = Chat.create_or_update( chat = Chat.create_or_update(