Commit c56eca3f authored by Alex Kalderimis's avatar Alex Kalderimis

Move repository creation to the synchronized block

This ensures that we only call create on the repo once
parent abd78e4d
......@@ -98,10 +98,11 @@ module DesignManagement
CREATION_TTL = 5.seconds
RETRY_DELAY = ->(num) { 0.2.seconds * num**2 }
def self.lock_for_creation(project_id, &block)
def self.lock_for_creation(project_id, repository, &block)
key = "lock_for_creation:#{name}:{#{project_id}}"
in_lock(key, ttl: CREATION_TTL, retries: 5, sleep_sec: RETRY_DELAY) do |_retried|
repository.create_if_not_exists
yield
end
end
......
......@@ -37,7 +37,7 @@ module DesignManagement
attr_reader :designs
def delete_designs!
DesignManagement::Version.lock_for_creation(project.id) do
DesignManagement::Version.lock_for_creation(project.id, repository) do
run_actions(build_actions)
end
end
......
......@@ -7,11 +7,13 @@ module DesignManagement
# this concern requires the following methods to be implemented:
# current_user, target_branch, repository, commit_message
#
# Before calling `run_actions`, you should ensure the repository exists, by
# calling `repository.create_if_not_exists`.
#
# @raise [NoActions] if actions are empty
def run_actions(actions)
raise NoActions if actions.empty?
repository.create_if_not_exists
sha = repository.multi_action(current_user,
branch_name: target_branch,
message: commit_message,
......
......@@ -33,9 +33,7 @@ module DesignManagement
attr_reader :files
def upload_designs!
::DesignManagement::Version.lock_for_creation(project.id) do
repository.create_if_not_exists
::DesignManagement::Version.lock_for_creation(project.id, repository) do
actions = build_actions
[actions.map(&:design), actions.presence && run_actions(actions)]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment