Skip to content

Instantly share code, notes, and snippets.

@amasses
Last active December 14, 2015 05:29
Show Gist options
  • Save amasses/5036029 to your computer and use it in GitHub Desktop.
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...
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)
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