-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent memory bloat in ActiveJob children
`ActiveJob` attaches a single `LogSubscriber` to be notified of events. When one job enqueues another, that's treated as a child event and stored on the same stack as the parent. If a large number of children are enqueued that leads to R14 errors on Heroku. The fix here is to unsubscribe all existing "enqueue" listeners and then add a new subscriber for just that event. The original handler is copied from the parent class using `define_method` so that it's picked up by `attach_to`. Related: rails/rails#21036 rails/rails#5932 Before and after: ![Heroku metrics chart showing 1GB drop in memory usage after this change deployed](https://user-images.githubusercontent.com/47136/28436362-b51b3d1e-6d64-11e7-8b24-fc67927e7081.png)
- Loading branch information
Showing
5 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
lib/suspenders/generators/initialize_active_job_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require "rails/generators" | ||
|
||
module Suspenders | ||
class InitializeActiveJobGenerator < Rails::Generators::Base | ||
source_root( | ||
File.expand_path( | ||
File.join("..", "..", "..", "templates"), | ||
File.dirname(__FILE__), | ||
), | ||
) | ||
|
||
def initialize_active_job | ||
copy_file( | ||
"active_job.rb", | ||
"config/initializers/active_job.rb", | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "active_job/logging" | ||
|
||
ActiveSupport::Notifications.unsubscribe("enqueue.active_job") | ||
|
||
module ActiveJob | ||
module Logging | ||
class EnqueueLogSubscriber < LogSubscriber | ||
define_method :enqueue, instance_method(:enqueue) | ||
end | ||
end | ||
end | ||
|
||
ActiveJob::Logging::EnqueueLogSubscriber.attach_to(:active_job) |