From f35645e051eb0939a08820a044f847f4c6a942f5 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 22 Aug 2017 19:36:48 +0200 Subject: [PATCH] Prevent database dead locks. --- app/controllers/taskbar_controller.rb | 8 ++++++-- app/models/observer/organization/ref_object_touch.rb | 12 ++++++++++-- app/models/taskbar.rb | 8 +++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/controllers/taskbar_controller.rb b/app/controllers/taskbar_controller.rb index dd4cd7b61..ade959ade 100644 --- a/app/controllers/taskbar_controller.rb +++ b/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 diff --git a/app/models/observer/organization/ref_object_touch.rb b/app/models/observer/organization/ref_object_touch.rb index 043113b28..beec37fd4 100644 --- a/app/models/observer/organization/ref_object_touch.rb +++ b/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 diff --git a/app/models/taskbar.rb b/app/models/taskbar.rb index f4614a15c..63c64d860 100644 --- a/app/models/taskbar.rb +++ b/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?