Skip to content

Commit

Permalink
Merge pull request #359 from alphagov/fix-memory-leak-when-bulk-emailing
Browse files Browse the repository at this point in the history
Fix memory leak when queuing jobs inside a job
  • Loading branch information
alanth committed Jul 29, 2015
2 parents f9f16a8 + 515d1d9 commit d4905e3
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions config/initializers/active_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'active_job/logging'

# TODO: remove this when the following issue is resolved:
# https://github.com/rails/rails/issues/21036

# Unsubscribe all Active Job notifications
ActiveSupport::Notifications.unsubscribe 'perform_start.active_job'
ActiveSupport::Notifications.unsubscribe 'perform.active_job'
ActiveSupport::Notifications.unsubscribe 'enqueue_at.active_job'
ActiveSupport::Notifications.unsubscribe 'enqueue.active_job'

module ActiveJob
module Logging
class LogSubscriber
# Remove all public methods so that we can use the class
# as the base class for our two new subscriber classes.
remove_method :enqueue
remove_method :enqueue_at
remove_method :perform_start
remove_method :perform
end

class EnqueueSubscriber < LogSubscriber
def enqueue(event)
info do
job = event.payload[:job]
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)}" + args_info(job)
end
end

def enqueue_at(event)
info do
job = event.payload[:job]
"Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event)}" + args_info(job)
end
end
end

class ExecutionSubscriber < LogSubscriber
def perform_start(event)
info do
job = event.payload[:job]
"Performing #{job.class.name} from #{queue_name(event)}" + args_info(job)
end
end

def perform(event)
info do
job = event.payload[:job]
"Performed #{job.class.name} from #{queue_name(event)} in #{event.duration.round(2)}ms"
end
end
end
end
end

# Suubscribe to Active Job notifications
ActiveJob::Logging::EnqueueSubscriber.attach_to :active_job
ActiveJob::Logging::ExecutionSubscriber.attach_to :active_job

0 comments on commit d4905e3

Please sign in to comment.