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 <alexandersmith223@gmail.com>
This commit is contained in:
codekatas 2020-02-04 19:51:09 -06:00 committed by GitHub
parent 97ee0a804f
commit bae7ee8b73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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