public Runnable createAction(CommandLineParser parser, ParsedCommandLine commandLine) {
    Parameters parameters = parametersConverter.convert(commandLine, new Parameters());
    parameters
        .getDaemonParameters()
        .applyDefaultsFor(
            new JvmVersionDetector(new DefaultExecActionFactory(new IdentityFileResolver()))
                .getJavaVersion(parameters.getDaemonParameters().getEffectiveJvm()));

    if (parameters.getDaemonParameters().isStop()) {
      return stopAllDaemons(parameters.getDaemonParameters(), loggingServices);
    }
    if (parameters.getDaemonParameters().isForeground()) {
      DaemonParameters daemonParameters = parameters.getDaemonParameters();
      ForegroundDaemonConfiguration conf =
          new ForegroundDaemonConfiguration(
              UUID.randomUUID().toString(),
              daemonParameters.getBaseDir(),
              daemonParameters.getIdleTimeout(),
              daemonParameters.getPeriodicCheckInterval());
      return new ForegroundDaemonAction(loggingServices, conf);
    }
    if (parameters.getDaemonParameters().getDaemonUsage().isEnabled()) {
      return runBuildWithDaemon(
          parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices);
    }
    if (canUseCurrentProcess(parameters.getDaemonParameters())) {
      return runBuildInProcess(
          parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices);
    }

    return runBuildInSingleUseDaemon(
        parameters.getStartParameter(), parameters.getDaemonParameters(), loggingServices);
  }
  private Runnable runBuildInSingleUseDaemon(
      StartParameter startParameter,
      DaemonParameters daemonParameters,
      ServiceRegistry loggingServices) {
    // (SF) this is a workaround until this story is completed. I'm hardcoding setting the idle
    // timeout to be max X mins.
    // this way we avoid potential runaway daemons that steal resources on linux and break builds on
    // windows.
    // We might leave that in if we decide it's a good idea for an extra safety net.
    int maxTimeout = 2 * 60 * 1000;
    if (daemonParameters.getIdleTimeout() > maxTimeout) {
      daemonParameters.setIdleTimeout(maxTimeout);
    }
    // end of workaround.

    // Create a client that will not match any existing daemons, so it will always startup a new one
    ServiceRegistry clientSharedServices = createGlobalClientServices();
    ServiceRegistry clientServices =
        clientSharedServices
            .get(DaemonClientFactory.class)
            .createSingleUseDaemonClientServices(
                loggingServices.get(OutputEventListener.class), daemonParameters, System.in);
    DaemonClient client = clientServices.get(DaemonClient.class);
    return runBuild(startParameter, daemonParameters, client, clientSharedServices);
  }
 private DaemonParameters constructDaemonParameters(StartParameter startParameter) {
   Map<String, String> mergedSystemProperties = startParameter.getMergedSystemProperties();
   DaemonParameters daemonParameters = new DaemonParameters();
   daemonParameters.configureFromBuildDir(
       startParameter.getCurrentDir(), startParameter.isSearchUpwards());
   daemonParameters.configureFromGradleUserHome(startParameter.getGradleUserHomeDir());
   daemonParameters.configureFromSystemProperties(mergedSystemProperties);
   return daemonParameters;
 }
 public DaemonRegistry getDaemonRegistry() {
   DaemonParameters parameters = new DaemonParameters(new BuildLayoutParameters());
   parameters.setBaseDir(getDaemonBaseDir());
   return new DaemonClientServices(
           LoggingServiceRegistry.newEmbeddableLogging(),
           parameters,
           new ByteArrayInputStream(new byte[0]))
       .get(DaemonRegistry.class);
 }
 private Action<ExecutionListener> daemonBuildAction(
     ParsedCommandLine commandLine, DaemonParameters daemonParameters, DaemonClient client) {
   return new ActionAdapter(
       new DaemonBuildAction(
           client,
           commandLine,
           getWorkingDir(),
           clientMetaData(),
           getBuildStartTime(),
           daemonParameters.getEffectiveSystemProperties(),
           System.getenv()));
 }
  private Action<ExecutionListener> runBuildInSingleUseDaemon(
      ParsedCommandLine commandLine,
      DaemonParameters daemonParameters,
      ServiceRegistry loggingServices) {
    // (SF) this is a workaround until this story is completed. I'm hardcoding setting the idle
    // timeout to be max X mins.
    // this way we avoid potential runaway daemons that steal resources on linux and break builds on
    // windows.
    // We might leave that in if we decide it's a good idea for an extra safety net.
    int maxTimeout = 2 * 60 * 1000;
    if (daemonParameters.getIdleTimeout() > maxTimeout) {
      daemonParameters.setIdleTimeout(maxTimeout);
    }
    // end of workaround.

    // Create a client that will not match any existing daemons, so it will always startup a new one
    DaemonClientServices clientServices =
        new SingleUseDaemonClientServices(loggingServices, daemonParameters, System.in);
    DaemonClient client = clientServices.get(DaemonClient.class);
    return daemonBuildAction(commandLine, daemonParameters, client);
  }
  private Action<ExecutionListener> createAction(
      CommandLineParser parser,
      final ParsedCommandLine commandLine,
      CommandLineConverter<StartParameter> startParameterConverter,
      final ServiceRegistry loggingServices) {
    if (commandLine.hasOption(HELP)) {
      return new ActionAdapter(new ShowUsageAction(parser));
    }
    if (commandLine.hasOption(VERSION)) {
      return new ActionAdapter(new ShowVersionAction());
    }
    if (commandLine.hasOption(GUI)) {
      return new ActionAdapter(new ShowGuiAction());
    }

    final StartParameter startParameter = new StartParameter();
    startParameterConverter.convert(commandLine, startParameter);
    DaemonParameters daemonParameters = constructDaemonParameters(startParameter);
    if (commandLine.hasOption(FOREGROUND)) {
      ForegroundDaemonConfiguration conf =
          new ForegroundDaemonConfiguration(
              daemonParameters.getUid(),
              daemonParameters.getBaseDir(),
              daemonParameters.getIdleTimeout());
      return new ActionAdapter(new ForegroundDaemonMain(conf));
    }

    if (commandLine.hasOption(STOP)) {
      return stopAllDaemons(daemonParameters, loggingServices);
    }
    if (useDaemon(commandLine, daemonParameters)) {
      return runBuildWithDaemon(commandLine, daemonParameters, loggingServices);
    }
    if (canUseCurrentProcess(daemonParameters)) {
      return runBuildInProcess(loggingServices, startParameter);
    }
    return runBuildInSingleUseDaemon(commandLine, daemonParameters, loggingServices);
  }
 private Runnable runBuild(
     StartParameter startParameter,
     DaemonParameters daemonParameters,
     BuildActionExecuter<BuildActionParameters> executer,
     ServiceRegistry sharedServices) {
   BuildActionParameters parameters =
       new DefaultBuildActionParameters(
           daemonParameters.getEffectiveSystemProperties(),
           System.getenv(),
           SystemProperties.getInstance().getCurrentDir(),
           startParameter.getLogLevel(),
           daemonParameters.getDaemonUsage(),
           startParameter.isContinuous(),
           daemonParameters.isInteractive(),
           ClassPath.EMPTY);
   return new RunBuildAction(
       executer,
       startParameter,
       clientMetaData(),
       getBuildStartTime(),
       parameters,
       sharedServices);
 }
  private Parameters initParams(ProviderOperationParameters operationParameters) {
    BuildLayoutParameters layout = new BuildLayoutParameters();
    if (operationParameters.getGradleUserHomeDir() != null) {
      layout.setGradleUserHomeDir(operationParameters.getGradleUserHomeDir());
    }
    layout.setSearchUpwards(
        operationParameters.isSearchUpwards() != null
            ? operationParameters.isSearchUpwards()
            : true);
    layout.setProjectDir(operationParameters.getProjectDir());

    Map<String, String> properties = new HashMap<String, String>();
    new LayoutToPropertiesConverter().convert(layout, properties);

    DaemonParameters daemonParams = new DaemonParameters(layout);
    new PropertiesToDaemonParametersConverter().convert(properties, daemonParams);
    if (operationParameters.getDaemonBaseDir(null) != null) {
      daemonParams.setBaseDir(operationParameters.getDaemonBaseDir(null));
    }

    // override the params with the explicit settings provided by the tooling api
    List<String> defaultJvmArgs = daemonParams.getAllJvmArgs();
    daemonParams.setJvmArgs(operationParameters.getJvmArguments(defaultJvmArgs));
    File defaultJavaHome = daemonParams.getEffectiveJavaHome();
    daemonParams.setJavaHome(operationParameters.getJavaHome(defaultJavaHome));

    if (operationParameters.getDaemonMaxIdleTimeValue() != null
        && operationParameters.getDaemonMaxIdleTimeUnits() != null) {
      int idleTimeout =
          (int)
              operationParameters
                  .getDaemonMaxIdleTimeUnits()
                  .toMillis(operationParameters.getDaemonMaxIdleTimeValue());
      daemonParams.setIdleTimeout(idleTimeout);
    }

    return new Parameters(daemonParams, properties);
  }
 private boolean useDaemon(ParsedCommandLine commandLine, DaemonParameters daemonParameters) {
   boolean useDaemon = daemonParameters.isEnabled();
   useDaemon = useDaemon || commandLine.hasOption(DAEMON);
   useDaemon = useDaemon && !commandLine.hasOption(NO_DAEMON);
   return useDaemon;
 }
Beispiel #11
0
 private boolean hasSystemProperties(DaemonParameters requiredBuildParameters) {
   return containsAll(
       getJvmOptions().getSystemProperties(), requiredBuildParameters.getSystemProperties());
 }
Beispiel #12
0
 private boolean hasJvmArgs(DaemonParameters requiredBuildParameters) {
   return requiredBuildParameters.isUsingDefaultJvmArgs()
       || (effectiveJvmOptions
           .getManagedJvmArgs()
           .equals(requiredBuildParameters.getEffectiveJvmArgs()));
 }
Beispiel #13
0
 private boolean hasJavaHome(DaemonParameters requiredJavaHome) {
   return getJavaHome().equals(requiredJavaHome.getEffectiveJavaHome());
 }
  public DaemonStartupInfo startDaemon() {
    String daemonUid = UUID.randomUUID().toString();

    GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
    ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation);
    ClassPath classpath;
    List<File> searchClassPath;
    if (gradleInstallation == null) {
      // When not running from a Gradle distro, need runtime impl for launcher plus the search path
      // to look for other modules
      classpath = new DefaultClassPath();
      for (Module module : registry.getModule("gradle-launcher").getAllRequiredModules()) {
        classpath = classpath.plus(module.getClasspath());
      }
      searchClassPath = registry.getAdditionalClassPath().getAsFiles();
    } else {
      // When running from a Gradle distro, only need launcher jar. The daemon can find everything
      // from there.
      classpath = registry.getModule("gradle-launcher").getImplementationClasspath();
      searchClassPath = Collections.emptyList();
    }
    if (classpath.isEmpty()) {
      throw new IllegalStateException(
          "Unable to construct a bootstrap classpath when starting the daemon");
    }

    versionValidator.validate(daemonParameters);

    List<String> daemonArgs = new ArrayList<String>();
    daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath());

    List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs();
    daemonArgs.addAll(daemonOpts);
    daemonArgs.add("-cp");
    daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles()));

    if (Boolean.getBoolean("org.gradle.daemon.debug")) {
      daemonArgs.add("-Xdebug");
      daemonArgs.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005");
    }
    LOGGER.debug("Using daemon args: {}", daemonArgs);

    daemonArgs.add(GradleDaemon.class.getName());
    // Version isn't used, except by a human looking at the output of jps.
    daemonArgs.add(GradleVersion.current().getVersion());

    // Serialize configuration to daemon via the process' stdin
    ByteArrayOutputStream serializedConfig = new ByteArrayOutputStream();
    FlushableEncoder encoder =
        new KryoBackedEncoder(new EncodedStream.EncodedOutput(serializedConfig));
    try {
      encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath());
      encoder.writeString(daemonDir.getBaseDir().getAbsolutePath());
      encoder.writeSmallInt(daemonParameters.getIdleTimeout());
      encoder.writeString(daemonUid);
      encoder.writeSmallInt(daemonOpts.size());
      for (String daemonOpt : daemonOpts) {
        encoder.writeString(daemonOpt);
      }
      encoder.writeSmallInt(searchClassPath.size());
      for (File file : searchClassPath) {
        encoder.writeString(file.getAbsolutePath());
      }
      encoder.flush();
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
    ByteArrayInputStream stdInput = new ByteArrayInputStream(serializedConfig.toByteArray());

    DaemonStartupInfo daemonInfo = startProcess(daemonArgs, daemonDir.getVersionedDir(), stdInput);
    listener.daemonStarted(daemonInfo);
    return daemonInfo;
  }