From e4896fbb7efa33a4494e6e7b1a0842cafa4f2b90 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 1 May 2015 16:40:50 +0200 Subject: [PATCH] Moved to background jobs. --- .../observer/ticket/escalation_calculation.rb | 1 - .../ticket/online_notification_seen.rb | 3 +- .../background_job.rb | 10 +++++ .../observer/ticket/user_ticket_counter.rb | 31 +--------------- .../user_ticket_counter/background_job.rb | 37 +++++++++++++++++++ 5 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 app/models/observer/ticket/online_notification_seen/background_job.rb create mode 100644 app/models/observer/ticket/user_ticket_counter/background_job.rb diff --git a/app/models/observer/ticket/escalation_calculation.rb b/app/models/observer/ticket/escalation_calculation.rb index 5431d2b1f..a089c8fa2 100644 --- a/app/models/observer/ticket/escalation_calculation.rb +++ b/app/models/observer/ticket/escalation_calculation.rb @@ -37,6 +37,5 @@ class Observer::Ticket::EscalationCalculation < ActiveRecord::Observer record.callback_loop = true record.escalation_calculation record.callback_loop = false - end end diff --git a/app/models/observer/ticket/online_notification_seen.rb b/app/models/observer/ticket/online_notification_seen.rb index 2872bba29..e3cfeebb3 100644 --- a/app/models/observer/ticket/online_notification_seen.rb +++ b/app/models/observer/ticket/online_notification_seen.rb @@ -22,6 +22,7 @@ class Observer::Ticket::OnlineNotificationSeen < ActiveRecord::Observer return true if !record.online_notification_seen_state # set all online notifications to seen - OnlineNotification.seen_by_object( 'Ticket', record.id ) + # send background job + Delayed::Job.enqueue( Observer::Ticket::UserTicketCounter::BackgroundJob.new( record.id ) ) end end diff --git a/app/models/observer/ticket/online_notification_seen/background_job.rb b/app/models/observer/ticket/online_notification_seen/background_job.rb new file mode 100644 index 000000000..33bddaff0 --- /dev/null +++ b/app/models/observer/ticket/online_notification_seen/background_job.rb @@ -0,0 +1,10 @@ +class Observer::Ticket::OnlineNotificationSeen::BackgroundJob + def initialize(id) + @ticket_id = id + end + def perform + + # set all online notifications to seen + OnlineNotification.seen_by_object( 'Ticket', @ticket_id ) + end +end diff --git a/app/models/observer/ticket/user_ticket_counter.rb b/app/models/observer/ticket/user_ticket_counter.rb index 5dd0c1c09..7896d44ad 100644 --- a/app/models/observer/ticket/user_ticket_counter.rb +++ b/app/models/observer/ticket/user_ticket_counter.rb @@ -17,35 +17,8 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer return if !record.customer_id - # open ticket count - state_open = Ticket::State.by_category( 'open' ) - tickets_open = Ticket.where( - customer_id: record.customer_id, - state_id: state_open, - ).count() - - # closed ticket count - state_closed = Ticket::State.by_category( 'closed' ) - tickets_closed = Ticket.where( - customer_id: record.customer_id, - state_id: state_closed, - ).count() - - # check if update is needed - customer = User.lookup( id: record.customer_id ) - need_update = false - if customer[:preferences][:tickets_open] != tickets_open - need_update = true - customer[:preferences][:tickets_open] = tickets_open - end - if customer[:preferences][:tickets_closed] != tickets_closed - need_update = true - customer[:preferences][:tickets_closed] = tickets_closed - end - - return if !need_update - - customer.save + # send background job + Delayed::Job.enqueue( Observer::Ticket::UserTicketCounter::BackgroundJob.new( record.customer_id ) ) end end diff --git a/app/models/observer/ticket/user_ticket_counter/background_job.rb b/app/models/observer/ticket/user_ticket_counter/background_job.rb new file mode 100644 index 000000000..825f66751 --- /dev/null +++ b/app/models/observer/ticket/user_ticket_counter/background_job.rb @@ -0,0 +1,37 @@ +class Observer::Ticket::UserTicketCounter::BackgroundJob + def initialize(id) + @customer_id = id + end + def perform + + # open ticket count + state_open = Ticket::State.by_category( 'open' ) + tickets_open = Ticket.where( + customer_id: @customer_id, + state_id: state_open, + ).count() + + # closed ticket count + state_closed = Ticket::State.by_category( 'closed' ) + tickets_closed = Ticket.where( + customer_id: @customer_id, + state_id: state_closed, + ).count() + + # check if update is needed + customer = User.lookup( id: @customer_id ) + need_update = false + if customer[:preferences][:tickets_open] != tickets_open + need_update = true + customer[:preferences][:tickets_open] = tickets_open + end + if customer[:preferences][:tickets_closed] != tickets_closed + need_update = true + customer[:preferences][:tickets_closed] = tickets_closed + end + + return if !need_update + + customer.save + end +end