Move Articles::DetectHumanLanguage from background job to inline (#5888) [deploy]

* Move DetectHumanLanguage inline

* Remove old DetectHumanLanguage job and spec

* Fix copy/pasta mixup...oopsie!

* Update article_spec for inline LanguageDetector
This commit is contained in:
Alex 2020-02-04 10:45:42 -08:00 committed by GitHub
parent de3ee9ebec
commit 67894aa1f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 42 deletions

View file

@ -1,12 +0,0 @@
module Articles
class DetectHumanLanguageJob < ApplicationJob
queue_as :articles_detect_human_language
def perform(article_id)
article = Article.find_by(id: article_id)
return unless article
article.update_column(:language, LanguageDetector.new(article).detect)
end
end
end

View file

@ -453,7 +453,7 @@ class Article < ApplicationRecord
def detect_human_language
return if language.present?
Articles::DetectHumanLanguageJob.perform_later(id)
update_column(:language, LanguageDetector.new(self).detect)
end
def async_score_calc

View file

@ -1,23 +0,0 @@
require "rails_helper"
RSpec.describe Articles::DetectHumanLanguageJob, type: :job do
include_examples "#enqueues_job", "articles_detect_human_language", [1]
describe "#perform_now" do
context "with article" do
let_it_be(:article) { create(:article) }
it "updates article language with detected language" do
described_class.perform_now(article.id)
expect(article.language).to eql("en")
end
end
context "without aritcle" do
it "does not error" do
expect { described_class.perform_now(nil) }.not_to raise_error
end
end
end
end

View file

@ -612,18 +612,25 @@ RSpec.describe Article, type: :model do
end
describe "detect human language" do
let(:language_detector) { instance_double(LanguageDetector) }
before do
allow(LanguageDetector).to receive(:new).and_return(language_detector)
allow(language_detector).to receive(:detect)
end
it "calls the human language detector" do
article.language = ""
assert_enqueued_with(job: Articles::DetectHumanLanguageJob) do
article.save
end
article.save
expect(language_detector).to have_received(:detect)
end
it "does not call the human language detector if there is already a language" do
article.language = "en"
assert_no_enqueued_jobs(only: Articles::DetectHumanLanguageJob) do
article.save
end
article.save
expect(language_detector).not_to have_received(:detect)
end
end
end