VRFS-1483 various fixes from testing

This commit is contained in:
Jonathan Kolyer 2014-03-19 16:32:53 +00:00
parent 21bd8fcf2f
commit 74a5f3863a
5 changed files with 71 additions and 37 deletions

View File

@ -17,7 +17,6 @@ require "postgres_ext"
require 'builder'
require 'cgi'
require 'resque_mailer'
require 'sendgrid_smtpapi'
require "jam_ruby/constants/limits"
require "jam_ruby/constants/notification_types"

View File

@ -2,41 +2,36 @@ module JamRuby
class BatchMailer < JamRuby::AsyncMailer
layout "batch_mailer"
sendgrid_category :batch_email
sendgrid_category :use_subject_lines
sendgrid_unique_args :env => Environment.mode
def send_batch_email(batch_id, user_ids)
@users = User.find_all_by_id(user_ids)
batch = EmailBatch.where(:id => batch_id).limit(1).first
def _send_batch(batch, users)
@body = batch.body
emails = users.map(&:email)
sendgrid_recipients(emails)
sendgrid_substitute(EmailBatch::VAR_FIRST_NAME, users.map(&:first_name))
batch.did_send(emails)
substitute(EmailBatch::VAR_FIRST_NAME, @users.map(&:first_name))
emails = @users.map(&:email)
mail(:to => emails,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
batch.did_send(emails)
end
def send_batch_email(batch_id, user_ids)
users = User.find_all_by_id(user_ids)
batch = EmailBatch.where(:id => batch_id).limit(1).first
self._send_batch(batch, users)
end
def send_batch_email_test(batch_id)
batch = EmailBatch.where(:id => batch_id).limit(1).first
@body = batch.body
@users = batch.test_users
emails = @users.map(&:email)
substitute(EmailBatch::VAR_FIRST_NAME, @users.map(&:first_name))
mail(:to => emails,
:from => batch.from_email,
:subject => batch.subject) do |format|
format.text
format.html
end
batch.did_send(emails)
users = batch.test_users
self._send_batch(batch, users)
end
end

View File

@ -24,7 +24,7 @@
<tr>
<td align="left"><h1 style="font-size:22px;font-weight:normal;margin-top:0px"><font color="#F34E1C" face="Arial, Helvetica, sans-serif"><%= yield(:title) %></font></h1>
<p><font size="3" color="#AAAAAA" face="Arial, Helvetica, sans-serif"><%= yield %></font></p>
<p><font size="3" color="#AAAAAA" face="Arial, Helvetica, sans-serif"><%= @body %></font></p>
<br>
</td>
</tr>

View File

@ -1,4 +1,4 @@
<%= yield %>
<%= Nokogiri::HTML(@body).text %>
<% unless @suppress_user_has_account_footer == true %>

View File

@ -2,7 +2,8 @@ module JamRuby
class EmailBatch < ActiveRecord::Base
self.table_name = "email_batches"
attr_accessible :lock_version
attr_accessible :from_email, :subject, :test_emails, :body
attr_accessible :lock_version, :qualified_count, :sent_count, :started_at, :completed_at
VAR_FIRST_NAME = '@FIRSTNAME'
VAR_LAST_NAME = '@LASTNAME'
@ -21,16 +22,19 @@ module JamRuby
event :enable do
transitions :from => :disabled, :to => :pending
end
event :reset do
transitions :from => [:disabled, :testing, :tested, :batching, :batched, :pending], :to => :pending
end
event :do_test_run, :before => :running_tests do
transitions :from => [:pending, :tested, :batched], :to => :testing
end
event :did_test_run do
event :did_test_run, :after => :ran_tests do
transitions :from => :testing, :to => :tested
end
event :do_batch_run, :before => :running_batch do
transitions :from => [:tested, :pending, :batched], :to => :batching
end
event :did_batch_run do
event :did_batch_run, :after => :ran_batch do
transitions :from => :batching, :to => :batched
end
event :disable do
@ -45,8 +49,23 @@ module JamRuby
.where(:opt_out_email_batch => false)
end
def self.create_with_params(params)
obj = self.new
obj.update_with_params(params)
obj.save!
obj
end
def update_with_params(params)
self.from_email = params[:from_email].strip
self.subject = params[:subject].strip
self.test_emails = params[:test_emails].strip
self.body = params[:body].strip
self
end
def deliver
self.do_batch_run!
self.perform_event('do_batch_run!')
self.class.qualified_users.pluck(:id).find_in_batches(batch_size: 100) do |user_ids|
BatchMailer.send_batch_email(self.id, user_ids).deliver
end
@ -68,7 +87,7 @@ module JamRuby
end
def send_test_batch
self.do_test_run!
self.perform_event('do_test_run!')
BatchMailer.send_batch_email_test(self.id).deliver
end
@ -81,13 +100,24 @@ module JamRuby
if self.sent_count >= self.qualified_count
if batching?
self.did_batch_run!
self.perform_event('did_batch_run!')
elsif testing?
self.did_test_run!
self.perform_event('did_test_run!')
end
end
end
def perform_event(event_name)
num_try = 0
self.send(event_name)
rescue ActiveRecord::StaleObjectError
num_try += 1
if 5 > num_try
self.reload
retry
end
end
def update_with_conflict_validation(*args)
num_try = 0
update_attributes(*args)
@ -98,17 +128,27 @@ module JamRuby
retry
end
end
def running_batch
self.update_attributes({:qualified_count => self.class.qualified_users.count,
:sent_count => 0
})
self.update_with_conflict_validation({:qualified_count => self.class.qualified_users.count,
:sent_count => 0,
:started_at => Time.now
})
end
def running_tests
self.update_attributes({:qualified_count => self.class.qualified_users.count,
:sent_count => 0
})
self.update_with_conflict_validation({:qualified_count => self.test_count,
:sent_count => 0,
:started_at => Time.now
})
end
def ran_tests
self.update_with_conflict_validation({ :completed_at => Time.now })
end
def ran_batch
self.update_with_conflict_validation({ :completed_at => Time.now })
end
end