Fixed thread issue with event buffer.

This commit is contained in:
Martin Edenhofer 2013-06-19 14:04:09 +02:00
parent d668fc7d6c
commit f583e7f24c
2 changed files with 23 additions and 8 deletions

View file

@ -1,20 +1,17 @@
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
require 'event_buffer'
require 'notification_factory' require 'notification_factory'
class Observer::Ticket::Notification < ActiveRecord::Observer class Observer::Ticket::Notification < ActiveRecord::Observer
observe :ticket, 'ticket::_article' observe :ticket, 'ticket::_article'
@@event_buffer = []
def self.transaction def self.transaction
# return if we run import mode # return if we run import mode
return if Setting.get('import_mode') return if Setting.get('import_mode')
# puts '@@event_buffer' EventBuffer.list.each { |event|
# puts @@event_buffer.inspect
@@event_buffer.each { |event|
# get current state of objects # get current state of objects
if event[:name] == 'Ticket::Article' if event[:name] == 'Ticket::Article'
@ -163,7 +160,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
} }
# reset buffer # reset buffer
@@event_buffer = [] EventBuffer.reset
end end
def self.send_notify(data, ticket, article) def self.send_notify(data, ticket, article)
@ -261,7 +258,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
:data => record, :data => record,
:id => record.id, :id => record.id,
} }
@@event_buffer.push e EventBuffer.add(e)
end end
def before_update(record) def before_update(record)
@ -287,7 +284,7 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
:data => record, :data => record,
:id => record.id, :id => record.id,
} }
@@event_buffer.push e EventBuffer.add(e)
end end
def after_update(record) def after_update(record)

18
lib/event_buffer.rb Normal file
View file

@ -0,0 +1,18 @@
module EventBuffer
def self.list
Thread.current[:event_buffer] || []
end
def self.add(item)
if !Thread.current[:event_buffer]
Thread.current[:event_buffer] = []
end
Thread.current[:event_buffer].push item
end
def self.reset
Thread.current[:event_buffer] = []
end
end