Last active
December 14, 2015 05:29
-
-
Save amasses/5036029 to your computer and use it in GitHub Desktop.
A minor extension to Sidekiq's #delay_until to make delaying emails until a reasonable time easier...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sidekiq | |
module Extensions | |
module Klass | |
def delay_until_reasonable(options = {}) | |
reasonable_time = case Time.now.hour | |
when 0..7 | |
Time.now.change(hour: 8) | |
when 22..24 | |
Date.tomorrow.to_time.change(hour: 8) | |
else | |
Time.now | |
end | |
Proxy.new(DelayedMailer, self, options.merge(at: reasonable_time.to_f)) | |
end | |
end | |
end | |
end | |
Module.send(:include, Sidekiq::Extensions::Klass) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'delay_until_reasonable' # You can include this globally in your application.rb file. | |
# In a sidekiq job scheduled for... say 3am | |
invoices = Invoice.unpaid.overdue | |
invoices.each do |invoice| | |
invoice.add_note("Your invoice is now overdue....") | |
InvoiceMailer.delay_until_reasonable.payment_reminder(invoice.id) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment