Skip to content

Commit 4f0fcaa

Browse files
committed
Add suggestions for enqueuing many jobs and emails
1 parent b14b217 commit 4f0fcaa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.adoc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,38 @@ Sending emails while generating page response should be avoided.
16401640
It causes delays in loading of the page and request can timeout if multiple email are sent.
16411641
To 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+
== Jobs
1659+
1660+
=== Enqueuing Many Jobs at Once [[enqueuing-many-jobs]]
1661+
1662+
Prefer enqueuing many jobs at once instead of one by one in a loop.
1663+
This can greatly reduce the number of round-trips to the queue datastore.
1664+
1665+
[source,ruby]
1666+
----
1667+
# bad
1668+
ids.each { |id| MyJob.perform_later(id) }
1669+
1670+
# good
1671+
jobs = ids.map { |id| MyJob.new(id) }
1672+
ActiveJob.perform_all_later(jobs)
1673+
----
1674+
16431675
== Active Support Core Extensions
16441676

16451677
=== `try!` [[try-bang]]

0 commit comments

Comments
 (0)