Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

お知らせ通知用のジョブに例外処理を追加 #7540

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/jobs/post_announcement_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@ class PostAnnouncementJob < ApplicationJob

def perform(announcement, receivers)
receivers.each do |receiver|
ActivityDelivery.with(announcement:, receiver:).notify!(:post_announcement)
send_mail_notification(announcement, receiver)
send_on_site_notification(announcement, receiver)
end
end

private

def send_mail_notification(announcement, receiver)
ActivityMailer.post_announcement(announcement:, receiver:).deliver_now
rescue Postmark::InactiveRecipientError => e
Rails.logger.warn "[Postmark] 受信者由来のエラーのためメールを送信できませんでした。:#{e.message}"
rescue StandardError => e
Rails.logger.warn(e)
end

def send_on_site_notification(announcement, receiver)
Notification.create!(
kind: Notification.kinds[:announced],
user: receiver,
sender: announcement.sender,
link: Rails.application.routes.url_helpers.polymorphic_path(announcement),
message: "お知らせ「#{announcement.title}」",
read: false
)
rescue StandardError => e
Rails.logger.warn(e)
end
end
15 changes: 0 additions & 15 deletions app/notifiers/activity_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,6 @@ def first_report(params = {})
)
end

def post_announcement(params = {})
params.merge!(@params)
announce = params[:announcement]
receiver = params[:receiver]

notification(
body: "お知らせ「#{announce.title}」",
kind: :announced,
sender: announce.user,
receiver:,
link: Rails.application.routes.url_helpers.polymorphic_path(announce),
read: false
)
end

def retired(params = {})
params.merge!(@params)
sender = params[:sender]
Expand Down
3 changes: 1 addition & 2 deletions test/jobs/post_announcement_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
class PostAnnouncementJobTest < ActiveJob::TestCase
teardown do
ActionMailer::Base.deliveries.clear
AbstractNotifier::Testing::Driver.deliveries.clear
end

test '#perform' do
announcement = announcements(:announcement1)
receivers = [users(:machida), users(:adminonly), users(:sotugyou_with_job)]

assert_difference [-> { ActionMailer::Base.deliveries.count }, -> { AbstractNotifier::Testing::Driver.deliveries.count }], 3 do
assert_difference [-> { ActionMailer::Base.deliveries.count }, -> { Notification.count }], 3 do
perform_enqueued_jobs do
PostAnnouncementJob.perform_later(announcement, receivers)
end
Expand Down
12 changes: 0 additions & 12 deletions test/notifiers/activity_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ class ActivityNotifierTest < ActiveSupport::TestCase
end
end

test '#announcement' do
notification = ActivityNotifier.with(announcement: announcements(:announcement1), receiver: users(:kimura)).post_announcement

assert_difference -> { AbstractNotifier::Testing::Driver.deliveries.count }, 1 do
notification.notify_now
end

assert_difference -> { AbstractNotifier::Testing::Driver.enqueued_deliveries.count }, 1 do
notification.notify_later
end
end

test '#hibernated' do
notification = ActivityNotifier.with(sender: users(:kimura), receiver: users(:komagata)).hibernated

Expand Down