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.