Move welcome notification job to worker for Sidekiq (#5465) [deploy]

This commit is contained in:
serena 2020-01-13 19:28:23 +00:00 committed by Molly Struve
parent faa696444b
commit 6e9539415e
5 changed files with 44 additions and 3 deletions

View file

@ -82,7 +82,7 @@ class Notification < ApplicationRecord
end
def send_welcome_notification(receiver_id)
Notifications::WelcomeNotificationJob.perform_later(receiver_id)
Notifications::WelcomeNotificationWorker.perform_async(receiver_id)
end
def send_moderation_notification(notifiable)

View file

@ -149,7 +149,7 @@ class User < ApplicationRecord
scope :dev_account, -> { find_by(id: SiteConfig.staff_user_id) }
after_create :send_welcome_notification
after_create_commit :send_welcome_notification
after_save :bust_cache
after_save :subscribe_to_mailchimp_newsletter
after_save :conditionally_resave_articles

View file

@ -0,0 +1,12 @@
module Notifications
class WelcomeNotificationWorker
include Sidekiq::Worker
sidekiq_options queue: :low_priority, retry: 10
def perform(receiver_id)
welcome_broadcast = Broadcast.find_by(title: "Welcome Notification")
Notifications::WelcomeNotification::Send.call(receiver_id, welcome_broadcast) if welcome_broadcast
end
end
end

View file

@ -296,7 +296,7 @@ RSpec.describe "NotificationsIndex", type: :request do
it "renders the welcome notification" do
broadcast = create(:broadcast, :onboarding)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
Notification.send_welcome_notification(user.id)
end
get "/notifications"

View file

@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe Notifications::WelcomeNotificationWorker, type: :worker do
describe "#perform" do
let!(:broadcast) { create(:broadcast, :onboarding) }
let(:user) { create(:user) }
let(:service) { Notifications::WelcomeNotification::Send }
let(:worker) { subject }
before do
allow(service).to receive(:call)
end
it "calls a service" do
worker.perform(user.id)
expect(service).to have_received(:call).with(user.id, broadcast).once
end
context "when there is a non-existent broadcast" do
before do
broadcast.destroy
end
it "does nothing" do
worker.perform(user.id)
expect(service).not_to have_received(:call)
end
end
end
end