lib/mail_spy/manager.rb in mail_spy-0.0.11 vs lib/mail_spy/manager.rb in mail_spy-0.0.12

- old
+ new

@@ -68,44 +68,52 @@ end # ------------------------------------------- SEND OUTSTANDING EMAILS # Batches through all the emails that were scheduled and have come due # sends them out (step many at a time) - def send_outstanding_emails(step=100) + def send_outstanding_emails(step=100, num_threads=50) raise "No Email service providers installed" unless MailSpy.esps.present? + pool = MailSpy::ThreadPool.new(num_threads) offset = 0 sent = 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}") pony_hash[pony_key] = value if value.present? end + while true emails = MailSpy::Email. limit(step).offset(offset). where(:schedule_at.lte => DateTime.now, :sent => false, :failed => false).all break if emails.blank? emails.each do |email| - begin - mail = MailSpy::CoreMailer.template(email) - #TODO might be nice to flush mail out in debug mode - mail.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! + pool.schedule 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 end + + while(pool.job_queue_size > (2 * num_threads)) + sleep(1) + end + end offset += step end + pool.shutdown sent end # ------------------------------------------- TRACKING