Search in sources :

Example 1 with JobExecutionList

use of org.platformlayer.jobs.model.JobExecutionList in project platformlayer by platformlayer.

the class HttpPlatformLayerClient method listJobExecutions.

@Override
public JobExecutionList listJobExecutions(String jobId) throws PlatformLayerClientException {
    String relativePath = "jobs/" + jobId + "/runs";
    JobExecutionList executions = doRequest(HttpMethod.GET, relativePath, JobExecutionList.class, Format.XML, null, null);
    return executions;
}
Also used : JobExecutionList(org.platformlayer.jobs.model.JobExecutionList)

Example 2 with JobExecutionList

use of org.platformlayer.jobs.model.JobExecutionList in project platformlayer by platformlayer.

the class PlatformLayerTestContext method waitForJobComplete.

public JobData waitForJobComplete(JobData job, TimeSpan timeout) throws OpsException, IOException {
    TypedPlatformLayerClient client = getTypedClient();
    PlatformLayerKey jobKey = job.key;
    long startedAt = System.currentTimeMillis();
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new IllegalStateException("Interrupted", e);
        }
        if (timeout != null && timeout.hasTimedOut(startedAt)) {
            throw new OpsException("Timeout waiting for job completion");
        }
        // TODO: We really need a "get job status" function
        JobData found = null;
        for (JobData candidate : client.listJobs().getJobs()) {
            if (jobKey.equals(candidate.getJobKey())) {
                found = candidate;
            }
        }
        if (found == null) {
            // Assume completed?
            throw new IllegalStateException("Job not found in job list");
        }
        JobExecutionList executions = client.listJobExecutions(job.getJobKey().getItemIdString());
        JobExecutionData foundExecution = null;
        for (JobExecutionData candidate : executions) {
            if (jobKey.equals(candidate.getJobKey())) {
                foundExecution = candidate;
            }
        }
        if (foundExecution == null) {
            throw new IllegalStateException("Execution not found in execution list");
        }
        JobState state = foundExecution.getState();
        switch(state) {
            case FAILED:
            case SUCCESS:
                System.out.println("Job completed; state=" + state);
                return found;
            case RUNNING:
                System.out.println("Continuing to wait for " + job.key + "; state=" + state);
                break;
            default:
                throw new IllegalStateException("Unexpected state: " + state + " for " + job.key);
        }
    }
}
Also used : TypedPlatformLayerClient(org.platformlayer.TypedPlatformLayerClient) OpsException(org.platformlayer.ops.OpsException) JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) JobState(org.platformlayer.jobs.model.JobState) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) JobData(org.platformlayer.jobs.model.JobData) JobExecutionList(org.platformlayer.jobs.model.JobExecutionList)

Example 3 with JobExecutionList

use of org.platformlayer.jobs.model.JobExecutionList in project platformlayer by platformlayer.

the class GetJobLog method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException {
    PlatformLayerClient client = getPlatformLayerClient();
    if (jobId.contains("/") && executionId == null) {
        String[] tokens = jobId.split("/");
        if (tokens.length == 2) {
            jobId = tokens[0];
            executionId = tokens[1];
        }
    }
    List<JobLog> logs = Lists.newArrayList();
    if (Strings.isNullOrEmpty(executionId)) {
        JobExecutionList jobExecutions = client.listJobExecutions(jobId);
        List<JobExecutionData> runs = jobExecutions.getRuns();
        sort(runs);
        // TODO: Remove limit (or iterate)
        if (runs.size() > 10) {
            runs = Lists.newArrayList(runs.subList(runs.size() - 10, runs.size()));
        }
        // TODO: Fix 1+N slowness...
        for (JobExecutionData execution : runs) {
            if (execution.getState() == JobState.PRESTART) {
                continue;
            }
            String executionId = execution.getExecutionId();
            try {
                JobLog jobLog = client.getJobExecutionLog(jobId, executionId);
                logs.add(jobLog);
            } catch (PlatformLayerClientNotFoundException e) {
            // TODO: Warn?
            }
        }
    } else {
        JobLog jobLog = client.getJobExecutionLog(jobId, executionId);
        logs.add(jobLog);
    }
    return logs;
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) PlatformLayerClientNotFoundException(org.platformlayer.PlatformLayerClientNotFoundException) JobLog(org.platformlayer.jobs.model.JobLog) JobExecutionList(org.platformlayer.jobs.model.JobExecutionList)

Example 4 with JobExecutionList

use of org.platformlayer.jobs.model.JobExecutionList in project platformlayer by platformlayer.

the class HttpPlatformLayerClient method listJobExecutions.

@Override
public JobExecutionList listJobExecutions() throws PlatformLayerClientException {
    String relativePath = "jobs/runs";
    JobExecutionList jobs = doRequest(HttpMethod.GET, relativePath, JobExecutionList.class, Format.XML, null, null);
    return jobs;
}
Also used : JobExecutionList(org.platformlayer.jobs.model.JobExecutionList)

Example 5 with JobExecutionList

use of org.platformlayer.jobs.model.JobExecutionList in project platformlayer by platformlayer.

the class TailLog method runCommand.

@Override
public Object runCommand() throws PlatformLayerClientException, InterruptedException {
    PlatformLayerClient client = getPlatformLayerClient();
    // TODO: System.out isn't quite right
    JobLogPrinter jobLogPrinter = new JobLogPrinter(new PrintWriter(System.out));
    if (Strings.isNullOrEmpty(executionId)) {
        JobExecutionList jobExecutions = client.listJobExecutions(jobId);
        JobExecutionData last = null;
        for (JobExecutionData execution : jobExecutions.getRuns()) {
            if (execution.getState() == JobState.PRESTART) {
                continue;
            }
            if (last == null) {
                last = execution;
                continue;
            }
            if (last.getStartedAt().before(execution.getStartedAt())) {
                last = execution;
                continue;
            }
        }
        if (last != null) {
            executionId = last.getExecutionId();
        }
    }
    // TODO: What if executionId == null? Also retries..
    JobLog previousJobLog = null;
    int jobLogOffset = 0;
    while (true) {
        // TODO: Only fetch tail
        JobLog jobLog = client.getJobExecutionLog(jobId, executionId);
        if (previousJobLog == null) {
            jobLogPrinter.startJobLog(jobLog);
        }
        List<JobLogLine> lines = jobLog.getLines();
        if (jobLogOffset < lines.size()) {
            for (JobLogLine line : lines.subList(jobLogOffset, lines.size())) {
                jobLogPrinter.write(line);
            }
        }
        jobLogPrinter.flush();
        jobLogOffset = lines.size();
        previousJobLog = jobLog;
        Thread.sleep(1000);
    }
}
Also used : PlatformLayerClient(org.platformlayer.PlatformLayerClient) JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) JobLogLine(org.platformlayer.jobs.model.JobLogLine) JobLog(org.platformlayer.jobs.model.JobLog) JobExecutionList(org.platformlayer.jobs.model.JobExecutionList) PrintWriter(java.io.PrintWriter)

Aggregations

JobExecutionList (org.platformlayer.jobs.model.JobExecutionList)9 JobExecutionData (org.platformlayer.jobs.model.JobExecutionData)5 PlatformLayerClient (org.platformlayer.PlatformLayerClient)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 JobLog (org.platformlayer.jobs.model.JobLog)2 JobState (org.platformlayer.jobs.model.JobState)2 CliException (com.fathomdb.cli.CliException)1 Ansi (com.fathomdb.cli.commands.Ansi)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 PlatformLayerClientNotFoundException (org.platformlayer.PlatformLayerClientNotFoundException)1 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 JobData (org.platformlayer.jobs.model.JobData)1 JobLogLine (org.platformlayer.jobs.model.JobLogLine)1 OpsException (org.platformlayer.ops.OpsException)1 JobQuery (org.platformlayer.ops.tasks.JobQuery)1