Browse Source

Prevent database dead locks.

Martin Edenhofer 7 years ago
parent
commit
f35645e051

+ 6 - 2
app/controllers/taskbar_controller.rb

@@ -21,14 +21,18 @@ class TaskbarController < ApplicationController
   def update
     taskbar = Taskbar.find(params[:id])
     access(taskbar)
-    taskbar.update_attributes!(Taskbar.param_cleanup(params))
+    taskbar.with_lock do
+      taskbar.update_attributes!(Taskbar.param_cleanup(params))
+    end
     model_update_render_item(taskbar)
   end
 
   def destroy
     taskbar = Taskbar.find(params[:id])
     access(taskbar)
-    taskbar.destroy
+    taskbar.with_lock do
+      taskbar.destroy
+    end
     model_destroy_render_item()
   end
 

+ 10 - 2
app/models/observer/organization/ref_object_touch.rb

@@ -24,11 +24,19 @@ class Observer::Organization::RefObjectTouch < ActiveRecord::Observer
     return if User.where(organization_id: record.id).count > 100
 
     # touch organizations tickets
-    Ticket.select('id').where(organization_id: record.id).each(&:touch)
+    Ticket.select('id').where(organization_id: record.id).pluck(:id).each { |ticket_id|
+      ticket = Ticket.find(ticket_id)
+      ticket.with_lock do
+        ticket.touch
+      end
+    }
 
     # touch current members
     record.member_ids.uniq.each { |user_id|
-      User.find(user_id).touch
+      user = User.find(user_id)
+      user.with_lock do
+        user.touch
+      end
     }
   end
 end

+ 5 - 3
app/models/taskbar.rb

@@ -88,9 +88,11 @@ class Taskbar < ApplicationModel
     # update other taskbars
     Taskbar.where(key: key).order(:created_at, :id).each { |taskbar|
       next if taskbar.id == id
-      taskbar.preferences = preferences
-      taskbar.local_update = true
-      taskbar.save!
+      taskbar.with_lock do
+        taskbar.preferences = preferences
+        taskbar.local_update = true
+        taskbar.save!
+      end
     }
 
     return true if destroyed?