diff --git a/app/workers/protip_mailer_popular_protips_send_worker.rb b/app/workers/protip_mailer_popular_protips_send_worker.rb new file mode 100644 index 00000000..ae560a9a --- /dev/null +++ b/app/workers/protip_mailer_popular_protips_send_worker.rb @@ -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 diff --git a/app/workers/protip_mailer_popular_protips_worker.rb b/app/workers/protip_mailer_popular_protips_worker.rb index 32737ff6..5f89b8be 100644 --- a/app/workers/protip_mailer_popular_protips_worker.rb +++ b/app/workers/protip_mailer_popular_protips_worker.rb @@ -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