lib/mail_spy/manager.rb in mail_spy-0.0.14 vs lib/mail_spy/manager.rb in mail_spy-0.0.15

- old
+ new

@@ -72,10 +72,11 @@ # sends them out (step many at a time) def send_outstanding_emails(step=100, num_threads=50) raise "No Email service providers installed" unless MailSpy.esps.present? wq = WorkQueue.new(num_threads, step*2) + current_time = DateTime.now offset = 0 sent = 0 # Helper function for setting present values def set_if_present(email, pony_hash, pony_key, email_key=nil) @@ -85,11 +86,11 @@ end while true emails = MailSpy::Email. limit(step).offset(offset). - where(:schedule_at.lte => DateTime.now, :sent => false, :failed => false).all + where(:schedule_at.lte => current_time, :sent => false, :failed => false).all break if emails.blank? emails.each do |email| wq.enqueue_b do begin MailSpy::CoreMailer.template(email).deliver @@ -101,9 +102,11 @@ email.error_backtrace = e.try(:backtrace) email.save! end 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 sent