Improved background job.

This commit is contained in:
Martin Edenhofer 2015-09-29 23:21:12 +02:00
parent f2d1882238
commit e38ca9c2b8
2 changed files with 15 additions and 9 deletions

View file

@ -19,7 +19,12 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
return if !record.customer_id return if !record.customer_id
# send background job # send background job
Delayed::Job.enqueue( Observer::Ticket::UserTicketCounter::BackgroundJob.new( record.customer_id ) ) Delayed::Job.enqueue(
Observer::Ticket::UserTicketCounter::BackgroundJob.new(
record.customer_id,
UserInfo.current_user_id || record.updated_by_id,
)
)
end end
end end

View file

@ -1,20 +1,21 @@
class Observer::Ticket::UserTicketCounter::BackgroundJob class Observer::Ticket::UserTicketCounter::BackgroundJob
def initialize(id) def initialize(customer_id, updated_by_id)
@customer_id = id @customer_id = customer_id
@updated_by_id = updated_by_id
end end
def perform def perform
# open ticket count # open ticket count
state_open = Ticket::State.by_category( 'open' ) state_open = Ticket::State.by_category( 'open' )
tickets_open = Ticket.where( tickets_open = Ticket.where(
customer_id: @customer_id, customer_id: @customer_id,
state_id: state_open, state_id: state_open,
).count() ).count()
# closed ticket count # closed ticket count
state_closed = Ticket::State.by_category( 'closed' ) state_closed = Ticket::State.by_category( 'closed' )
tickets_closed = Ticket.where( tickets_closed = Ticket.where(
customer_id: @customer_id, customer_id: @customer_id,
state_id: state_closed, state_id: state_closed,
).count() ).count()
@ -24,7 +25,7 @@ class Observer::Ticket::UserTicketCounter::BackgroundJob
need_update = false need_update = false
if customer[:preferences][:tickets_open] != tickets_open if customer[:preferences][:tickets_open] != tickets_open
need_update = true need_update = true
customer[:preferences][:tickets_open] = tickets_open customer[:preferences][:tickets_open] = tickets_open
end end
if customer[:preferences][:tickets_closed] != tickets_closed if customer[:preferences][:tickets_closed] != tickets_closed
need_update = true need_update = true
@ -32,7 +33,7 @@ class Observer::Ticket::UserTicketCounter::BackgroundJob
end end
return if !need_update return if !need_update
customer.updated_by_id = @updated_by_id
customer.save customer.save
end end
end end