Change Notifications::RemoveAllJob to Notifications::RemoveAllWorker and move to sidekiq (#5451)

This commit is contained in:
Lucas Hiago 2020-01-15 14:34:12 -03:00 committed by Molly Struve
parent 2e5a36e538
commit 74f9decc56
8 changed files with 43 additions and 30 deletions

View file

@ -115,13 +115,13 @@ class Notification < ApplicationRecord
def remove_all(notifiable_ids:, notifiable_type:)
return unless %w[Article Comment Mention].include?(notifiable_type) && notifiable_ids.present?
Notifications::RemoveAllJob.perform_later(notifiable_ids, notifiable_type)
Notifications::RemoveAllWorker.perform_async(notifiable_ids, notifiable_type)
end
def remove_all_without_delay(notifiable_ids:, notifiable_type:)
return unless %w[Article Comment Mention].include?(notifiable_type) && notifiable_ids.present?
Notifications::RemoveAllJob.perform_now(notifiable_ids, notifiable_type)
Notifications::RemoveAllWorker.new.perform(notifiable_ids, notifiable_type)
end
def update_notifications(notifiable, action = nil)

View file

@ -0,0 +1,13 @@
module Notifications
class RemoveAllWorker
include Sidekiq::Worker
sidekiq_options queue: :low_priority, retry: 10
def perform(notifiable_ids, notifiable_type)
return unless %w[Article Comment Mention].include?(notifiable_type) && notifiable_ids.present?
Notifications::RemoveAll.call(Array.wrap(notifiable_ids), notifiable_type)
end
end
end

View file

@ -1,18 +0,0 @@
require "rails_helper"
RSpec.describe Notifications::RemoveAllJob, type: :job do
include_examples "#enqueues_job", "remove_all_notifications", {}
describe "#perform_now" do
let(:remove_all_service) { double }
before do
allow(remove_all_service).to receive(:call)
end
it "calls the service" do
described_class.perform_now(1, "Article", remove_all_service)
expect(remove_all_service).to have_received(:call).with([1], "Article")
end
end
end

View file

@ -309,7 +309,7 @@ RSpec.describe Comment, type: :model do
context "when callbacks are triggered after update" do
it "deletes the comment's notifications when deleted is set to true" do
create(:notification, notifiable: comment, user: user)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
comment.update(deleted: true)
end
expect(comment.notifications).to be_empty

View file

@ -504,11 +504,11 @@ RSpec.describe Notification, type: :model do
mention = create(:mention, user: user, mentionable: comment)
create(:notification, user: mention.user, notifiable: mention)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.remove_all(notifiable_ids: mention.id, notifiable_type: "Mention")
end.to change(user.notifications, :count).by(-1)
end
end
end.to change(user.notifications, :count).by(-1)
end
end

View file

@ -14,11 +14,11 @@ RSpec.describe "ArticlesDestroy", type: :request do
expect(destroyed_article).to be_nil
end
it "schedules a RemoveAllJob if there are comments" do
it "schedules a RemoveAllWorker if there are comments" do
create(:comment, commentable: article, user: user)
expect do
sidekiq_assert_enqueued_with(job: Notifications::RemoveAllWorker) do
delete "/articles/#{article.id}"
end.to have_enqueued_job(Notifications::RemoveAllJob).once
end
end
it "removes all previous published notifications" do

View file

@ -15,9 +15,9 @@ RSpec.describe Articles::Destroyer, type: :service do
it "schedules removing notifications if there are comments" do
create(:comment, commentable: article)
expect do
sidekiq_assert_enqueued_with(job: Notifications::RemoveAllWorker) do
described_class.call(article)
end.to have_enqueued_job(Notifications::RemoveAllJob).once
end
end
it "calls events dispatcher" do

View file

@ -0,0 +1,18 @@
require "rails_helper"
RSpec.describe Notifications::RemoveAllWorker, type: :woker do
include_examples "#enqueues_on_correct_queue", "low_priority", 1
describe "#perform" do
let(:worker) { subject }
before do
allow(Notifications::RemoveAll).to receive(:call)
end
it "calls the service" do
worker.perform(1, "Article")
expect(Notifications::RemoveAll).to have_received(:call).with([1], "Article")
end
end
end