Replace Notifications::NotifiableActionJob with Notifications::NotifiableActionWorker for Sidekiq (#5588) [deploy]

This commit is contained in:
serena 2020-01-22 14:40:55 +00:00 committed by Molly Struve
parent 544bc3f9a9
commit 1297f1cb3e
7 changed files with 63 additions and 21 deletions

View file

@ -40,7 +40,7 @@ class Notification < ApplicationRecord
end
def send_to_followers(notifiable, action = nil)
Notifications::NotifiableActionJob.perform_later(notifiable.id, notifiable.class.name, action)
Notifications::NotifiableActionWorker.perform_async(notifiable.id, notifiable.class.name, action)
end
def send_new_comment_notifications_without_delay(comment)

View file

@ -0,0 +1,14 @@
module Notifications
class NotifiableActionWorker
include Sidekiq::Worker
sidekiq_options queue: :low_priority, retry: 10
def perform(notifiable_id, notifiable_type, action)
# checking type, but leaving space for notifyable types
return unless notifiable_type == "Article"
notifiable = notifiable_type.constantize.find_by(id: notifiable_id)
Notifications::NotifiableAction::Send.call(notifiable, action) if notifiable
end
end
end

View file

@ -150,7 +150,6 @@ RSpec.describe Notification, type: :model do
sidekiq_perform_enqueued_jobs do
described_class.send_new_follower_notification(user_follows_organization)
end
expect(organization.notifications.last&.user_id).to be(nil)
end
@ -409,11 +408,11 @@ RSpec.describe Notification, type: :model do
it "sends a notification to the author's followers" do
user2.follow(user)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_to_followers(article, "Published")
end.to change(user2.notifications, :count).by(1)
end
end
end.to change(user2.notifications, :count).by(1)
end
end
@ -423,21 +422,21 @@ RSpec.describe Notification, type: :model do
it "sends a notification to author's followers" do
user2.follow(user)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_to_followers(org_article, "Published")
end.to change(user2.notifications, :count).by(1)
end
end
end.to change(user2.notifications, :count).by(1)
end
it "sends a notification to the organization's followers" do
user3.follow(organization)
perform_enqueued_jobs do
expect do
expect do
sidekiq_perform_enqueued_jobs do
described_class.send_to_followers(org_article, "Published")
end.to change(user3.notifications, :count).by(1)
end
end
end.to change(user3.notifications, :count).by(1)
end
end
end
@ -446,7 +445,7 @@ RSpec.describe Notification, type: :model do
context "when there are article notifications to update" do
before do
user2.follow(user)
perform_enqueued_jobs { described_class.send_to_followers(article, "Published") }
sidekiq_perform_enqueued_jobs { described_class.send_to_followers(article, "Published") }
end
it "updates the notification with the new article title" do

View file

@ -85,7 +85,7 @@ RSpec.describe "ArticlesUpdate", type: :request do
it "creates a notification job if published" do
article.update_column(:published, false)
assert_enqueued_with(job: Notifications::NotifiableActionJob) do
sidekiq_assert_enqueued_with(job: Notifications::NotifiableActionWorker) do
put "/articles/#{article.id}", params: {
article: { published: true }
}
@ -94,7 +94,7 @@ RSpec.describe "ArticlesUpdate", type: :request do
it "removes all published notifications if unpublished" do
user2.follow(user)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
Notification.send_to_followers(article, "Published")
end
expect(article.notifications.size).to eq 1

View file

@ -369,7 +369,7 @@ RSpec.describe "NotificationsIndex", type: :request do
before do
user2.follow(user)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
Notification.send_to_followers(article, "Published")
end
sign_in user2
@ -410,7 +410,7 @@ RSpec.describe "NotificationsIndex", type: :request do
before do
user2.follow(user)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
Notification.send_to_followers(article, "Published")
end
sign_in admin

View file

@ -20,9 +20,9 @@ RSpec.describe Articles::Creator, type: :service do
it "schedules a job" do
valid_attributes[:published] = true
expect do
sidekiq_assert_enqueued_with(job: Notifications::NotifiableActionWorker) do
described_class.call(user, valid_attributes)
end.to have_enqueued_job(Notifications::NotifiableActionJob).once
end
end
it "creates a notification subscription" do

View file

@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe Notifications::NotifiableActionWorker, type: :worker do
describe "#perform" do
let(:service) { Notifications::NotifiableAction::Send }
let(:worker) { subject }
let(:action) { "Published" }
before do
allow(service).to receive(:call)
end
it "calls the service when existing notifiable passed" do
notifiable = create(:article)
worker.perform(notifiable.id, "Article", action)
expect(service).to have_received(:call).with(notifiable, action).once
end
it "doesn't call a service when notifiable doesn't exist" do
worker.perform(Article.maximum(:id).to_i + 1, "Article", action)
expect(service).not_to have_received(:call)
end
it "doesn't call a service when unexpected notifiable type passed" do
user = create(:user)
worker.perform(user.id, "User", "Upgraded")
expect(service).not_to have_received(:call)
end
end
end