Search in sources :

Example 1 with ListOptions

use of org.apache.flink.client.cli.ListOptions in project flink by apache.

the class CliFrontend method list.

/**
	 * Executes the list action.
	 * 
	 * @param args Command line arguments for the list action.
	 */
protected int list(String[] args) {
    LOG.info("Running 'list' command.");
    ListOptions options;
    try {
        options = CliFrontendParser.parseListCommand(args);
    } catch (CliArgsException e) {
        return handleArgException(e);
    } catch (Throwable t) {
        return handleError(t);
    }
    // evaluate help flag
    if (options.isPrintHelp()) {
        CliFrontendParser.printHelpForList();
        return 0;
    }
    boolean running = options.getRunning();
    boolean scheduled = options.getScheduled();
    // print running and scheduled jobs if not option supplied
    if (!running && !scheduled) {
        running = true;
        scheduled = true;
    }
    try {
        ActorGateway jobManagerGateway = getJobManagerGateway(options);
        LOG.info("Connecting to JobManager to retrieve list of jobs");
        Future<Object> response = jobManagerGateway.ask(JobManagerMessages.getRequestRunningJobsStatus(), clientTimeout);
        Object result;
        try {
            result = Await.result(response, clientTimeout);
        } catch (Exception e) {
            throw new Exception("Could not retrieve running jobs from the JobManager.", e);
        }
        if (result instanceof RunningJobsStatus) {
            LOG.info("Successfully retrieved list of jobs");
            List<JobStatusMessage> jobs = ((RunningJobsStatus) result).getStatusMessages();
            ArrayList<JobStatusMessage> runningJobs = null;
            ArrayList<JobStatusMessage> scheduledJobs = null;
            if (running) {
                runningJobs = new ArrayList<JobStatusMessage>();
            }
            if (scheduled) {
                scheduledJobs = new ArrayList<JobStatusMessage>();
            }
            for (JobStatusMessage rj : jobs) {
                if (running && (rj.getJobState().equals(JobStatus.RUNNING) || rj.getJobState().equals(JobStatus.RESTARTING))) {
                    runningJobs.add(rj);
                }
                if (scheduled && rj.getJobState().equals(JobStatus.CREATED)) {
                    scheduledJobs.add(rj);
                }
            }
            SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            Comparator<JobStatusMessage> njec = new Comparator<JobStatusMessage>() {

                @Override
                public int compare(JobStatusMessage o1, JobStatusMessage o2) {
                    return (int) (o1.getStartTime() - o2.getStartTime());
                }
            };
            if (running) {
                if (runningJobs.size() == 0) {
                    System.out.println("No running jobs.");
                } else {
                    Collections.sort(runningJobs, njec);
                    System.out.println("------------------ Running/Restarting Jobs -------------------");
                    for (JobStatusMessage rj : runningJobs) {
                        System.out.println(df.format(new Date(rj.getStartTime())) + " : " + rj.getJobId() + " : " + rj.getJobName() + " (" + rj.getJobState() + ")");
                    }
                    System.out.println("--------------------------------------------------------------");
                }
            }
            if (scheduled) {
                if (scheduledJobs.size() == 0) {
                    System.out.println("No scheduled jobs.");
                } else {
                    Collections.sort(scheduledJobs, njec);
                    System.out.println("----------------------- Scheduled Jobs -----------------------");
                    for (JobStatusMessage rj : scheduledJobs) {
                        System.out.println(df.format(new Date(rj.getStartTime())) + " : " + rj.getJobId() + " : " + rj.getJobName());
                    }
                    System.out.println("--------------------------------------------------------------");
                }
            }
            return 0;
        } else {
            throw new Exception("ReqeustRunningJobs requires a response of type " + "RunningJobs. Instead the response is of type " + result.getClass() + ".");
        }
    } catch (Throwable t) {
        return handleError(t);
    }
}
Also used : ListOptions(org.apache.flink.client.cli.ListOptions) CliArgsException(org.apache.flink.client.cli.CliArgsException) ProgramInvocationException(org.apache.flink.client.program.ProgramInvocationException) ProgramMissingJobException(org.apache.flink.client.program.ProgramMissingJobException) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) ProgramParametrizationException(org.apache.flink.client.program.ProgramParametrizationException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) CliArgsException(org.apache.flink.client.cli.CliArgsException) IOException(java.io.IOException) Date(java.util.Date) Comparator(java.util.Comparator) RunningJobsStatus(org.apache.flink.runtime.messages.JobManagerMessages.RunningJobsStatus) JobStatusMessage(org.apache.flink.runtime.client.JobStatusMessage) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)1 CliArgsException (org.apache.flink.client.cli.CliArgsException)1 ListOptions (org.apache.flink.client.cli.ListOptions)1 ProgramInvocationException (org.apache.flink.client.program.ProgramInvocationException)1 ProgramMissingJobException (org.apache.flink.client.program.ProgramMissingJobException)1 ProgramParametrizationException (org.apache.flink.client.program.ProgramParametrizationException)1 IllegalConfigurationException (org.apache.flink.configuration.IllegalConfigurationException)1 JobStatusMessage (org.apache.flink.runtime.client.JobStatusMessage)1 ActorGateway (org.apache.flink.runtime.instance.ActorGateway)1 RunningJobsStatus (org.apache.flink.runtime.messages.JobManagerMessages.RunningJobsStatus)1