Skip to content

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

employ advisory lock for journal creation on attachment uploading

ulferts34ac8c9621b3
diff --git a/Gemfile b/Gemfile
index 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'
end
group :opf_plugins do
diff --git a/Gemfile.lock b/Gemfile.lock
index 4712120f9f0..4289f3f4596 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -962,6 +962,8 @@ GEM
websocket-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 @@ DEPENDENCIES
webdrivers (~> 4.2.0)
webmock (~> 3.7.2)
will_paginate (~> 3.1.7)
+ with_advisory_lock (~> 4.6.0)
RUBY VERSION
ruby 2.6.5p114
diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb
index 8bf3c8cefe2..2e8d9f6cac2 100644
--- a/app/models/journal_manager.rb
+++ b/app/models/journal_manager.rb
@@ -45,6 +45,14 @@ class JournalManager
@send_notification = true
end
+ 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
+
private
def 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.rb
index 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)
+ end
end
private
- 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_container
end
end
def add_journal
- # reload to get the newly added attachment
- container.attachments.reload
container.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 before
diff --git a/lib/api/v3/attachments/attachments_by_container_api.rb b/lib/api/v3/attachments/attachments_by_container_api.rb
index 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 API
file[: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 do
service.call uploaded_file: uploaded_file,