diff --git a/app/models/article.rb b/app/models/article.rb index 5e9e19b55..686814014 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -66,7 +66,7 @@ class Article < ApplicationRecord before_save :clean_data after_commit :async_score_calc after_save :bust_cache - after_save :update_main_image_background_hex + after_commit :update_main_image_background_hex after_save :detect_human_language before_save :update_cached_user before_destroy :before_destroy_actions, prepend: true @@ -447,7 +447,7 @@ class Article < ApplicationRecord def update_main_image_background_hex return if main_image.blank? || main_image_background_hex_color != "#dddddd" - Articles::UpdateMainImageBackgroundHexJob.perform_later(id) + Articles::UpdateMainImageBackgroundHexWorker.perform_async(id) end def detect_human_language diff --git a/app/workers/articles/update_main_image_background_hex_worker.rb b/app/workers/articles/update_main_image_background_hex_worker.rb new file mode 100644 index 000000000..f133a999a --- /dev/null +++ b/app/workers/articles/update_main_image_background_hex_worker.rb @@ -0,0 +1,15 @@ +module Articles + class UpdateMainImageBackgroundHexWorker + include Sidekiq::Worker + + sidekiq_options queue: :high_priority, retry: 10 + + def perform(article_id) + article = Article.find_by(id: article_id) + + return unless article + + article.update_column(:main_image_background_hex_color, ColorFromImage.new(article.main_image).main) + end + end +end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index cc05cc68c..6ed58c1a4 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -580,7 +580,7 @@ RSpec.describe Article, type: :model do it "enqueues a job to update the main image background if #dddddd" do article.main_image_background_hex_color = "#dddddd" allow(article).to receive(:update_main_image_background_hex).and_call_original - assert_enqueued_with(job: Articles::UpdateMainImageBackgroundHexJob) do + sidekiq_assert_enqueued_with(job: Articles::UpdateMainImageBackgroundHexWorker) do article.save end expect(article).to have_received(:update_main_image_background_hex) @@ -589,7 +589,7 @@ RSpec.describe Article, type: :model do it "does not enqueue a job to update the main image background if not #dddddd" do article.main_image_background_hex_color = "#fff000" allow(article).to receive(:update_main_image_background_hex).and_call_original - assert_no_enqueued_jobs(only: Articles::UpdateMainImageBackgroundHexJob) do + sidekiq_assert_no_enqueued_jobs(only: Articles::UpdateMainImageBackgroundHexWorker) do article.save end expect(article).to have_received(:update_main_image_background_hex) diff --git a/spec/workers/articles/update_main_image_background_hex_worker_spec.rb b/spec/workers/articles/update_main_image_background_hex_worker_spec.rb new file mode 100644 index 000000000..f04ef105c --- /dev/null +++ b/spec/workers/articles/update_main_image_background_hex_worker_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe Articles::UpdateMainImageBackgroundHexWorker, type: :job do + subject(:worker) { described_class.new } + + describe "#perform" do + context "with article" do + let_it_be(:article) { create(:article) } + + it "updates articles main image background hex" do + color_from_image = double + allow(color_from_image).to receive(:main).and_return("#eee") + allow(ColorFromImage).to receive(:new).and_return(color_from_image) + + worker.perform(article.id) + + expect(article.reload.main_image_background_hex_color).to eql("#eee") + end + end + + context "without article" do + it "does not error" do + expect { worker.perform(nil) }.not_to raise_error + end + end + end +end