Skip to content

Commit

Permalink
Check for already running status code when launching core agent. (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrothrock authored Dec 18, 2023
1 parent ebc5afb commit fdfe6e4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/scout_apm/core/agent/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
import logging
import os
import signal
import subprocess
import tarfile
import time
Expand All @@ -17,6 +16,8 @@

logger = logging.getLogger(__name__)

CA_ALREADY_RUNNING_EXIT_CODE = 3


class CoreAgentManager(object):
def __init__(self):
Expand Down Expand Up @@ -73,8 +74,10 @@ def run(self):
stdout=devnull,
)
except subprocess.CalledProcessError as err:
if err.returncode in [signal.SIGTERM, signal.SIGQUIT]:
logger.debug("Core agent returned signal: {}".format(err.returncode))
if err.returncode == CA_ALREADY_RUNNING_EXIT_CODE:
# Other processes may have already started the core agent.
logger.debug("Core agent already running.")
return True
else:
logger.exception("CalledProcessError running Core Agent")
return False
Expand Down
17 changes: 0 additions & 17 deletions tests/integration/core/agent/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,6 @@ def test_launch_error(caplog, core_agent_manager):
assert caplog.records[0].exc_info[1] is exception


@pytest.mark.parametrize(["signal_code"], [[signal.SIGQUIT], [signal.SIGTERM]])
def test_launch_expected_signal_error(signal_code, caplog, core_agent_manager):
caplog.set_level(logging.ERROR)
exception = subprocess.CalledProcessError(signal_code, "err")
with mock.patch.object(
manager.CoreAgentManager,
"agent_binary",
side_effect=exception,
):
result = core_agent_manager.launch()

assert not result
assert not core_agent_is_running()
# Caplog doesn't contain debug messages
assert caplog.record_tuples == []


def test_launch_unexpected_signal_error(caplog, core_agent_manager):
caplog.set_level(logging.ERROR)
exception = subprocess.CalledProcessError(signal.SIGINT, "err")
Expand Down

0 comments on commit fdfe6e4

Please sign in to comment.