Skip to content

Commit

Permalink
[pantsd] Launch the daemon via a subprocess call. (#5021)
Browse files Browse the repository at this point in the history
Closes #4618
  • Loading branch information
kwlzn authored Oct 27, 2017
1 parent dc1bf4f commit 8c03533
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 328 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
.pants.run
.pants.workdir.file_lock
.pants.stats
.pantsd.startup
.pids
.project
.settings
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/bin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ python_library(
'src/python/pants/init',
'src/python/pants/option',
'src/python/pants/reporting',
'src/python/pants/pantsd:pants_daemon_launcher',
'src/python/pants/pantsd:pants_daemon',
'src/python/pants/scm/subsystems:changed',
'src/python/pants/subsystem',
'src/python/pants/task',
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/bin/goal_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from pants.task.task import QuietTaskMixin
from pants.util.filtering import create_filters, wrap_filters


logger = logging.getLogger(__name__)


Expand Down
6 changes: 1 addition & 5 deletions src/python/pants/bin/pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ def run(self):

if bootstrap_options.enable_pantsd:
try:
return RemotePantsRunner(self._exiter,
self._args,
self._env,
bootstrap_options.pants_subprocessdir,
bootstrap_options).run()
return RemotePantsRunner(self._exiter, self._args, self._env, bootstrap_options).run()
except RemotePantsRunner.Fallback as e:
logger.debug('caught client exception: {!r}, falling back to non-daemon mode'.format(e))

Expand Down
24 changes: 8 additions & 16 deletions src/python/pants/bin/remote_pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import sys
from contextlib import contextmanager

from pants.bin.engine_initializer import EngineInitializer
from pants.java.nailgun_client import NailgunClient
from pants.java.nailgun_protocol import NailgunProtocol
from pants.pantsd.pants_daemon_launcher import PantsDaemonLauncher
from pants.pantsd.pants_daemon import PantsDaemon
from pants.util.collections import combined_dict
from pants.util.memo import memoized_property


logger = logging.getLogger(__name__)
Expand All @@ -32,13 +32,11 @@ class PortNotFound(Exception):
PANTS_COMMAND = 'pants'
RECOVERABLE_EXCEPTIONS = (PortNotFound, NailgunClient.NailgunConnectionError)

def __init__(self, exiter, args, env, process_metadata_dir,
bootstrap_options, stdin=None, stdout=None, stderr=None):
def __init__(self, exiter, args, env, bootstrap_options, stdin=None, stdout=None, stderr=None):
"""
:param Exiter exiter: The Exiter instance to use for this run.
:param list args: The arguments (e.g. sys.argv) for this run.
:param dict env: The environment (e.g. os.environ) for this run.
:param str process_metadata_dir: The directory in which process metadata is kept.
:param Options bootstrap_options: The Options bag containing the bootstrap options.
:param file stdin: The stream representing stdin.
:param file stdout: The stream representing stdout.
Expand All @@ -47,12 +45,14 @@ def __init__(self, exiter, args, env, process_metadata_dir,
self._exiter = exiter
self._args = args
self._env = env
self._process_metadata_dir = process_metadata_dir
self._bootstrap_options = bootstrap_options
self._stdin = stdin or sys.stdin
self._stdout = stdout or sys.stdout
self._stderr = stderr or sys.stderr
self._launcher = PantsDaemonLauncher(self._bootstrap_options, EngineInitializer)

@memoized_property
def pantsd(self):
return PantsDaemon.Factory.create(bootstrap_options=self._bootstrap_options)

@contextmanager
def _trapped_control_c(self, client):
Expand Down Expand Up @@ -80,14 +80,6 @@ def _setup_logging(self):
root.setLevel(log_level)
root.addHandler(handler)

def _find_or_launch_pantsd(self):
"""Launches pantsd if configured to do so.
:returns: The port pantsd can be found on.
:rtype: int
"""
return self._launcher.maybe_launch()

def _connect_and_execute(self, port):
# Merge the nailgun TTY capability environment variables with the passed environment dict.
ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr)
Expand All @@ -111,7 +103,7 @@ def _connect_and_execute(self, port):

def run(self, args=None):
self._setup_logging()
port = self._find_or_launch_pantsd()
port = self.pantsd.maybe_launch()

logger.debug('connecting to pailgun on port {}'.format(port))
try:
Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/core_tasks/pantsd_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
unicode_literals, with_statement)

from pants.base.exceptions import TaskError
from pants.pantsd.pants_daemon_launcher import PantsDaemonLauncher
from pants.pantsd.pants_daemon import PantsDaemon
from pants.pantsd.process_manager import ProcessManager
from pants.task.task import Task

Expand All @@ -16,6 +16,6 @@ class PantsDaemonKill(Task):

def execute(self):
try:
PantsDaemonLauncher(self.get_options()).terminate()
PantsDaemon.Factory.create(self.get_options()).terminate()
except ProcessManager.NonResponsiveProcess as e:
raise TaskError('failure while terminating pantsd: {}'.format(e))
24 changes: 8 additions & 16 deletions src/python/pants/pantsd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python_library(
dependencies = [
'3rdparty/python:psutil',
'src/python/pants/base:build_environment',
'src/python/pants/process',
'src/python/pants/util:dirutil',
'src/python/pants/util:process_handler',
]
Expand Down Expand Up @@ -57,27 +58,18 @@ python_library(
sources = ['pants_daemon.py'],
dependencies = [
'3rdparty/python:setproctitle',
'src/python/pants/base:build_environment',
'src/python/pants/base:exiter',
'src/python/pants/binaries:binary_util',
'src/python/pants/engine:native',
'src/python/pants/goal:run_tracker',
'src/python/pants/init',
'src/python/pants/logging',
':process_manager',
]
)

python_library(
name = 'pants_daemon_launcher',
sources = ['pants_daemon_launcher.py'],
dependencies = [
':pants_daemon',
':watchman_launcher',
'src/python/pants/pantsd/service:fs_event_service',
'src/python/pants/pantsd/service:pailgun_service',
'src/python/pants/pantsd/service:scheduler_service',
'src/python/pants/binaries:binary_util',
'src/python/pants/base:build_environment',
'src/python/pants/engine:native',
'src/python/pants/init',
'src/python/pants/process',
'src/python/pants/util:memo'
'src/python/pants/util:memo',
':process_manager',
':watchman_launcher'
]
)
Loading

0 comments on commit 8c03533

Please sign in to comment.