Launch a Program and wait until it's terminated.

@return A Launch instance.
@throws LaunchException
When a SchedulerException occurred.
515580
public Launch synchLaunch() throws LaunchException {
		JobDataMap jobDataMap = new JobDataMap();
		jobDataMap.put(Launch.PARAMETERS, this.launch.getParameters());
		jobDataMap.put(Launch.DEFAULT_EXECUTOR_KEY, this.launch.getExecutorService());
		try {
			this.launch.synchLaunch();
		} catch (org.parallelj.launching.LaunchException e) {
			throw new LaunchException(e);
		}
		
		org.parallelj.launching.LaunchResult result = this.launch.getLaunchResult();
		jobDataMap.put(QuartzUtils.RETURN_CODE, result.getStatusCode());
		jobDataMap.put(QuartzUtils.USER_RETURN_CODE, result.getReturnCode());
		jobDataMap.put(QuartzUtils.PROCEDURES_IN_ERROR, result.getProceduresInError());
		jobDataMap.put(Launch.OUTPUTS, this.launch.getLaunchResult().getOutputParameters());
		this.legacyLaunchResult = new LaunchResult(launch.getLaunchId(), jobDataMap);
		
		return this;
	}
493826
public static void main(final String[] args) {
    LOG.log(Level.INFO, "Entering Launch at :::" + new Date());
    try {
      if (args == null || args.length == 0) {
        throw new IllegalArgumentException("No arguments provided, at least a clrFolder should be supplied.");
      }
      final File dotNetFolder = new File(args[0]).getAbsoluteFile();
      final String[] removedArgs = Arrays.copyOfRange(args, 1, args.length);

      final Configuration config = getClientConfiguration(removedArgs);
      final Injector commandLineInjector = Tang.Factory.getTang().newInjector(parseCommandLine(removedArgs));
      final int waitTime = commandLineInjector.getNamedInstance(WaitTimeForDriver.class);
      final int driverMemory = commandLineInjector.getNamedInstance(DriverMemoryInMb.class);
      final boolean isLocal = commandLineInjector.getNamedInstance(Local.class);
      final String driverIdentifier = commandLineInjector.getNamedInstance(DriverIdentifier.class);
      final String jobSubmissionDirectory = commandLineInjector.getNamedInstance(DriverJobSubmissionDirectory.class);
      final boolean submit = commandLineInjector.getNamedInstance(Submit.class);
      final Injector injector = Tang.Factory.getTang().newInjector(config);
      final JobClient client = injector.getInstance(JobClient.class);
      client.setDriverInfo(driverIdentifier, driverMemory, jobSubmissionDirectory);

      if (submit) {
        client.submit(dotNetFolder, true, isLocal, null);
        client.waitForCompletion(waitTime);
      } else {
        client.submit(dotNetFolder, false, isLocal, config);
        client.waitForCompletion(0);
      }


      LOG.info("Done!");
    } catch (final BindException | InjectionException | IOException ex) {
      LOG.log(Level.SEVERE, "Job configuration error", ex);
    }
  }
765648
public static void main(String args[]) throws Exception {
        LaunchAppropriateUI launcher = new LaunchAppropriateUI(args);
        launcher.launch();
    }
164476
public ListenableFuture startAsync() {
        // This is run in a background thread by the Service implementation.
        if (chain == null) {
            // Just try to help catch what might be a programming error.
            log.warn("Starting up with no attached block chain. Did you forget to pass one to the constructor?");
        }
        checkState(!vUsedUp, "Cannot start a peer group twice");
        vRunning = true;
        vUsedUp = true;
        executorStartupLatch.countDown();
        // We do blocking waits during startup, so run on the executor thread.
        return executor.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("Starting ...");
                    channels.startAsync();
                    channels.awaitRunning();
                    triggerConnections();
                    setupPinging();
                } catch (Throwable e) {
                    log.error("Exception when starting up", e);  // The executor swallows exceptions :(
                }
            }
        });
    }
591034
@Override
	public void start() throws Exception {
		synchronized (lock) {
			if (jobExecutorService == null) {
				// create the embedded runtime
				jobExecutorServiceConfiguration = createConfiguration();

				// start it up
				jobExecutorService = createJobExecutorService(jobExecutorServiceConfiguration);
			} else {
				throw new IllegalStateException("The local executor was already started.");
			}
		}
	}
808031
private void runJobExecutionLauncher() throws JobException {
    long startTime = 0;
    String newPlanningId;
    String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY);
    Closer closer = Closer.create();
    try {
      HelixManager planningJobManager = this.taskDriverHelixManager.isPresent()?
          this.taskDriverHelixManager.get() : this.jobHelixManager;

      String builderStr = jobProps.getProperty(GobblinClusterConfigurationKeys.DISTRIBUTED_JOB_LAUNCHER_BUILDER,
          GobblinHelixDistributeJobExecutionLauncher.Builder.class.getName());

      // Check if any existing planning job is running
      Optional<String> planningJobIdFromStore = jobsMapping.getPlanningJobId(jobName);
      boolean nonblocking = false;
      // start of critical section to check if a job with same job name is running
      Lock jobLock = locks.get(jobName);
      jobLock.lock();

      try {
        if (planningJobIdFromStore.isPresent()) {
          String previousPlanningJobId = planningJobIdFromStore.get();

          if (HelixUtils.isJobFinished(previousPlanningJobId, previousPlanningJobId, planningJobManager)) {
            log.info("Previous planning job {} has reached to the final state. Start a new one.", previousPlanningJobId);
          } else {
            boolean killDuplicateJob = PropertiesUtils
                .getPropAsBoolean(this.jobProps, GobblinClusterConfigurationKeys.KILL_DUPLICATE_PLANNING_JOB, String.valueOf(GobblinClusterConfigurationKeys.DEFAULT_KILL_DUPLICATE_PLANNING_JOB));

            if (!killDuplicateJob) {
              log.info("Previous planning job {} has not finished yet. Skip this job.", previousPlanningJobId);
              return;
            } else {
              log.info("Previous planning job {} has not finished yet. Kill it.", previousPlanningJobId);
              long timeOut = PropertiesUtils.getPropAsLong(sysProps, GobblinClusterConfigurationKeys.HELIX_WORKFLOW_DELETE_TIMEOUT_SECONDS,
                  GobblinClusterConfigurationKeys.DEFAULT_HELIX_WORKFLOW_DELETE_TIMEOUT_SECONDS) * 1000;
              try {
                HelixUtils.deleteWorkflow(previousPlanningJobId, planningJobManager, timeOut);
              } catch (HelixException e) {
                log.info("Helix cannot delete previous planning job id {} within {} seconds.", previousPlanningJobId,
                    timeOut / 1000);
                throw new JobException("Helix cannot delete previous planning job id " + previousPlanningJobId, e);
              }
            }
          }
        } else {
          log.info("Planning job for {} does not exist. First time run.", jobName);
        }

        GobblinHelixDistributeJobExecutionLauncher.Builder builder = GobblinConstructorUtils.<GobblinHelixDistributeJobExecutionLauncher.Builder>invokeLongestConstructor(
            new ClassAliasResolver(GobblinHelixDistributeJobExecutionLauncher.Builder.class).resolveClass(builderStr));

        // Make a separate copy because we could update some of attributes in job properties (like adding planning id).
        Properties jobPlanningProps = new Properties();
        jobPlanningProps.putAll(this.jobProps);

        // Inject planning id and start time
        newPlanningId = HelixJobsMapping.createPlanningJobId(jobPlanningProps);
        jobPlanningProps.setProperty(GobblinClusterConfigurationKeys.PLANNING_ID_KEY, newPlanningId);
        jobPlanningProps.setProperty(GobblinClusterConfigurationKeys.PLANNING_JOB_CREATE_TIME, String.valueOf(System.currentTimeMillis()));

        builder.setSysProps(this.sysProps);
        builder.setJobPlanningProps(jobPlanningProps);
        builder.setJobHelixManager(this.jobHelixManager);
        builder.setTaskDriverHelixManager(this.taskDriverHelixManager);
        builder.setAppWorkDir(this.appWorkDir);
        builder.setJobsMapping(this.jobsMapping);
        builder.setPlanningJobLauncherMetrics(this.planningJobLauncherMetrics);
        builder.setHelixMetrics(this.helixMetrics);

        // if the distributed job launcher should wait for planning job completion

        Config combined = ConfigUtils.propertiesToConfig(jobPlanningProps)
            .withFallback(ConfigUtils.propertiesToConfig(sysProps));

        nonblocking = ConfigUtils.getBoolean(combined, GobblinClusterConfigurationKeys.NON_BLOCKING_PLANNING_JOB_ENABLED,
            GobblinClusterConfigurationKeys.DEFAULT_NON_BLOCKING_PLANNING_JOB_ENABLED);

        log.info("Planning job {} started.", newPlanningId);
        GobblinHelixDistributeJobExecutionLauncher launcher = builder.build();
        closer.register(launcher);
        this.jobsMapping.setPlanningJobId(jobName, newPlanningId);
        startTime = System.currentTimeMillis();
        this.currentJobMonitor = launcher.launchJob(null);

        // make sure the planning job is initialized (or visible) to other parallel running threads,
        // so that the same critical section check (querying Helix for job completeness)
        // can be applied.
        HelixUtils.waitJobInitialization(planningJobManager, newPlanningId, newPlanningId, 300_000);

      } finally {
        // end of the critical section to check if a job with same job name is running
        jobLock.unlock();
      }

      // we can remove the job spec from the catalog because Helix will drive this job to the end.
      this.deleteJobSpec();

      // If we are using non-blocking mode, this get() only guarantees the plannning job is submitted.
      // It doesn't guarantee the job will finish because internally we won't wait for Helix completion.
      this.currentJobMonitor.get();
      this.currentJobMonitor = null;
      if (nonblocking) {
        log.info("Planning job {} submitted successfully.", newPlanningId);
      } else {
        log.info("Planning job {} finished.", newPlanningId);
        this.planningJobLauncherMetrics.updateTimeForCompletedPlanningJobs(startTime);
      }
    } catch (Exception e) {
      if (startTime != 0) {
        this.planningJobLauncherMetrics.updateTimeForFailedPlanningJobs(startTime);
      }
      log.error("Failed to run planning job {}", jobName, e);
      throw new JobException("Failed to run planning job " + jobName, e);
    } finally {
      try {
        closer.close();
      } catch (IOException e) {
        throw new JobException("Cannot properly close planning job " + jobName, e);
      }
    }
  }
213787
public void run() throws Exception {
        // Send the initial command:
        client.sendRequest(cmd);

        long endTime = System.currentTimeMillis() + spec.getMaxDuration();

        // Wait until the 'after' time
        Thread.sleep(spec.getAfter());

        int numAttempts = 0;
        long now = System.currentTimeMillis();

        while (now < endTime) {
            client.sendRequest(cmd);
            now = System.currentTimeMillis();

            numAttempts ++;

            // See how to retry:
            long sleepTime = 0;
            if (spec.isConstant()) {
                sleepTime = spec.getInterval();
            } else if (spec.isLinear()) {
                sleepTime = spec.getInterval() * numAttempts;
            } else if (spec.isExponential()) {
                sleepTime = (long) Math.pow(spec.getInterval(), numAttempts);
            }

            if (spec.getCeil() > 0) {
                sleepTime = Math.min(spec.getCeil(), sleepTime);
            }

            if (now + sleepTime > endTime) {
                break;
            } else {
                accuSleep(sleepTime);
                now = System.currentTimeMillis();
            }
        }
    }
914437
public ScheduledFuture<?> schedule() {
        synchronized (this.triggerContextMonitor) {
            this.scheduledExecutionTime = this.trigger.nextExecutionTime(this.triggerContext);
            if (this.scheduledExecutionTime == null) {
                return null;
            }
            long initialDelay = this.scheduledExecutionTime.getTime() - System.currentTimeMillis();
            this.currentFuture = this.executor.schedule(this, initialDelay, TimeUnit.MILLISECONDS);
            return this;
        }
    }
653659
public void waitFor(long timeout) throws JSchException, ExitCode, InterruptedException {
        long deadline;

        try {
            deadline = System.currentTimeMillis() + timeout;
            while (!channel.isClosed()) {
                if (System.currentTimeMillis() >= deadline) {
                    throw new TimeoutException(this);
                }
                Thread.sleep(100); // throws InterruptedException
            }
            if (channel.getExitStatus() != 0) {
                throw new ExitCode(new Launcher(command), channel.getExitStatus());
            }
        } finally {
            channel.disconnect();
        }
    }
793507
protected static void preloadAndLaunch(final Class<? extends Application> appClass, final Class<? extends Preloader> preloaderClass, final String... args) {
        LauncherImpl.launchApplication(appClass, preloaderClass, args);
    }
344771
protected void launch(String[] args, String mainClass, ClassLoader classLoader)
            throws Exception {
        Runnable runner = createMainMethodRunner(mainClass, args, classLoader);
        Thread.currentThread().setContextClassLoader(classLoader);
        runner.run();
    }
600069
@Override
    public void run() {
        // Check for a pending exception.  A CancellationException could be set if a cancel() was driven while this Runnable
        // was being re-scheduled.
        if (this.m_pendingException != null) {
            return;
        }

        // NOTE: Already running in Default Executor thread.
        try {
            this.m_runnable.run();
        } catch (Exception e) {
            this.m_pendingException = new ExecutionException(e);
            this.m_coordinationLatch.countDown();
            return;
        }

        try {
            long scheduleTime = this.m_periodInterval;
            if (!this.m_scheduledWithDelay) {
                this.m_myNextExecutionTime = this.m_myNextExecutionTime + this.m_periodInterval;
                long currentTime = System.nanoTime();

                scheduleTime = (this.m_myNextExecutionTime > currentTime) ? (this.m_myNextExecutionTime - currentTime) : 0;
            }

            m_schedFuture = this.m_scheduledExecutor.schedule(this, scheduleTime, TimeUnit.NANOSECONDS);
        } catch (Exception e) {
            this.m_pendingException = e;
            this.m_coordinationLatch.countDown();
        }
    }
1281100
public Process launch() throws IOException {
        final ProcessBuilder processBuilder = new ProcessBuilder(builder.build());
        if (outputDestination != null) {
            processBuilder.redirectOutput(outputDestination);
        }
        if (errorDestination != null) {
            processBuilder.redirectError(errorDestination);
        }
        if (workingDirectory != null) {
            processBuilder.directory(workingDirectory);
        }
        if (!env.isEmpty()) {
            processBuilder.environment().putAll(env);
        }
        processBuilder.redirectErrorStream(redirectErrorStream);
        return processBuilder.start();
    }
104203
protected void start() {
        // if a periodic execution throws an exception, future executions are suspended,
        // this task wraps the call in a try-catch block to prevent that. Errors are still propagated.
        final Runnable resilientTask = () -> {
            try {
                run();
            } catch (final Exception e) {
                logger.error("[{}] Error during timeout-initiated flush", name, e);
            }
        };

        scheduler.scheduleAtFixedRate(resilientTask, 0, batchTimeout + salt, TimeUnit.MILLISECONDS);
    }
691208
protected final WaitForProcess startInternal() throws IOException {
    // Invoke listeners - they can modify this executor
    listeners.beforeStart(this);

    if (builder.command().isEmpty()) {
      throw new IllegalStateException("Command has not been set.");
    }
    validateStreams(streams, readOutput);

    applyEnvironment();
    messageLogger.message(log, getExecutingLogMessage());
    Process process = invokeStart();
    messageLogger.message(log, "Started {}", process);
    ProcessAttributes attributes = getAttributes();

    ExecuteStreamHandler newStreams = streams;
    ByteArrayOutputStream out = null;
    if (readOutput) {
      PumpStreamHandler pumps = (PumpStreamHandler) streams;
      out = new ByteArrayOutputStream();
      newStreams = redirectOutputAlsoTo(pumps, out);
    }
    return startInternal(process, attributes, newStreams, out);
  }
167191
@Override
	public void start() throws Exception {
		synchronized (lock) {
			if (client == null) {
				client = new RestClusterClient<>(clientConfiguration, "RemoteExecutor");
				client.setPrintStatusDuringExecution(isPrintingStatusDuringExecution());
			}
			else {
				throw new IllegalStateException("The remote executor was already started.");
			}
		}
	}
293954
private static void launch(String[] args, String mainClass) throws Throwable {
        EeClassLoader classLoader = new EeClassLoader();
        classLoader.invokeMain(mainClass, args);
    }