use new_badge_achievement_worker instead of active job (#5358) [deploy]

This commit is contained in:
serena 2020-01-07 13:16:17 +00:00 committed by Molly Struve
parent 73dccaaea6
commit 252027ad9c
8 changed files with 48 additions and 20 deletions

View file

@ -7,7 +7,7 @@ class BadgeAchievement < ApplicationRecord
validates :badge_id, uniqueness: { scope: :user_id }
after_create :notify_recipient
after_create_commit :notify_recipient
after_create :send_email_notification
after_create :award_credits
before_validation :render_rewarding_context_message_html

View file

@ -62,12 +62,8 @@ class Notification < ApplicationRecord
end
def send_new_badge_achievement_notification(badge_achievement)
Notifications::NewBadgeAchievementJob.perform_later(badge_achievement.id)
Notifications::NewBadgeAchievementWorker.perform_async(badge_achievement.id)
end
# NOTE: this alias is temporary until the transition to ActiveJob is completed
# and all old DelayedJob jobs are processed by the queue workers.
# It can be removed after pre-existing jobs are done
alias send_new_badge_notification send_new_badge_achievement_notification
def send_reaction_notification(reaction, receiver)
return if reaction.skip_notification_for?(receiver)

View file

@ -0,0 +1,11 @@
module Notifications
class NewBadgeAchievementWorker
include Sidekiq::Worker
sidekiq_options queue: :low_priority, retry: 10
def perform(badge_achievement_id)
badge_achievement = BadgeAchievement.find_by(id: badge_achievement_id)
Notifications::NewBadgeAchievement::Send.call(badge_achievement) if badge_achievement
end
end
end

View file

@ -17,4 +17,10 @@ RSpec.describe BadgeAchievement, type: :model do
it "awards credits after create" do
expect(achievement.user.credits.size).to eq(5)
end
it "notifies recipients after commit" do
allow(Notification).to receive(:send_new_badge_achievement_notification)
achievement.run_callbacks(:commit)
expect(Notification).to have_received(:send_new_badge_achievement_notification).with(achievement)
end
end

View file

@ -491,20 +491,12 @@ RSpec.describe Notification, type: :model do
describe "#send_new_badge_achievement_notification" do
it "enqueues a new badge achievement job" do
assert_enqueued_with(job: Notifications::NewBadgeAchievementJob, args: [badge_achievement.id]) do
sidekiq_assert_enqueued_with(job: Notifications::NewBadgeAchievementWorker, args: [badge_achievement.id]) do
described_class.send_new_badge_achievement_notification(badge_achievement)
end
end
end
describe "#send_new_badge_notification (deprecated)" do
it "enqueues a new badge achievement job" do
assert_enqueued_with(job: Notifications::NewBadgeAchievementJob, args: [badge_achievement.id]) do
described_class.send_new_badge_notification(badge_achievement)
end
end
end
describe "#remove_all" do
it "removes all mention related notifications" do
mention = create(:mention, user: user, mentionable: comment)

View file

@ -309,7 +309,7 @@ RSpec.describe "NotificationsIndex", type: :request do
sign_in user
badge = create(:badge)
badge_achievement = create(:badge_achievement, user: user, badge: badge)
perform_enqueued_jobs do
sidekiq_perform_enqueued_jobs do
Notification.send_new_badge_achievement_notification(badge_achievement)
end
get "/notifications"

View file

@ -40,9 +40,10 @@ RSpec.describe "Admin awards badges", type: :system do
end
it "notifies users of new badges" do
expect { award_two_badges }.to enqueue_job(Notifications::NewBadgeAchievementJob).
exactly(2).times.
and enqueue_job(BadgeAchievements::SendEmailNotificationJob).
exactly(2).times
assert_enqueued_jobs(2, only: BadgeAchievements::SendEmailNotificationJob) do
sidekiq_assert_enqueued_jobs(2) do
award_two_badges
end
end
end
end

View file

@ -0,0 +1,22 @@
require "rails_helper"
RSpec.describe Notifications::NewBadgeAchievementWorker, type: :worker do
describe "#perform" do
let(:badge_achievement) { create(:badge_achievement) }
let(:service) { Notifications::NewBadgeAchievement::Send }
let(:worker) { subject }
before do
allow(service).to receive(:call)
end
it "calls a service" do
worker.perform(badge_achievement.id)
expect(service).to have_received(:call).with(badge_achievement).once
end
it "does nothing for non-existent badge achievement" do
worker.perform(nil)
expect(service).not_to have_received(:call)
end
end
end