Skip to content

Commit 6cec25c

Browse files
committed
Add suggestions for enqueuing many jobs and emails
1 parent b14b217 commit 6cec25c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,30 @@ travel_to(Time.current.to_time)
19561956
freeze_time
19571957
----
19581958

1959+
== Performance
1960+
1961+
=== Enqueuing Many Jobs at Once [[enqueuing-many-jobs]]
1962+
1963+
Prefer enqueuing many jobs and emails at once instead of one by one in a loop.
1964+
This can greatly reduce the number of round-trips to the queue datastore.
1965+
1966+
[source,ruby]
1967+
----
1968+
# bad
1969+
ids.each { |id| MyJob.perform_later(id) }
1970+
1971+
# good
1972+
jobs = ids.map { |id| MyJob.new(id) }
1973+
ActiveJob.perform_all_later(jobs)
1974+
1975+
# bad
1976+
users.each { |user| Notifier.welcome(user).deliver_later }
1977+
1978+
# good
1979+
emails = users.map { |user| Notifier.welcome(user) }
1980+
ActionMailer.deliver_all_later(emails)
1981+
----
1982+
19591983
== Managing Processes
19601984

19611985
=== Foreman [[foreman]]

0 commit comments

Comments
 (0)