From be6741937941c293ac01f59fe7e08fafd1214374 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 6 Sep 2016 07:51:12 +0200 Subject: [PATCH] Squashed commit of the following: commit c8f7c2c2c489708c16e4eef584f45995576e2ce8 Author: Martin Edenhofer Date: Mon Sep 5 23:23:21 2016 +0200 Introduced Transaction.execute. --- app/models/channel/email_parser.rb | 8 +- app/models/job.rb | 10 +-- .../background_job.rb | 2 +- app/models/ticket.rb | 81 ++++++++++--------- app/models/transaction.rb | 19 ++++- lib/facebook.rb | 7 +- lib/push_messages.rb | 3 + lib/tweet_base.rb | 6 +- 8 files changed, 67 insertions(+), 69 deletions(-) diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index a50f4e3a0..2db520a5c 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -419,14 +419,13 @@ retrns # set interface handle original_interface_handle = ApplicationHandleInfo.current - ApplicationHandleInfo.current = "#{original_interface_handle}.postmaster" ticket = nil article = nil session_user = nil # use transaction - ActiveRecord::Base.transaction do + Transaction.execute(interface_handle: "#{original_interface_handle}.postmaster") do # get sender user session_user_id = mail[ 'x-zammad-session-user-id'.to_sym ] @@ -552,11 +551,6 @@ retrns end end - ApplicationHandleInfo.current = original_interface_handle - - # execute object transaction - Observer::Transaction.commit - # run postmaster post filter filters = {} Setting.where(area: 'Postmaster::PostFilter').order(:name).each { |setting| diff --git a/app/models/job.rb b/app/models/job.rb index c9a34da89..ced783284 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -40,16 +40,8 @@ class Job < ApplicationModel if tickets tickets.each do |ticket| - - # use transaction - ActiveRecord::Base.transaction do - UserInfo.current_user_id = 1 + Transaction.execute(disable_notification: job.disable_notification, reset_user_id: true) do ticket.perform_changes(job.perform, 'job') - - # execute object transaction - Observer::Transaction.commit( - disable_notification: job.disable_notification - ) end 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 index 40578883c..90e931ff4 100644 --- a/app/models/observer/ticket/online_notification_seen/background_job.rb +++ b/app/models/observer/ticket/online_notification_seen/background_job.rb @@ -7,7 +7,7 @@ class Observer::Ticket::OnlineNotificationSeen::BackgroundJob def perform # set all online notifications to seen - ActiveRecord::Base.transaction do + Transaction.execute do ticket = Ticket.lookup(id: @ticket_id) OnlineNotification.list_by_object('Ticket', @ticket_id).each { |notification| next if notification.seen diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 2d5519422..3e8e49c8c 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -160,15 +160,12 @@ returns .where('pending_time <= ?', Time.zone.now) tickets.each { |ticket| - ticket.state_id = next_state_map[ticket.state_id] - ticket.updated_at = Time.zone.now - ticket.updated_by_id = 1 - ticket.save! - - # we do not have an destructor at this point, so we need to - # execute object transaction manually - Observer::Transaction.commit - + Transaction.execute do + ticket.state_id = next_state_map[ticket.state_id] + ticket.updated_at = Time.zone.now + ticket.updated_by_id = 1 + ticket.save! + end result.push ticket } end @@ -285,46 +282,50 @@ returns def merge_to(data) # update articles - Ticket::Article.where(ticket_id: id).each(&:touch) + Transaction.execute do - # quiet update of reassign of articles - Ticket::Article.where(ticket_id: id).update_all(['ticket_id = ?', data[:ticket_id] ]) + Ticket::Article.where(ticket_id: id).each(&:touch) - # touch new ticket (to broadcast change) - Ticket.find(data[:ticket_id]).touch + # quiet update of reassign of articles + Ticket::Article.where(ticket_id: id).update_all(['ticket_id = ?', data[:ticket_id]]) - # update history + # update history - # create new merge article - Ticket::Article.create( - ticket_id: id, - type_id: Ticket::Article::Type.lookup(name: 'note').id, - sender_id: Ticket::Article::Sender.lookup(name: 'Agent').id, - body: 'merged', - internal: false, - created_by_id: data[:user_id], - updated_by_id: data[:user_id], - ) + # create new merge article + Ticket::Article.create( + ticket_id: id, + type_id: Ticket::Article::Type.lookup(name: 'note').id, + sender_id: Ticket::Article::Sender.lookup(name: 'Agent').id, + body: 'merged', + internal: false, + created_by_id: data[:user_id], + updated_by_id: data[:user_id], + ) - # add history to both + # add history to both - # link tickets - Link.add( - link_type: 'parent', - link_object_source: 'Ticket', - link_object_source_value: data[:ticket_id], - link_object_target: 'Ticket', - link_object_target_value: id - ) + # link tickets + Link.add( + link_type: 'parent', + link_object_source: 'Ticket', + link_object_source_value: data[:ticket_id], + link_object_target: 'Ticket', + link_object_target_value: id + ) - # set state to 'merged' - self.state_id = Ticket::State.lookup(name: 'merged').id + # set state to 'merged' + self.state_id = Ticket::State.lookup(name: 'merged').id - # rest owner - self.owner_id = User.find_by(login: '-').id + # rest owner + self.owner_id = User.find_by(login: '-').id - # save ticket - save + # save ticket + save! + + # touch new ticket (to broadcast change) + Ticket.find(data[:ticket_id]).touch + end + true end =begin diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 9dc9b0b8b..23e1641d1 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -1,3 +1,20 @@ class Transaction - + def self.execute(options = {}) + if options[:reset_user_id] == true + UserInfo.current_user_id = 1 + end + original_interface_handle = ApplicationHandleInfo.current + if options[:interface_handle] + ApplicationHandleInfo.current = options[:interface_handle] + end + ActiveRecord::Base.transaction do + PushMessages.init + yield + if options[:interface_handle] + ApplicationHandleInfo.current = original_interface_handle + end + Observer::Transaction.commit(disable_notification: options[:disable_notification]) + PushMessages.finish + end + end end diff --git a/lib/facebook.rb b/lib/facebook.rb index 3fcda92df..13c67a54c 100644 --- a/lib/facebook.rb +++ b/lib/facebook.rb @@ -285,9 +285,7 @@ result ticket = nil # use transaction - ActiveRecord::Base.transaction do - - UserInfo.current_user_id = 1 + Transaction.execute(reset_user_id: true) do existing_article = Ticket::Article.find_by(message_id: post['id']) ticket = if existing_article existing_article.ticket @@ -295,9 +293,6 @@ result to_ticket(post, group_id, channel, page) end to_article(post, ticket, page) - - # execute object transaction - Observer::Transaction.commit end ticket diff --git a/lib/push_messages.rb b/lib/push_messages.rb index 74c9f4bda..96422053d 100644 --- a/lib/push_messages.rb +++ b/lib/push_messages.rb @@ -6,6 +6,7 @@ module PushMessages end def self.init + return true if enabled? Thread.current[:push_messages] = [] end @@ -22,6 +23,7 @@ module PushMessages end def self.finish + return false if !enabled? Thread.current[:push_messages].each { |data| Sessions.broadcast( data[:message], @@ -30,6 +32,7 @@ module PushMessages ) } Thread.current[:push_messages] = nil + true end end diff --git a/lib/tweet_base.rb b/lib/tweet_base.rb index 4a02eaedb..1244541cb 100644 --- a/lib/tweet_base.rb +++ b/lib/tweet_base.rb @@ -220,9 +220,8 @@ class TweetBase end } end - ActiveRecord::Base.transaction do - UserInfo.current_user_id = 1 + Transaction.execute(reset_user_id: true) do # check if parent exists user = to_user(tweet) @@ -251,9 +250,6 @@ class TweetBase else raise "Unknown tweet type '#{tweet.class}'" end - - # execute object transaction - Observer::Transaction.commit end if @connection_type == 'stream'