File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -1640,6 +1640,42 @@ Sending emails while generating page response should be avoided.
16401640It causes delays in loading of the page and request can timeout if multiple email are sent.
16411641To overcome this emails can be sent in background process with the help of https://github.com/mperham/sidekiq[sidekiq] gem.
16421642
1643+ === Enqueuing Many Emails at Once [[enqueuing-many-emails]]
1644+ 
1645+ Prefer enqueuing many emails at once instead of one by one in a loop.
1646+ This can greatly reduce the number of round-trips to the queue datastore.
1647+ 
1648+ [source,ruby]
1649+ ---- 
1650+ # bad 
1651+ users.each { |user| Notifier.welcome(user).deliver_later } 
1652+ 
1653+ # good 
1654+ emails = users.map { |user| Notifier.welcome(user) } 
1655+ ActionMailer.deliver_all_later(emails) 
1656+ ---- 
1657+ 
1658+ NOTE: Rails 8.1 or later is required for using `deliver_all_later`.
1659+ 
1660+ == Jobs
1661+ 
1662+ === Enqueuing Many Jobs at Once [[enqueuing-many-jobs]]
1663+ 
1664+ Prefer enqueuing many jobs at once instead of one by one in a loop.
1665+ This can greatly reduce the number of round-trips to the queue datastore.
1666+ 
1667+ [source,ruby]
1668+ ---- 
1669+ # bad 
1670+ ids.each { |id| MyJob.perform_later(id) } 
1671+ 
1672+ # good 
1673+ jobs = ids.map { |id| MyJob.new(id) } 
1674+ ActiveJob.perform_all_later(jobs) 
1675+ ---- 
1676+ 
1677+ NOTE: Rails 7.1 or later is required for using `perform_all_later`.
1678+ 
16431679== Active Support Core Extensions
16441680
16451681=== `try!` [[try-bang]]
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments