diff --git a/app/models/concerns/project_media_creators.rb b/app/models/concerns/project_media_creators.rb index c63009722..61efc3bbd 100644 --- a/app/models/concerns/project_media_creators.rb +++ b/app/models/concerns/project_media_creators.rb @@ -264,7 +264,7 @@ def create_claim_description_and_fact_check def create_tags_in_background if self.set_tags.is_a?(Array) - GenericWorker.perform_in(1.second, 'ProjectMedia', 'create_tags', project_media_id: self.id, tags_json: self.set_tags.to_json, user_id: self.user_id) + ProjectMedia.run_later_in(1.second, 'create_tags', project_media_id: self.id, tags_json: self.set_tags.to_json, user_id: self.user_id) end end end diff --git a/config/initializers/class_extensions.rb b/config/initializers/class_extensions.rb new file mode 100644 index 000000000..461cf05ce --- /dev/null +++ b/config/initializers/class_extensions.rb @@ -0,0 +1,5 @@ +class Class + def run_later_in(time, klass_method, *method_args) + GenericWorker.perform_in(time, self.to_s, klass_method, *method_args) + end +end diff --git a/test/models/project_media_8_test.rb b/test/models/project_media_8_test.rb index 929b63ad1..854c91165 100644 --- a/test/models/project_media_8_test.rb +++ b/test/models/project_media_8_test.rb @@ -6,10 +6,10 @@ def setup require 'sidekiq/testing' end - test "create tags when project media id and tags are present" do - t = create_team - p = create_project team: t - pm = create_project_media project: p + test ":create_tags should create tags when project media id and tags are present" do + team = create_team + project = create_project team: team + pm = create_project_media project: project assert_nothing_raised do ProjectMedia.create_tags(project_media_id: pm.id, tags_json: ['one', 'two'].to_json) @@ -17,32 +17,56 @@ def setup assert_equal 2, pm.annotations('tag').count end - test "does not raise an error when no project media is sent" do + test ":create_tags should not raise an error when no project media is sent" do assert_nothing_raised do CheckSentry.expects(:notify).once ProjectMedia.create_tags(project_media_id: nil, tags_json: ['one', 'two'].to_json) end end - test "when creating an item with tag/tags, tags should be created in the background" do + test "when creating an item with tag/tags, after_create :create_tags_in_background callback should create tags in the background" do Sidekiq::Testing.inline! - t = create_team - p = create_project team: t - pm = create_project_media project: p, tags: ['one'] + team = create_team + project = create_project team: team + pm = create_project_media project: project, tags: ['one'] assert_equal 1, pm.annotations('tag').count end - test "when creating an item with multiple tags, only one job should be scheduled" do + test "when creating an item with multiple tags, after_create :create_tags_in_background callback should only schedule one job" do Sidekiq::Testing.fake! - t = create_team - p = create_project team: t + team = create_team + project = create_project team: team assert_nothing_raised do - create_project_media project: p, tags: ['one', 'two', 'three'] + create_project_media project: project, tags: ['one', 'two', 'three'] end assert_equal 1, GenericWorker.jobs.size end + + test "when using :run_later_in to schedule multiple tags creation, it should only schedule one job" do + Sidekiq::Testing.fake! + + team = create_team + project = create_project team: team + pm = create_project_media project: project + + ProjectMedia.run_later_in(0.second, 'create_tags', project_media_id: pm.id, tags_json: ['one', 'two', 'three'].to_json, user_id: pm.user_id) + + assert_equal 1, GenericWorker.jobs.size + end + + test "when using :run_later_in to schedule multiple tags creation, tags should be created" do + Sidekiq::Testing.inline! + + team = create_team + project = create_project team: team + pm = create_project_media project: project + + ProjectMedia.run_later_in(0.second, 'create_tags', project_media_id: pm.id, tags_json: ['one', 'two', 'three'].to_json, user_id: pm.user_id) + + assert_equal 3, pm.annotations('tag').count + end end