Browse Source

Squashed commit of the following:

commit c8f7c2c2c489708c16e4eef584f45995576e2ce8
Author: Martin Edenhofer <me@edenhofer.de>
Date:   Mon Sep 5 23:23:21 2016 +0200

    Introduced Transaction.execute.
Martin Edenhofer 8 years ago
parent
commit
be67419379

+ 1 - 7
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|

+ 1 - 9
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

+ 1 - 1
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

+ 41 - 40
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

+ 18 - 1
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

+ 1 - 6
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

+ 3 - 0
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

+ 1 - 5
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'