Skip to content

Commit 65dc7db

Browse files
committed
Merge pull request #1643 from DataDog/yann/file-to-stop-jmx
[jmxfetch][windows] write to file to exit JMXFetch
2 parents a065747 + 77fc6c0 commit 65dc7db

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

jmxfetch.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# datadog
1111
from util import get_os, yLoader, yDumper
12+
from utils.platform import Platform
1213
from config import get_config, get_confd_path, get_jmx_status_path, get_logging_config, \
1314
PathNotFound, DEFAULT_CHECK_FREQUENCY
1415

@@ -49,6 +50,7 @@
4950
JMX_COLLECT_COMMAND: "Start the collection of metrics based on your current configuration and display them in the console"}
5051

5152
PYTHON_JMX_STATUS_FILE = 'jmx_status_python.yaml'
53+
PYTHON_JMX_EXIT_FILE = 'jmxfetch_exit'
5254

5355
LINK_TO_DOC = "See http://docs.datadoghq.com/integrations/java/ for more information"
5456

@@ -232,6 +234,13 @@ def _start(self, path_to_java, java_run_opts, jmx_checks, command, reporter, too
232234
command, # Name of the command
233235
]
234236

237+
if Platform.is_windows():
238+
# Signal handlers are not supported on Windows:
239+
# use a file to trigger JMXFetch exit instead
240+
path_to_exit_file = os.path.join(get_jmx_status_path(), PYTHON_JMX_EXIT_FILE)
241+
subprocess_args.insert(len(subprocess_args) - 1, '--exit_file_location')
242+
subprocess_args.insert(len(subprocess_args) - 1, path_to_exit_file)
243+
235244
subprocess_args.insert(4, '--check')
236245
for check in jmx_checks:
237246
subprocess_args.insert(5, check)
@@ -379,12 +388,31 @@ def _is_jmx_check(self, check_config, check_name, checks_list):
379388
return is_jmx, java_bin_path, java_options, tools_jar_path
380389

381390
def _get_path_to_jmxfetch(self):
382-
if get_os() != 'windows':
391+
if not Platform.is_windows():
383392
return os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "checks",
384393
"libs", JMX_FETCH_JAR_NAME))
385394
return os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "..",
386395
"jmxfetch", JMX_FETCH_JAR_NAME))
387396

397+
@staticmethod
398+
def write_exit_file():
399+
"""
400+
Create a 'special' file, which acts as a trigger to exit JMXFetch.
401+
Note: Windows only
402+
"""
403+
open(os.path.join(get_jmx_status_path(), PYTHON_JMX_EXIT_FILE), 'a').close()
404+
405+
@staticmethod
406+
def clean_exit_file():
407+
"""
408+
Remove exit file trigger -may not exist-.
409+
Note: Windows only
410+
"""
411+
try:
412+
os.remove(os.path.join(get_jmx_status_path(), PYTHON_JMX_EXIT_FILE))
413+
except OSError:
414+
pass
415+
388416

389417
def _clean_status_file():
390418
"""

win32/agent.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ def run(self):
193193
def stop(self):
194194
log.debug("Windows Service - Stopping collector")
195195
self.collector.stop()
196-
if JMXFetch.is_running():
197-
JMXFetch.stop()
198196
self.running = False
199197

200198
def get_emitters(self):
@@ -282,10 +280,14 @@ def __init__(self, agentConfig, hostname):
282280

283281
def run(self):
284282
if self.is_enabled:
283+
JMXFetch.clean_exit_file()
285284
self.jmx_daemon.run()
286285

287-
def stop(self):
288-
pass
286+
def terminate(self):
287+
"""
288+
Override `terminate` method to properly exit JMXFetch.
289+
"""
290+
JMXFetch.write_exit_file()
289291

290292

291293
if __name__ == '__main__':

0 commit comments

Comments
 (0)