Skip to content

Commit

Permalink
Merge pull request #16685 from jerryk55/smart_proxy_heartbeat_thread
Browse files Browse the repository at this point in the history
Add Heartbeat Thread to SmartProxy Worker
  • Loading branch information
roliveri authored Jan 4, 2018
2 parents d2e5a32 + 5d0ce26 commit a34f5e0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/models/miq_smart_proxy_worker/runner.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
class MiqSmartProxyWorker::Runner < MiqQueueWorkerBase::Runner
self.delay_startup_for_vim_broker = true # NOTE: For smartproxy role

def do_before_work_loop
@tid = start_heartbeat_thread
end

def before_exit(message, _exit_code)
@exit_requested = true
#
# Stop the Heartbeat Thread
#
safe_log("#{message} Stopping Heartbeat Thread.")

#
# Wait for the Heartbeat Thread to stop
#
if @tid
safe_log("#{message} Waiting for Heartbeat Thread to Stop.")
begin
@tid.join(worker_settings[:heartbeat_thread_shutdown_timeout])
rescue NoMethodError => join_err
safe_log(join_err)
end
end
end

def start_heartbeat_thread
@exit_requested = false
@heartbeat_started = Concurrent::Event.new
_log.info("#{log_prefix} Starting Heartbeat Thread")

tid = Thread.new do
begin
heartbeat_thread
rescue => err
_log.error("#{log_prefix} Heartbeat Thread aborted because [#{err.message}]")
_log.log_backtrace(err)
Thread.exit
ensure
@heartbeat_started.set
end
end

@heartbeat_started.wait
_log.info("#{log_prefix} Started Heartbeat Thread")

tid
end

def heartbeat_thread
@heartbeat_started.set
until @exit_requested
heartbeat
sleep 30
end
end

def do_work
if @tid.nil? || !@tid.alive?
unless @tid.try(:status)
dead_tid, @tid = @tid, nil
_log.info("#{log_prefix} Waiting for the Heartbeat Thread to exit...")
dead_tid.join # raise the exception the dead thread failed with
end

_log.info("#{log_prefix} Heartbeat Thread gone. Restarting...")
@tid = start_heartbeat_thread
end

super
end
end
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,7 @@
:memory_threshold: 2.gigabytes
:queue_timeout: 20.minutes
:restart_interval: 6.hours
:heartbeat_thread_shutdown_timeout: 10.seconds
:schedule_worker:
:container_entities_purge_interval: 1.day
:binary_blob_purge_interval: 1.hour
Expand Down

0 comments on commit a34f5e0

Please sign in to comment.