From a1bde47726e82adfc9ca00c37e80671163bbe35e Mon Sep 17 00:00:00 2001 From: manu vasconcelos Date: Mon, 2 Sep 2024 15:56:56 -0300 Subject: [PATCH] Create all tags in one background job Each tag in a separate background job run means background job overhead for a small task, and it can also lead to inconsistencies --- app/models/concerns/project_media_creators.rb | 4 +++- app/models/project_media.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/project_media_creators.rb b/app/models/concerns/project_media_creators.rb index 4262f01409..0fb58650b4 100644 --- a/app/models/concerns/project_media_creators.rb +++ b/app/models/concerns/project_media_creators.rb @@ -263,6 +263,8 @@ def create_claim_description_and_fact_check end def create_tags - self.set_tags.each { |tag| GenericWorker.perform_in(1.second, 'Tag', 'create!', annotated_type: 'ProjectMedia' , annotated_id: self.id, tag: tag.strip, skip_check_ability: true) } if self.set_tags.is_a?(Array) + if self.set_tags.is_a?(Array) + GenericWorker.perform_in(1.second, 'ProjectMedia', 'create_tags_in_background', project_media_id: self.id, tags_json: self.set_tags.to_json) + end end end diff --git a/app/models/project_media.rb b/app/models/project_media.rb index 534b506698..b85e08dd07 100644 --- a/app/models/project_media.rb +++ b/app/models/project_media.rb @@ -485,6 +485,13 @@ def add_nested_objects(ms) ms.attributes[:requests] = requests end + + def self.create_tags_in_background(**params) + project_media = ProjectMedia.find(params['project_media_id']) + tags = JSON.parse(params['tags_json']) + tags.each { |tag| Tag.create! annotated: project_media, tag: tag.strip, skip_check_ability: true } + end + # private # # Please add private methods to app/models/concerns/project_media_private.rb