2023-09-06 18:51:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-09-06 20:58:30 +08:00
|
|
|
module FailedDeliveryNotifiableJob
|
2023-09-06 18:51:34 +08:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_enqueue do |job|
|
|
|
|
unless job.arguments.last.is_a?(Hash) && job.arguments.last[:user_id].present?
|
|
|
|
raise ArgumentError, 'required :user_id argument is missing! Needed for user notification in case of failure.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from StandardError do |e|
|
|
|
|
logger.error e.message
|
|
|
|
logger.error e.backtrace.join("\n")
|
|
|
|
create_failed_notification!
|
2023-09-26 19:20:15 +08:00
|
|
|
raise e
|
2023-09-06 18:51:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_failed_notification!
|
|
|
|
@user = User.find_by(id: arguments.last[:user_id])
|
|
|
|
return if @user.blank?
|
|
|
|
|
2023-12-08 20:57:43 +08:00
|
|
|
DeliveryNotification.send_notifications(
|
|
|
|
{
|
|
|
|
title: failed_notification_title,
|
|
|
|
message: failed_notification_message,
|
|
|
|
error: true,
|
|
|
|
user: @user
|
|
|
|
}
|
|
|
|
)
|
2023-09-06 18:51:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def failed_notification_title
|
|
|
|
I18n.t('activejob.failure_notifiable_job.general_notification_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_notification_message
|
|
|
|
I18n.backend.date_format = @user.settings[:date_format]
|
|
|
|
timestamp = I18n.l(enqueued_at.in_time_zone(@user.time_zone), format: :full)
|
|
|
|
I18n.t('activejob.failure_notifiable_job.general_notification_message', request_timestamp: timestamp)
|
|
|
|
ensure
|
|
|
|
I18n.backend.date_format = nil
|
|
|
|
end
|
|
|
|
end
|