lib/mail_spy/manager.rb in mail_spy-0.0.18 vs lib/mail_spy/manager.rb in mail_spy-0.1.0

- old
+ new

@@ -62,10 +62,11 @@ email.save! # Enable sendgrid specific enhancements # Must happen after the email has been saved for the id + # TODO test this works if esp.options[:enable_sendgrid_event_tracking].present? header = MailSpy::Sendgrid::SmtpApiHeader.new header.setUniqueArgs({:eid => email.id.to_s}) email.headers = {'X-SMTPAPI' => header.asJSON}.merge(email.headers) email.save! @@ -90,11 +91,11 @@ }) wq = WorkQueue.new(num_threads, step*2) current_time = DateTime.now offset = 0 - sent = 0 + processed = 0 # Helper function for setting present values def set_if_present(email, pony_hash, pony_key, email_key=nil) email_key = pony_key if email_key.nil? value = email.send("#{email_key}") @@ -102,35 +103,27 @@ end while true emails = MailSpy::Email. limit(step).offset(offset).asc(:_id). - where(:schedule_at.lte => current_time, :sent => false, :failed => false). - collect + where(:schedule_at.lte => current_time, :sent => false, :failed => false).all break if emails.count <= 0 #returns enumerator which is never blank emails.each do |email| + processed += 1 wq.enqueue_b do - begin - MailSpy::CoreMailer.template(email).deliver - email.update_attribute(:sent, true) - sent += 1 - rescue Exception => e - email.failed = true - email.error_message = e.try(:message) - email.error_backtrace = e.try(:backtrace) - email.save! - end + MailSpy.using_delayed_job ? email.delay.deliver : email.deliver end end # We must join here otherwise the next loop email lookup will be in a # race condition with the results of our worker_queue. wq.join + offset += step end success = true - return sent + return processed ensure if current_process end_time = Time.now current_process.end = end_time