Search in sources :

Example 6 with JobExecutionData

use of org.platformlayer.jobs.model.JobExecutionData 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)

Example 7 with JobExecutionData

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

use of org.platformlayer.jobs.model.JobExecutionData 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)

Example 9 with JobExecutionData

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

the class JobDataFormatter method visit.

@Override
public void visit(CliContext context, JobData o, OutputSink sink) throws IOException {
    LinkedHashMap<String, Object> values = Maps.newLinkedHashMap();
    values.put("id", o.key.getItemIdString());
    values.put("target", o.targetId);
    values.put("action", o.action.getType());
    // values.put("state", o.state);
    JobExecutionData lastRun = o.getLastRun();
    values.put("lastrun:id", lastRun != null ? lastRun.getExecutionId() : null);
    values.put("lastrun:state", lastRun != null ? lastRun.getState() : null);
    values.put("lastrun:end", lastRun != null ? lastRun.getEndedAt() : null);
    sink.outputRow(values);
}
Also used : JobExecutionData(org.platformlayer.jobs.model.JobExecutionData)

Example 10 with JobExecutionData

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

the class JobLogFormatter method visit.

@Override
public void visit(CliContext context, JobLog o, OutputSink sink) throws IOException {
    LinkedHashMap<String, Object> values = Maps.newLinkedHashMap();
    JobExecutionData execution = o.getExecution();
    if (execution != null) {
        values.put("jobId", execution.getJobKey().getItemIdString());
        values.put("executionId", execution.getExecutionId());
        values.put("start", o.getExecution().getStartedAt());
        values.put("end", o.getExecution().getEndedAt());
        values.put("state", execution.getState());
    }
    List<JobLogLine> lines = o.getLines();
    values.put("lines", lines.size());
    sink.outputRow(values);
}
Also used : JobExecutionData(org.platformlayer.jobs.model.JobExecutionData) JobLogLine(org.platformlayer.jobs.model.JobLogLine)

Aggregations

JobExecutionData (org.platformlayer.jobs.model.JobExecutionData)12 JobExecutionList (org.platformlayer.jobs.model.JobExecutionList)5 JobLog (org.platformlayer.jobs.model.JobLog)3 OpsException (org.platformlayer.ops.OpsException)3 IOException (java.io.IOException)2 Date (java.util.Date)2 PlatformLayerClient (org.platformlayer.PlatformLayerClient)2 RepositoryException (org.platformlayer.RepositoryException)2 JobData (org.platformlayer.jobs.model.JobData)2 JobLogLine (org.platformlayer.jobs.model.JobLogLine)2 JobState (org.platformlayer.jobs.model.JobState)2 CliException (com.fathomdb.cli.CliException)1 Ansi (com.fathomdb.cli.commands.Ansi)1 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)1 PrintWriter (java.io.PrintWriter)1 SQLException (java.sql.SQLException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 PlatformLayerClientNotFoundException (org.platformlayer.PlatformLayerClientNotFoundException)1