OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
employ advisory lock for journal creation on attachment uploading
ulferts34ac8c9621b3
diff --git a/Gemfile b/Gemfileindex 1355f857c41..18219a5b71b 100644--- a/Gemfile+++ b/Gemfile@@ -289,6 +289,10 @@ platforms :mri, :mingw, :x64_mingw do# Support application loading when no database exists yet.gem 'activerecord-nulldb-adapter', '~> 0.4.0'++ # Have application level locks on the database to have a mutex shared between workers/hosts.+ # We e.g. emply this to safeguard the creation of journals.+ gem 'with_advisory_lock', '~> 4.6.0'endgroup :opf_plugins dodiff --git a/Gemfile.lock b/Gemfile.lockindex 4712120f9f0..4289f3f4596 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -962,6 +962,8 @@ GEMwebsocket-extensions (>= 0.1.0)websocket-extensions (0.1.4)will_paginate (3.1.8)+ with_advisory_lock (4.6.0)+ activerecord (>= 4.2)xpath (3.2.0)nokogiri (~> 1.8)zeitwerk (2.2.2)@@ -1133,6 +1135,7 @@ DEPENDENCIESwebdrivers (~> 4.2.0)webmock (~> 3.7.2)will_paginate (~> 3.1.7)+ with_advisory_lock (~> 4.6.0)RUBY VERSIONruby 2.6.5p114diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rbindex 8bf3c8cefe2..2e8d9f6cac2 100644--- a/app/models/journal_manager.rb+++ b/app/models/journal_manager.rb@@ -45,6 +45,14 @@ class JournalManager@send_notification = trueend+ def with_advisory_lock_transaction(container)+ ActiveRecord::Base.transaction do+ container.class.with_advisory_lock("create_journal_on_#{container.class.name}_#{container.id}") do+ yield+ end+ end+ end+privatedef merge_reference_journals_by_id(new_journals, old_journals, id_key, value)diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rbindex 6f51a82f7bb..7429727bf31 100644--- a/app/services/attachments/create_service.rb+++ b/app/services/attachments/create_service.rb@@ -40,29 +40,44 @@ class Attachments::CreateService## An ActiveRecord::RecordInvalid error is raised if any record can't be saved.def call(uploaded_file:, description:)- attachment = Attachment.new(file: uploaded_file,- container: container,- description: description,- author: author)- save attachment-- attachment+ if container.respond_to? :add_journal+ save_journalized(uploaded_file, description)+ else+ save_unjournalized(uploaded_file, description)+ endendprivate- def save(attachment)- ActiveRecord::Base.transaction do- attachment.save!+ def save_journalized(uploaded_file, description)+ JournalManager.with_advisory_lock_transaction(container) do+ # Get the latest attachments to ensure having them all for journalization.+ # A different worker might have added attachments in the meantime, e.g when bulk uploading+ container.attachments.reload+ attachment = build_attachment(uploaded_file, description)++ add_journal+ save_container++ attachment+ end+ end- add_journal if container.respond_to? :add_journal+ def save_unjournalized(uploaded_file, description)+ build_attachment(uploaded_file, description).tap do+ save_containerendenddef add_journal- # reload to get the newly added attachment- container.attachments.reloadcontainer.add_journal author+ end++ def build_attachment(uploaded_file, description)+ container.attachments.build(file: uploaded_file, description: description, author: author)+ end++ def save_container# We allow invalid containers to be saved as# adding the attachments does not change the validity of the container# but without that leeway, the user needs to fix the container beforediff --git a/lib/api/v3/attachments/attachments_by_container_api.rb b/lib/api/v3/attachments/attachments_by_container_api.rbindex 3f41ee9c868..4e987ed9f70 100644--- a/lib/api/v3/attachments/attachments_by_container_api.rb+++ b/lib/api/v3/attachments/attachments_by_container_api.rb@@ -79,7 +79,7 @@ module APIfile[:type],file_name: metadata.file_name.to_s- service = Attachments::CreateService.new(container, author: current_user)+ service = ::Attachments::CreateService.new(container, author: current_user)with_handled_create_errors doservice.call uploaded_file: uploaded_file,