From 75d10cbbe64a87c4aa874f8310d5837c0c3aed90 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 26 Aug 2014 01:29:28 +0200 Subject: [PATCH 1/5] Removed not longer needed worker backend. Just notify client on change. --- app/models/recent_view.rb | 16 ++++++++ lib/sessions.rb | 27 ++++++++++++ lib/sessions/backend/recent_viewed.rb | 59 --------------------------- lib/sessions/client.rb | 1 - 4 files changed, 43 insertions(+), 60 deletions(-) delete mode 100644 lib/sessions/backend/recent_viewed.rb diff --git a/app/models/recent_view.rb b/app/models/recent_view.rb index f0b4f8d77..5bd6193b8 100644 --- a/app/models/recent_view.rb +++ b/app/models/recent_view.rb @@ -3,6 +3,10 @@ class RecentView < ApplicationModel belongs_to :object_lookup, :class_name => 'ObjectLookup' + after_create :notify_clients + after_update :notify_clients + after_destroy :notify_clients + def self.log( object, o_id, user ) # lookups @@ -61,6 +65,18 @@ class RecentView < ApplicationModel :assets => assets, } end + + def notify_clients + data = RecentView.list_fulldata( User.find(self.created_by_id), 10 ) + Sessions.send_to( + self.created_by_id, + { + :event => 'update_recent_viewed', + :data => data, + } + ) + end + class Object < ApplicationModel end end \ No newline at end of file diff --git a/lib/sessions.rb b/lib/sessions.rb index 64cdc5739..b0da69bcc 100644 --- a/lib/sessions.rb +++ b/lib/sessions.rb @@ -281,6 +281,33 @@ returns =begin +send message to recipient client + + Sessions.send_to(user_id, data) + +returns + + true|false + +=end + + def self.send_to( user_id, data ) + + # list all current clients + client_list = self.sessions + client_list.each {|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 ) + } + true + end + +=begin + send message to all client Sessions.broadcast(data) diff --git a/lib/sessions/backend/recent_viewed.rb b/lib/sessions/backend/recent_viewed.rb deleted file mode 100644 index f04cb4c44..000000000 --- a/lib/sessions/backend/recent_viewed.rb +++ /dev/null @@ -1,59 +0,0 @@ -class Sessions::Backend::RecentViewed - - def initialize( user, client = nil, client_id = nil ) - @user = user - @client = client - @client_id = client_id - @last_change = nil - end - - def load - - # get whole collection - recent_viewed = RecentView.list( @user, 10 ) - - # no data exists - return if !recent_viewed - return if recent_viewed.empty? - - # no change exists - return if @last_change == recent_viewed - - # remember last state - @last_change = recent_viewed - - RecentView.list_fulldata( @user, 10 ) - end - - def client_key - "as::load::#{ self.class.to_s }::#{ @user.id }::#{ @client_id }" - end - - def push - - # check timeout - timeout = Sessions::CacheIn.get( self.client_key ) - return if timeout - - # set new timeout - Sessions::CacheIn.set( self.client_key, true, { :expires_in => 15.seconds } ) - - data = self.load - - return if !data||data.empty? - - if !@client - return { - :event => 'update_recent_viewed', - :data => data, - } - end - - @client.log 'notify', "push recent_viewed for user #{ @user.id }" - @client.send({ - :event => 'update_recent_viewed', - :data => data, - }) - end - -end \ No newline at end of file diff --git a/lib/sessions/client.rb b/lib/sessions/client.rb index 7d226fdab..a0c866517 100644 --- a/lib/sessions/client.rb +++ b/lib/sessions/client.rb @@ -15,7 +15,6 @@ class Sessions::Client 'Sessions::Backend::Collections', 'Sessions::Backend::Rss', 'Sessions::Backend::ActivityStream', - 'Sessions::Backend::RecentViewed', 'Sessions::Backend::TicketCreate', ] From befd3bce0125aac9ff802469376874f70d88dee9 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 26 Aug 2014 01:58:04 +0200 Subject: [PATCH 2/5] Moved sending emails to background job. --- app/models/observer/ticket/notification.rb | 85 ++--------------- .../ticket/notification/background_job.rb | 91 +++++++++++++++++++ 2 files changed, 98 insertions(+), 78 deletions(-) create mode 100644 app/models/observer/ticket/notification/background_job.rb diff --git a/app/models/observer/ticket/notification.rb b/app/models/observer/ticket/notification.rb index f80218257..3d3f35e80 100644 --- a/app/models/observer/ticket/notification.rb +++ b/app/models/observer/ticket/notification.rb @@ -169,84 +169,13 @@ class Observer::Ticket::Notification < ActiveRecord::Observer def self.send_notify(data, ticket, article) - # find recipients - recipients = [] - - # group of agents to work on - if data[:recipient] == 'group' - recipients = ticket.agent_of_group() - - # owner - elsif data[:recipient] == 'owner' - if ticket.owner_id != 1 - recipients.push ticket.owner - end - - # customer - elsif data[:recipient] == 'customer' - if ticket.customer_id != 1 - # temporarily disabled - # recipients.push ticket.customer - end - - # owner or group of agents to work on - elsif data[:recipient] == 'to_work_on' - if ticket.owner_id != 1 - recipients.push ticket.owner - else - recipients = ticket.agent_of_group() - end - end - - # send notifications - recipient_list = '' - notification_subject = '' - recipients.each do |user| - next if !user.email || user.email == '' - - # add recipient_list - if recipient_list != '' - recipient_list += ',' - end - recipient_list += user.email.to_s - - # prepare subject & body - notification = {} - [:subject, :body].each { |key| - notification[key.to_sym] = NotificationFactory.build( - :locale => user.locale, - :string => data[key.to_sym], - :objects => { - :ticket => ticket, - :article => article, - :recipient => user, - } - ) - } - notification_subject = notification[:subject] - - # rebuild subject - notification[:subject] = ticket.subject_build( notification[:subject] ) - - # send notification - NotificationFactory.send( - :recipient => user, - :subject => notification[:subject], - :body => notification[:body] - ) - end - - # add history record - if recipient_list != '' - History.add( - :o_id => ticket.id, - :history_type => 'notification', - :history_object => 'Ticket', - :value_from => notification_subject, - :value_to => recipient_list, - :created_by_id => article.created_by_id || 1 - ) - end + # send background job + params = { + :ticket_id => ticket.id, + :article => article.id, + :data => data, + } + Delayed::Job.enqueue( Observer::Ticket::Article::Notification::BackgroundJob.new( params ) ) end def after_create(record) diff --git a/app/models/observer/ticket/notification/background_job.rb b/app/models/observer/ticket/notification/background_job.rb new file mode 100644 index 000000000..b46e70b4d --- /dev/null +++ b/app/models/observer/ticket/notification/background_job.rb @@ -0,0 +1,91 @@ +class Observer::Ticket::Article::Notification::BackgroundJob + def initialize(params) + @ticket_id = params[:ticket_id] + @article_id = params[:article_id] + @data = params[:data] + end + def perform + ticket = Ticket.find(@ticket_id) + article = Ticket::Article.find(@article_id) + data = @data + + # find recipients + recipients = [] + + # group of agents to work on + if data[:recipient] == 'group' + recipients = ticket.agent_of_group() + + # owner + elsif data[:recipient] == 'owner' + if ticket.owner_id != 1 + recipients.push ticket.owner + end + + # customer + elsif data[:recipient] == 'customer' + if ticket.customer_id != 1 + # temporarily disabled + # recipients.push ticket.customer + end + + # owner or group of agents to work on + elsif data[:recipient] == 'to_work_on' + if ticket.owner_id != 1 + recipients.push ticket.owner + else + recipients = ticket.agent_of_group() + end + end + + # send notifications + recipient_list = '' + notification_subject = '' + recipients.each do |user| + next if !user.email || user.email == '' + + # add recipient_list + if recipient_list != '' + recipient_list += ',' + end + recipient_list += user.email.to_s + + # prepare subject & body + notification = {} + [:subject, :body].each { |key| + notification[key.to_sym] = NotificationFactory.build( + :locale => user.locale, + :string => data[key.to_sym], + :objects => { + :ticket => ticket, + :article => article, + :recipient => user, + } + ) + } + notification_subject = notification[:subject] + + # rebuild subject + notification[:subject] = ticket.subject_build( notification[:subject] ) + + # send notification + NotificationFactory.send( + :recipient => user, + :subject => notification[:subject], + :body => notification[:body] + ) + end + + # add history record + if recipient_list != '' + History.add( + :o_id => ticket.id, + :history_type => 'notification', + :history_object => 'Ticket', + :value_from => notification_subject, + :value_to => recipient_list, + :created_by_id => article.created_by_id || 1 + ) + end + end +end \ No newline at end of file From 33e339546a9937e7b4e3ecc96f9df07f677467cb Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 26 Aug 2014 01:59:56 +0200 Subject: [PATCH 3/5] Removed not longer needed test. --- test/unit/session_basic_test.rb | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/test/unit/session_basic_test.rb b/test/unit/session_basic_test.rb index 442ba03c9..f42e5bcf4 100644 --- a/test/unit/session_basic_test.rb +++ b/test/unit/session_basic_test.rb @@ -257,36 +257,6 @@ class SessionBasicTest < ActiveSupport::TestCase assert( result1, "check as - recall 3" ) end - test 'b recent_viewed' do - - user = User.lookup(:id => 1) - ticket = Ticket.all.last - RecentView.log( ticket.class.to_s, ticket.id, user ) - recent_viewed_client1 = Sessions::Backend::RecentViewed.new(user, false, '123-1') - - # get as stream - result1 = recent_viewed_client1.push - assert( result1, "check recent_viewed" ) - sleep 1 - - # next check should be empty - result1 = recent_viewed_client1.push - assert( !result1, "check recent_viewed - recall" ) - - # next check should be empty - sleep 20 - result1 = recent_viewed_client1.push - assert( !result1, "check recent_viewed - recall 2" ) - - RecentView.log( ticket.class.to_s, ticket.id, user ) - - sleep 20 - - # get as stream - result1 = recent_viewed_client1.push - assert( result1, "check recent_viewed - recall 3" ) - end - test 'b ticket_create' do UserInfo.current_user_id = 1 From ccb81e6bfd63f993942f73f1fbbd31b856878ab2 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 26 Aug 2014 02:18:31 +0200 Subject: [PATCH 4/5] Fixed class name. --- app/models/observer/ticket/notification/background_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/observer/ticket/notification/background_job.rb b/app/models/observer/ticket/notification/background_job.rb index b46e70b4d..2cdf8d2db 100644 --- a/app/models/observer/ticket/notification/background_job.rb +++ b/app/models/observer/ticket/notification/background_job.rb @@ -1,4 +1,4 @@ -class Observer::Ticket::Article::Notification::BackgroundJob +class Observer::Ticket::Notification::BackgroundJob def initialize(params) @ticket_id = params[:ticket_id] @article_id = params[:article_id] From 4f531f2ad2404ffcd92dfa6953f75f41606122a5 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 26 Aug 2014 02:38:39 +0200 Subject: [PATCH 5/5] Fixed small typos. --- app/models/observer/ticket/notification.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/observer/ticket/notification.rb b/app/models/observer/ticket/notification.rb index 3d3f35e80..a6d5119fd 100644 --- a/app/models/observer/ticket/notification.rb +++ b/app/models/observer/ticket/notification.rb @@ -171,11 +171,11 @@ class Observer::Ticket::Notification < ActiveRecord::Observer # send background job params = { - :ticket_id => ticket.id, - :article => article.id, - :data => data, + :ticket_id => ticket.id, + :article_id => article.id, + :data => data, } - Delayed::Job.enqueue( Observer::Ticket::Article::Notification::BackgroundJob.new( params ) ) + Delayed::Job.enqueue( Observer::Ticket::Notification::BackgroundJob.new( params ) ) end def after_create(record)