pace out the email sending

This commit is contained in:
Kyle Drake 2015-06-29 13:28:59 -07:00
parent da4c51e1ef
commit cff1c3a20b
3 changed files with 10 additions and 8 deletions

View file

@ -25,6 +25,7 @@ post '/admin/email' do
day = 0
until sites.empty?
seconds = 0.0
queued_sites = []
Site::EMAIL_BLAST_MAXIMUM_PER_DAY.times {
break if sites.empty?
@ -32,12 +33,13 @@ post '/admin/email' do
}
queued_sites.each do |site|
EmailWorker.perform_at(day.days.from_now, {
from: 'noreply@neocities.org',
EmailWorker.perform_at((day.days.from_now + seconds), {
from: 'Kyle from Neocities <kyle@neocities.org>',
to: site.email,
subject: params[:subject],
body: params[:body]
})
seconds += 0.5
end
day += 1

View file

@ -154,7 +154,7 @@ class Site < Sequel::Model
if ENV['RACK_ENV'] == 'test'
EMAIL_BLAST_MAXIMUM_PER_DAY = 2
else
EMAIL_BLAST_MAXIMUM_PER_DAY = 2000
EMAIL_BLAST_MAXIMUM_PER_DAY = 1000
end
many_to_many :tags

View file

@ -41,16 +41,16 @@ describe 'email blasting' do
relevant_jobs.each do |job|
args = job['args'].first
args['from'].must_equal 'noreply@neocities.org'
args['from'].must_equal 'Kyle from Neocities <kyle@neocities.org>'
args['subject'].must_equal 'Subject Test'
args['body'].must_equal 'Body Test'
end
immediate_emails = relevant_jobs.select {|j| j['at'].nil? || j['at'] == Time.now.to_f}
immediate_emails.length.must_equal Site::EMAIL_BLAST_MAXIMUM_PER_DAY
relevant_jobs.select {|j| j['at'].nil? || j['at'] == Time.now.to_f}.length.must_equal 1
relevant_jobs.select {|j| j['at'] == (Time.now + 0.5).to_f}.length.must_equal 1
tomorrows_emails = relevant_jobs.select {|j| j['at'] == (time+1.day.to_i).to_f}
tomorrows_emails.length.must_equal Site::EMAIL_BLAST_MAXIMUM_PER_DAY
relevant_jobs.select {|j| j['at'] == (time+1.day.to_i).to_f}.length.must_equal 1
relevant_jobs.select {|j| j['at'] == (time+1.day.to_i+0.5).to_f}.length.must_equal 1
end
end
end