0

I came across this post and it seems that I could avoid using the delayed jobs gem which would be preferred.

Basis of the application:

  • an outage notification system for a cable company
  • When an outage occurs at a property (or properties), a ticket gets created
  • There are over 2000 properties, so properties are split into 8 categories based on business type and geographic location
  • a user can subscribe to 8 potential categories
  • only certain users can create tickets.
  • when a ticket is created, updated, or closed, a user will get an email/sms notification if they are subscribed to one of the categories that the property is in.

The issue: emails are being sent when a ticket is created/updated closed, but there will eventually be 300+ subscribed users receiving emails. I currently have about 15 test users and when a ticket is modified, it seems 15 separate requests are being sent every time: this is quite slow in the browser (ie, the ticket modifier may wait 10+ seconds after the create/update/close button is clicked), but bearable. As more and more users subscribe, this slowness will not be tolerable.

Here is the ticket controller in full if you'd like to check it out: I realize it is fairly repetitive and doesn't follow DRY principles, and could certainly be refactored.

I'm confused on how to implement (from the linked SO post above)

headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json

since the categorical logic is in the controller, but the app/mailers/user_notifier.rb is where the email methods are defined, and isn't that where headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json would have to go?

Line 219 of tickets_controller.rb is below.

def grab_all_sub_emails
    @all_users = Subscription.where.not(name: '')
    @sub_emails = @all_users.includes(:categories).map { |user| user.slice(:phone_number, :name).merge(categories: user.categories.map(&:name))}
end

Any input on how I need to modify my logic/implement headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json to speed up the emailing process would be sincerely appreciated.

Community
  • 1
  • 1
Nubtacular
  • 1,367
  • 2
  • 18
  • 38

1 Answers1

1

You don't need to iterate over @emails_for_email and call UserNotifier on each since you're already setting the :to key in the X-SMTPAPI header.

Just pass array_of_recipients to the UserNotifier, set it as a header:

def ticket_created(array_of_recipients)
  ...
  headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json
  ...
  mail
end

And SenGrid will read the X-SMTPAPI header, read the :to key and deliver the message to all the recipients.


May I recommend using the sendgrid-ruby gem and perhaps store the templates in SendGrid?

miligraf
  • 1,042
  • 9
  • 22
  • @milligraf thank you for the reply. So I removed that iteration as it was unnecessary and just call `UserNotifier` once. As for the `X-SMTPAPI` header, I'm still a bit confused. So that doesn't need to be called anywhere explicitly in my code? Or do I need to reference it in the mailer `user_notifier.rb` ? – Nubtacular Apr 04 '16 at 14:25
  • Ahhh I see now! Interesting, didn't realize it was so straight forward. Really appreciate your help. After updating/creating/closing a ticket, there is way less delay on the submit button. Tyvm! (And the array of email recipients is showing up correctly as an array in the server log). – Nubtacular Apr 04 '16 at 14:50