Skip to content

Split up the actual sending of the emails to speed things up #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/workers/protip_mailer_popular_protips_send_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ProtipMailerPopularProtipsSendWorker
include Sidekiq::Worker
sidekiq_options queue: :low

def perform(user_id, protip_ids, from, to)
fail "Only #{protip_ids.count} protips but expected 10" unless protip_ids.count == 10

begin
if REDIS.sismember(ProtipMailer::CAMPAIGN_ID, user_id.to_s)
Rails.logger.warn("Already sent email to #{user_id} please check Redis SET #{ProtipMailer::CAMPAIGN_ID}.")
else
Rails.logger.warn("Sending email to #{user_id}.")
# In development the arguments are the correct type but in production
# they have to be recast from string back to date types :D
from = Time.zone.parse(from.to_s)
to = Time.zone.parse(to.to_s)
user = User.find(user_id.to_i)
protips = Protip.where('id in (?)', protip_ids.map(&:to_i) )
fail "Only #{protips.count} protips but expected 10" unless protips.count == 10

ProtipMailer.popular_protips(user, protips, from, to).deliver
end
rescue => ex
Rails.logger.error("[ProtipMailer.popular_protips] Unable to send email due to '#{ex}' >>\n#{ex.backtrace.join("\n ")}")
Rails.logger.ap([from, to, user_id, protip_ids], :error)
end
end
end
18 changes: 5 additions & 13 deletions app/workers/protip_mailer_popular_protips_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ def perform(from, to)
from = Time.zone.parse(from.to_s)
to = Time.zone.parse(to.to_s)

protips = ProtipMailer::Queries.popular_protips(from, to)
protip_ids = ProtipMailer::Queries.popular_protips(from, to).map(&:id)

User.find_each(batch_size: 100) do |user|
begin
if REDIS.sismember(ProtipMailer::CAMPAIGN_ID, user.id.to_s)
Rails.logger.warn("Already sent email to #{user.id} please check Redis SET #{ProtipMailer::CAMPAIGN_ID}.")
else
Rails.logger.warn("Sending email to #{user.id}.")
ProtipMailer.popular_protips(user, protips, from, to).deliver
end
rescue => ex
Rails.logger.error("[ProtipMailer.popular_protips] Unable to send email due to '#{ex}' >>\n#{ex.backtrace.join("\n ")}")
Rails.logger.ap([from, to, user, protips], :error)
end
fail "Only #{protip_ids.count} protips but expected 10" unless protip_ids.count == 10

User.order('updated_at desc').find_each(batch_size: 100) do |user|
ProtipMailerPopularProtipsSendWorker.perform_async(user.id, protip_ids, from, to)
end
end
end