From bae7ee8b73231629498e47650b661f1d3c021c52 Mon Sep 17 00:00:00 2001 From: codekatas <59459699+codekatas-magmalabs@users.noreply.github.com> Date: Tue, 4 Feb 2020 19:51:09 -0600 Subject: [PATCH] Migrate update main image background job to sidekiq (#5345) [deploy] * Add new UpdateMainImageBackgroundHexWorker worker class This is a port of app/jobs/update_main_image_background_job.rb that works with Sidekiq instead of DelayedJob. * Update all references to UpdateMainImageBackgroundHexJob This updates all references to UpdateMainImageBackgroundHexJob so they now call UpdateMainImageBackgroundHexWorker, which works with Sidekiq instead of DelayedJob. * Move method call to after_commit callback * Fix article specs with sidekiq helpers Co-authored-by: Alex --- app/models/article.rb | 4 +-- ...update_main_image_background_hex_worker.rb | 15 +++++++++++ spec/models/article_spec.rb | 4 +-- ...e_main_image_background_hex_worker_spec.rb | 27 +++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 app/workers/articles/update_main_image_background_hex_worker.rb create mode 100644 spec/workers/articles/update_main_image_background_hex_worker_spec.rb 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