diff --git a/app/models/team.rb b/app/models/team.rb index 146ea3c43..dd1fa2464 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -315,7 +315,7 @@ class Team < ApplicationRecord def generate_template_project return if without_templates - TemplatesService.new.update_team(self) + TemplatesService.new.delay(queue: :templates).update_team(self) end include FirstTimeDataGenerator diff --git a/app/services/templates_service.rb b/app/services/templates_service.rb index 3ec39599c..a6a025ffc 100644 --- a/app/services/templates_service.rb +++ b/app/services/templates_service.rb @@ -32,13 +32,16 @@ class TemplatesService return unless owner.present? updated = false exp_tmplt_dir_prefix = "#{@base_dir}/experiment_" - existing = tmpl_project.experiments.where.not(uuid: nil).pluck(:uuid) - @experiment_templates.except(*existing).each_value do |id| - importer_service = TeamImporter.new - importer_service.import_experiment_template_from_dir( - exp_tmplt_dir_prefix + id.to_s, tmpl_project.id, owner.id - ) - updated = true + # Create lock in case another worker starts to update same team + tmpl_project.with_lock do + existing = tmpl_project.experiments.where.not(uuid: nil).pluck(:uuid) + @experiment_templates.except(*existing).each_value do |id| + importer_service = TeamImporter.new + importer_service.import_experiment_template_from_dir( + exp_tmplt_dir_prefix + id.to_s, tmpl_project.id, owner.id + ) + updated = true + end end updated end