@@ -4,7 +4,25 @@ class SendPushCreatedEmailJob < ApplicationJob
44 queue_as :default
55
66 def perform ( notify_by_email_id )
7- notify_by_email = NotifyByEmail . find_by! ( id : notify_by_email_id )
7+ notify_by_email = NotifyByEmail . find_by ( id : notify_by_email_id )
8+
9+ if notify_by_email . nil?
10+ Rails . logger . error "[SendPushCreatedEmailJob] NotifyByEmail not found: #{ notify_by_email_id } "
11+
12+ return
13+ end
14+
15+ if notify_by_email . recipients . blank?
16+ notify_by_email . update ( status : :failed , error_message : _ ( "No recipients found." ) )
17+
18+ return
19+ end
20+
21+ unless Settings . notify_by_email_available?
22+ notify_by_email . update ( status : :failed , error_message : _ ( "Email notifications are not available." ) )
23+
24+ return
25+ end
826
927 return unless notify_by_email . pending?
1028
@@ -14,6 +32,12 @@ def perform(notify_by_email_id)
1432 locale = notify_by_email . locale
1533 recipients = notify_by_email . recipients . split ( "," ) . map ( &:strip )
1634
35+ if push . expired?
36+ notify_by_email . update ( status : :failed , error_message : _ ( "Push already expired." ) )
37+
38+ return
39+ end
40+
1741 successful_sends = [ ]
1842 recipients . each do |recipient |
1943 mail = PushCreatedMailer . with ( push : push , recipient : recipient , locale : locale ) . notify
@@ -23,14 +47,19 @@ def perform(notify_by_email_id)
2347 Rails . logger . error "[SendPushCreatedEmailJob] Error sending email: #{ e . message } "
2448 end
2549
26- status = if successful_sends . size == recipients . size
27- :completed
50+ status , error_message = if successful_sends . size == recipients . size
51+ [ :completed , nil ]
2852 elsif successful_sends . empty?
29- :fully_failed
53+ [ :failed , I18n . _ ( "No emails were sent successfully." ) ]
3054 else
31- :partially_failed
55+ [ :partially_failed , I18n . _ ( "Some emails could not be sent." ) ]
3256 end
3357
34- notify_by_email . update! ( successful_sends : successful_sends . join ( "," ) , status : status , proceed_at : Time . current )
58+ notify_by_email . update! ( successful_sends : successful_sends . join ( "," ) , status : status , error_message : error_message , proceed_at : Time . current )
3559 end
60+
61+ rescue => e
62+ Rails . logger . error "[SendPushCreatedEmailJob] Error sending email: #{ e . message } "
63+
64+ notify_by_email . update ( status : :failed , error_message : e . message )
3665end
0 commit comments