Skip to content

Commit

Permalink
[service] properly exit or kill JMXFetch
Browse files Browse the repository at this point in the history
We want to JMXFetch to exit properly because issues could arise should
it close abruptly its JMX connectioni (this is pure hypothesis, but
let's stay safe). We still kill it if we have to wait more than 15s.
(= one full run of JMXFetch)
  • Loading branch information
degemer committed Feb 10, 2017
1 parent 068c4b4 commit 20c5e6c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions win32/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class DDProcess(object):
"""
DEFAULT_MAX_RESTARTS = 5
_RESTART_TIMEFRAME = 3600
_STOP_TIMEOUT = 3

def __init__(self, name, command, env, enabled=True, max_restarts=None):
self._name = name
Expand All @@ -220,14 +221,21 @@ def start(self):
def stop(self):
if self._proc is not None and self._proc.is_running():
log.info("Stopping %s...", self._name)
self._proc.terminate()
children_proc = self._proc.children()

psutil.wait_procs([self._proc], timeout=3)
self._proc.terminate()

# Should never have to wait, terminate is equivalent to kill on Windows
self._proc.wait(timeout=self._STOP_TIMEOUT)
if self._proc.is_running():
log.debug("%s didn't exit. Killing it.", self._name)
self._proc.kill()

# Mostly for JMXFetch, if it didn't exit yet, kill it
for child_proc in children_proc:
if child_proc.is_running():
child_proc.kill()

log.info("%s is stopped.", self._name)
else:
log.debug('%s was not running.', self._name)
Expand Down Expand Up @@ -268,6 +276,10 @@ def restart(self):


class JMXFetchProcess(DDProcess):
# It's an entire JMXFetch run, because JMXFetch only checks that the exit
# file exists at the beginning of each run
_JMX_STOP_TIMEOUT = 15

def start(self):
if self.is_enabled():
JMXFiles.clean_exit_file()
Expand All @@ -280,6 +292,10 @@ def stop(self):
"""
if self._proc is not None and self._proc.is_running():
JMXFiles.write_exit_file()
try:
self._proc.wait(timeout=self._JMX_STOP_TIMEOUT)
except psutil.TimeoutExpired:
log.debug("JMXFetch process didn't stop after %ss, killing it", self._JMX_STOP_TIMEOUT)

super(JMXFetchProcess, self).stop()

Expand Down

0 comments on commit 20c5e6c

Please sign in to comment.