Skip to content

Instantly share code, notes, and snippets.

@chrishomer
Created June 23, 2011 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishomer/1041875 to your computer and use it in GitHub Desktop.
Save chrishomer/1041875 to your computer and use it in GitHub Desktop.
def self.schedule_announcement(email_name,users = nil,time_to_run = nil)
lim = Rails.env == "production" ? nil : 175
users ||= self.address_completed.select("users.id").limit(lim).all
time_to_run ||= Time.now
group_size = 50
groups = users.count / group_size
(groups + 1).times do |i|
group_start = i * group_size
group_end = group_start + group_size - 1
user_ids = users[group_start..group_end].andand.map(&:id)
Delayed::Job.enqueue(User::AnnouncementMailingJob.new(email_name,user_ids),{:priority => 5,:run_at => time_to_run}) if user_ids && user_ids.count > 0
end
end
class AnnouncementMailingJob < Struct.new(:email_name,:users)
def perform
User.where(id: users).all.each do |u|
begin
Notifications.send(email_name,{user: u.id}).deliver
rescue StandardError => e
detail = {
:parameters => {
:class => User::AnnouncementMailingJob,
:user => u.attributes,
:method => email_name
}
}
HoptoadNotifier.notify(e,detail)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment