VRFS-1483 error handling

This commit is contained in:
Jonathan Kolyer 2014-04-04 10:08:00 +00:00
parent 5683b7386a
commit e750ac9e68
8 changed files with 106 additions and 4 deletions

View File

@ -140,3 +140,4 @@ notifications_with_text.sql
notification_seen_at.sql
order_event_session.sql
emails.sql
email_batch.sql

48
db/up/email_batch.sql Normal file
View File

@ -0,0 +1,48 @@
-- CREATE TABLE email_batches (
-- id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
-- subject VARCHAR(256) NOT NULL,
-- body TEXT NOT NULL,
-- from_email VARCHAR(64) NOT NULL default 'support@jamkazam.com',
-- aasm_state VARCHAR(32) NOT NULL default 'pending',
-- test_emails TEXT NOT NULL default '',
-- qualified_count INTEGER NOT NULL default 0,
-- sent_count INTEGER NOT NULL default 0,
-- lock_version INTEGER,
-- started_at TIMESTAMP,
-- completed_at TIMESTAMP,
-- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
-- );
CREATE TABLE email_batch_sets (
id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
email_batch_id VARCHAR(64) REFERENCES email_batches(id) ON DELETE CASCADE,
started_at TIMESTAMP,
user_ids TEXT NOT NULL default '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE email_batch_sets ADD CONSTRAINT email_batch_set_uniqkey UNIQUE (email_batch_id, started_at);
CREATE INDEX email_batch_set_fkidx ON email_batch_sets (email_batch_id);
CREATE TABLE email_errors (
id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(),
email_batch_id VARCHAR(64) REFERENCES email_batches(id) ON DELETE CASCADE,
user_id VARCHAR(64) REFERENCES users(id) ON DELETE CASCADE,
error_type VARCHAR(32),
email_address VARCHAR(256),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX email_error_fkidx ON email_errors(email_batch_id);

View File

@ -134,6 +134,8 @@ require "jam_ruby/models/country"
require "jam_ruby/models/region"
require "jam_ruby/models/city"
require "jam_ruby/models/email_batch"
require "jam_ruby/models/email_batch_set"
require "jam_ruby/models/email_error"
require "jam_ruby/app/mailers/async_mailer"
require "jam_ruby/app/mailers/batch_mailer"

View File

@ -0,0 +1,8 @@
Resque::Mailer.error_handler = lambda { |mailer, message, error, action, args|
# Necessary to re-enqueue jobs that receieve the SIGTERM signal
if error.is_a?(Resque::TermException)
Resque.enqueue(mailer, action, *args)
else
raise error
end
}

View File

@ -2,6 +2,9 @@ module JamRuby
class EmailBatch < ActiveRecord::Base
self.table_name = "email_batches"
has_many :email_batch_sets, :class_name => 'JamRuby::EmailBatchSet', :dependent => :destroy
has_many :email_errors, :class_name => 'JamRuby::EmailError'
attr_accessible :from_email, :subject, :test_emails, :body
attr_accessible :lock_version, :qualified_count, :sent_count, :started_at, :completed_at
@ -54,8 +57,6 @@ FOO
end
end
# has_many :email_batch_results, :class_name => 'JamRuby::EmailBatchResult'
def self.create_with_params(params)
obj = self.new
params.each { |kk,vv| vv.strip! }
@ -67,7 +68,8 @@ FOO
def deliver_batch
self.perform_event('do_batch_run!')
User.email_opt_in.find_in_batches(batch_size: 1000) do |users|
BatchMailer.send_batch_email(self.id, users.map(&:id)).deliver
self.email_batch_sets << EmailBatchSet.deliver_set(self, users.map(&:id))
# BatchMailer.send_batch_email(self.id, users.map(&:id)).deliver
end
end
@ -88,7 +90,7 @@ FOO
def send_test_batch
self.perform_event('do_test_run!')
BatchMailer.send_batch_email_test(self.id).deliver
self.email_batch_sets << EmailBatchSet.deliver_test(self)
end
def merged_body(user)
@ -149,6 +151,7 @@ FOO
def ran_batch
self.update_with_conflict_validation({ :completed_at => Time.now })
end
end

View File

@ -0,0 +1,27 @@
module JamRuby
class EmailBatchSet < ActiveRecord::Base
self.table_name = "email_batch_sets"
belongs_to :email_batch, :class_name => 'JamRuby::EmailBatch'
def self.deliver_set(batch, user_ids)
bset = self.new
bset.email_batch = batch
bset.user_ids = user_ids.join(',')
bset.started_at = Time.now
bset.save!
BatchMailer.send_batch_email(self.email_batch_id, user_ids).deliver
bset
end
def self.deliver_test(batch)
bset = self.new
bset.email_batch = batch
bset.started_at = Time.now
bset.save!
BatchMailer.send_batch_email_test(batch.id).deliver
bset
end
end
end

View File

@ -0,0 +1,11 @@
module JamRuby
class EmailError < ActiveRecord::Base
self.table_name = "email_batch_errors"
belongs_to :email_batch, :class_name => 'JamRuby::EmailBatch'
ERR_BOUNCE = :bounce
ERR_INVALID = :invalid
end
end

View File

@ -9,6 +9,8 @@ describe BatchMailer do
batch.send_test_batch
sleep(3)
batch.email_batch_sets.count.should == 1
mail = BatchMailer.deliveries.detect { |dd| dd['to'].to_s.split(',')[0] == batch.test_emails.split(',')[0]}
# let (:mail) { BatchMailer.deliveries[0] }
# it { mail['to'].to_s.split(',')[0].should == batch.test_emails.split(',')[0] }