Skip to content

Commit 1592cd2

Browse files
committed
Added worker and schedule for running the popular protips emailer
1 parent 6316853 commit 1592cd2

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

app/clock.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
RefreshStaleUsersWorker.perform_async
1616
end
1717

18+
# On the first of every month send the popular protips from the previous month.
19+
every(1.day, 'protip_mailer:popular_protips', if: ->(t){ t.day == 1 }) do
20+
last_month = 1.month.ago
21+
ProtipMailerPopularProtipsWorker.perform_async(last_month.beginning_of_month, last_month.end_of_month)
22+
end
23+
1824
every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '00:00') {}
1925
every(1.day, 'clear_expired_sessions', at: '00:00') {}
2026
every(1.day, 'facts:system', at: '00:00') {}

app/mailers/protip_mailer.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ class ProtipMailer < ActionMailer::Base
1616
endorsements: 'endorsements',
1717
protips_count: 'protips'
1818
}
19+
ACTIVITY_SUBJECT_PREFIX = '[Coderwall]'
1920

2021
#################################################################################
2122
def popular_protips(user, protips, from, to)
22-
fail "Protips are required." if protips.nil? || protips.empty?
23-
headers['X-Mailgun-Campaign-Id'] = 'coderwall-popular_protips'
23+
fail 'Protips are required.' if protips.nil? || protips.empty?
24+
fail 'User is required.' unless user
25+
fail 'From date is required.' unless from
26+
fail 'To date is required.' unless to
27+
28+
headers['X-Mailgun-Campaign-Id'] = 'protip_mailer-popular_protips'
2429

2530
@user = user
2631
@protips = protips
@@ -31,13 +36,21 @@ def popular_protips(user, protips, from, to)
3136
@star_stat = star_stat_for_this_week
3237
@star_stat_string = STARS[@star_stat]
3338

34-
@most = star_stats(stars).sort_by { |star| -star[@star_stat] }.first
39+
@most = star_stats(stars).sort_by do |star|
40+
-star[@star_stat]
41+
end.first
3542
@most = nil if @most && (@most[@star_stat] <= 0)
3643

37-
mail to: 'mike@just3ws.com', subject: 'Popular Protips on Coderwall'
44+
mail(to: @user.email, subject: "#{ACTIVITY_SUBJECT_PREFIX} Popular Protips on Coderwall")
45+
rescue Exception => ex
46+
abort_delivery(ex)
3847
end
3948
#################################################################################
4049

50+
def abort_delivery(ex)
51+
Rails.logger.error("[ProtipMailer.popular_protips] Aborted email '#{ex}' >>\n#{ex.backtrace.join("\n ")}")
52+
end
53+
4154
def campaign_params
4255
{
4356
utm_campaign: 'coderwall-popular_protips',
@@ -65,7 +78,7 @@ def get_team_and_job_for(user)
6578
teams = teams_for_user(user)
6679
teams.each do |team|
6780
best_job = team.best_positions_for(user).detect do |job|
68-
(job.team_document_id == user.team_document_id) || !already_sent?(job, user)
81+
job.team_document_id == user.team_document_id
6982
end
7083
return [team, best_job] unless best_job.nil?
7184
end
@@ -79,16 +92,12 @@ def teams_for_user(user)
7992
end
8093
end
8194

82-
def already_sent?(mailable, user)
83-
SentMail.where(user_id: user.id, mailable_id: mailable.id, mailable_type: mailable.class.name).exists?
84-
end
85-
8695
module Queries
8796
def self.popular_protips(from, to)
8897
search_results = ProtipMailer::Queries.search_for_popular_protips(from, to)
8998
public_ids = search_results.map { |protip| protip['public_id'] }
9099

91-
Protip.eager_load(:user, :comments).where("public_id in (?)", public_ids)
100+
Protip.eager_load(:user, :comments).where('public_id in (?)', public_ids)
92101
end
93102

94103
def self.search_for_popular_protips(from, to, max_results=10)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class ProtipMailerPopularProtipsWorker
2+
include Sidekiq::Worker
3+
sidekiq_options queue: :low
4+
5+
def perform(from, to)
6+
protips = ProtipMailer::Queries.popular_protips(from, to)
7+
8+
User.find_each(batch_size: 100) do |user|
9+
ProtipMailer.popular_protips(user, protips, from, to).deliver
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)