Skip to content

Commit

Permalink
Do check restarting LongRunnable::Worker for liveliness
Browse files Browse the repository at this point in the history
Fixes a memory leak caused by starting a new Worker when the old one is still restarting.
  • Loading branch information
dsander committed Oct 28, 2015
1 parent ea98bf9 commit 07e2d9c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions app/concerns/long_runnable.rb
Expand Up @@ -51,12 +51,13 @@ def setup_worker
end

class Worker
attr_reader :thread, :id, :agent, :config, :mutex, :scheduler
attr_reader :thread, :id, :agent, :config, :mutex, :scheduler, :restarting

def initialize(options = {})
@id = options[:id]
@agent = options[:agent]
@config = options[:config]
@restarting = false
end

def run
Expand All @@ -65,6 +66,7 @@ def run

def run!
@thread = Thread.new do
Thread.current[:name] = "#{id}-#{Time.now}"
begin
run
rescue SignalException, SystemExit
Expand Down Expand Up @@ -95,9 +97,11 @@ def stop!
end

def restart!
stop!
setup!(scheduler, mutex)
run!
without_alive_check do
stop!
setup!(scheduler, mutex)
run!
end
end

def every(*args, &blk)
Expand All @@ -120,5 +124,12 @@ def boolify(value)
def schedule(method, args, &blk)
@scheduler.send(method, *args, tag: id, &blk)
end

def without_alive_check(&blk)
@restarting = true
yield
ensure
@restarting = false
end
end
end
2 changes: 1 addition & 1 deletion lib/agent_runner.rb
Expand Up @@ -100,7 +100,7 @@ def load_workers

def restart_dead_workers
@workers.each_pair do |id, worker|
if worker.thread && !worker.thread.alive?
if !worker.restarting && worker.thread && !worker.thread.alive?
puts "Restarting #{id.to_s}"
@workers[id].run!
end
Expand Down

0 comments on commit 07e2d9c

Please sign in to comment.