From e750ac9e68029f42333d55e771e3ef77e0b31ae5 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Fri, 4 Apr 2014 10:08:00 +0000 Subject: [PATCH] VRFS-1483 error handling --- db/manifest | 1 + db/up/email_batch.sql | 48 +++++++++++++++++++++ ruby/lib/jam_ruby.rb | 2 + ruby/lib/jam_ruby/errors/resque_mailer.rb | 8 ++++ ruby/lib/jam_ruby/models/email_batch.rb | 11 +++-- ruby/lib/jam_ruby/models/email_batch_set.rb | 27 ++++++++++++ ruby/lib/jam_ruby/models/email_error.rb | 11 +++++ ruby/spec/mailers/batch_mailer_spec.rb | 2 + 8 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 db/up/email_batch.sql create mode 100644 ruby/lib/jam_ruby/errors/resque_mailer.rb create mode 100644 ruby/lib/jam_ruby/models/email_batch_set.rb create mode 100644 ruby/lib/jam_ruby/models/email_error.rb diff --git a/db/manifest b/db/manifest index aba246d9b..de9c83f4c 100755 --- a/db/manifest +++ b/db/manifest @@ -140,3 +140,4 @@ notifications_with_text.sql notification_seen_at.sql order_event_session.sql emails.sql +email_batch.sql diff --git a/db/up/email_batch.sql b/db/up/email_batch.sql new file mode 100644 index 000000000..d10ea74bc --- /dev/null +++ b/db/up/email_batch.sql @@ -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); diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index b6f17a3a4..2bc1972a9 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -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" diff --git a/ruby/lib/jam_ruby/errors/resque_mailer.rb b/ruby/lib/jam_ruby/errors/resque_mailer.rb new file mode 100644 index 000000000..f674b9166 --- /dev/null +++ b/ruby/lib/jam_ruby/errors/resque_mailer.rb @@ -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 +} diff --git a/ruby/lib/jam_ruby/models/email_batch.rb b/ruby/lib/jam_ruby/models/email_batch.rb index 9b13f1f4d..a7e2ae9df 100644 --- a/ruby/lib/jam_ruby/models/email_batch.rb +++ b/ruby/lib/jam_ruby/models/email_batch.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/email_batch_set.rb b/ruby/lib/jam_ruby/models/email_batch_set.rb new file mode 100644 index 000000000..1349b6cf4 --- /dev/null +++ b/ruby/lib/jam_ruby/models/email_batch_set.rb @@ -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 diff --git a/ruby/lib/jam_ruby/models/email_error.rb b/ruby/lib/jam_ruby/models/email_error.rb new file mode 100644 index 000000000..6c4b7b4e3 --- /dev/null +++ b/ruby/lib/jam_ruby/models/email_error.rb @@ -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 diff --git a/ruby/spec/mailers/batch_mailer_spec.rb b/ruby/spec/mailers/batch_mailer_spec.rb index 784279db8..688e3a481 100644 --- a/ruby/spec/mailers/batch_mailer_spec.rb +++ b/ruby/spec/mailers/batch_mailer_spec.rb @@ -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] }