11module EmailAllPetitionSignatories
2- extend ActiveSupport ::Concern
2+ # Concern to add shared functionality to ActiveJob classes
3+ # that are responsible for enqueuing send email jobs
34
4- #
5- # Concern to add shared functionality to ActiveJob classes that are responsible
6- # for enqueuing send email jobs
7- #
5+ extend ActiveSupport ::Concern
86
97 included do
10- queue_as :default
8+ class_attribute :email_delivery_job_class
9+ class_attribute :timestamp_name
1110
12- def self . run_later_tonight ( petition )
13- requested_at = Time . current
11+ attr_reader :petition , :requested_at
1412
15- petition . set_email_requested_at_for ( new . timestamp_name , to : requested_at )
13+ queue_as :default
14+ end
15+
16+ module ClassMethods
17+ def run_later_tonight ( **args )
18+ petition , @requested_at = args [ :petition ] , nil
1619
17- set ( wait_until : later_tonight ) .
18- perform_later ( petition , requested_at . getutc . iso8601 ( 6 ) )
20+ petition . set_email_requested_at_for ( timestamp_name , to : requested_at )
21+ set ( wait_until : later_tonight ) . perform_later ( ** args . merge ( requested_at : requested_at_iso8601 ) )
1922 end
2023
21- def self . later_tonight
22- 1 . day . from_now . at_midnight + rand ( 240 ) . minutes + rand ( 60 ) . seconds
24+ private
25+
26+ def requested_at
27+ @requested_at ||= Time . current
2328 end
24- private_class_method :later_tonight
2529
26- end
30+ def requested_at_iso8601
31+ requested_at . getutc . iso8601 ( 6 )
32+ end
2733
34+ def later_tonight
35+ midnight + random_interval
36+ end
2837
38+ def midnight
39+ requested_at . end_of_day
40+ end
2941
30- def perform ( petition , requested_at_string )
31- @petition = petition
32- @requested_at = requested_at_string . in_time_zone
33- do_work!
42+ def random_interval
43+ rand ( 240 ) . minutes + rand ( 60 ) . seconds
44+ end
3445 end
3546
36- def timestamp_name
37- raise NotImplementedError . new "Including classes must implement #timestamp_name method"
47+ def perform ( **args )
48+ @petition , @requested_at = args [ :petition ] , args [ :requested_at ]
49+
50+ # If the petition has been updated since the job
51+ # was queued then don't send the emails.
52+ unless petition_has_been_updated?
53+ enqueue_send_email_jobs
54+ end
3855 end
3956
4057 private
4158
42- attr_reader :petition , :requested_at
43-
44- def do_work!
45- return if petition_has_been_updated?
46-
47- logger . info ( "Starting #{ self . class . name } for petition '#{ petition . action } ' with email requested at: #{ petition_timestamp } " )
48- enqueue_send_email_jobs
49- logger . info ( "Finished #{ self . class . name } for petition '#{ petition . action } '" )
50-
51- end
52-
53- #
5459 # Batches the signataries to send emails to in groups of 1000
5560 # and enqueues a job to do the actual sending
56- #
5761 def enqueue_send_email_jobs
5862 signatures_to_email . find_each do |signature |
59- email_delivery_job_class . perform_later (
60- signature : signature ,
61- timestamp_name : timestamp_name ,
62- petition : petition ,
63- requested_at_as_string : requested_at . getutc . iso8601 ( 6 )
64- )
63+ email_delivery_job_class . perform_later ( **mailer_arguments ( signature ) )
6564 end
6665 end
6766
67+ def mailer_arguments ( signature )
68+ {
69+ signature : signature ,
70+ timestamp_name : timestamp_name ,
71+ petition : petition ,
72+ requested_at : requested_at
73+ }
74+ end
75+
6876 # admins can ask to send the email multiple times and each time they
6977 # ask we enqueues a new job to send out emails with a new timestamp
7078 # we want to execute only the latest job enqueued
7179 def petition_has_been_updated?
72- ( petition_timestamp - requested_at ) . abs > 1
80+ ( petition_timestamp - requested_at . in_time_zone ) . abs > 1
7381 end
7482
7583 def petition_timestamp
@@ -79,11 +87,4 @@ def petition_timestamp
7987 def signatures_to_email
8088 petition . signatures_to_email_for ( timestamp_name )
8189 end
82-
83- # The job class that handles the actual email sending for this job type
84- def email_delivery_job_class
85- raise NotImplementedError . new "Including classes must implement #email_delivery_job_class method"
86- end
8790end
88-
89-
0 commit comments