Search in sources :

Example 1 with JobState

use of org.platformlayer.jobs.model.JobState 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 2 with JobState

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

the class ListJobExecutions method formatRaw.

@Override
public void formatRaw(Object o, PrintWriter writer) {
    JobExecutionList jobs = (JobExecutionList) o;
    switch(getFormat()) {
        case JSON:
            JsonHelper<JobExecutionList> jsonHelper = JsonHelper.build(JobExecutionList.class);
            boolean formatted = true;
            try {
                String json = jsonHelper.marshal(jobs, formatted);
                writer.println(json);
                return;
            } catch (IOException e) {
                throw new CliException("Error formatting for output", e);
            }
    }
    Ansi ansi = new Ansi(writer);
    for (JobExecutionData job : jobs) {
        JobState state = job.state;
        if (state != null) {
            switch(job.state) {
                case FAILED:
                    ansi.setColorRed();
                    break;
                case SUCCESS:
                    ansi.setColorGreen();
                    break;
                case RUNNING:
                    ansi.setColorBlue();
                    break;
                default:
                    ansi.setColorBlue();
                    break;
            }
        } else {
            ansi.setColorBlue();
        }
        writer.println(job.getJobId() + "/" + job.executionId);
    }
    ansi.reset();
}
Also used : CliException(com.fathomdb.cli.CliException) JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) JobState(org.platformlayer.jobs.model.JobState) IOException(java.io.IOException) JobExecutionList(org.platformlayer.jobs.model.JobExecutionList) Ansi(com.fathomdb.cli.commands.Ansi)

Aggregations

JobExecutionData (org.platformlayer.jobs.model.JobExecutionData)2 JobExecutionList (org.platformlayer.jobs.model.JobExecutionList)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 TypedPlatformLayerClient (org.platformlayer.TypedPlatformLayerClient)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 JobData (org.platformlayer.jobs.model.JobData)1 OpsException (org.platformlayer.ops.OpsException)1