Search in sources :

Example 1 with WorkStatusCode

use of org.batfish.common.CoordConsts.WorkStatusCode in project batfish by batfish.

the class Client method printWorkStatusResponse.

private void printWorkStatusResponse(Pair<WorkStatusCode, String> response, boolean unconditionalPrint) {
    if (unconditionalPrint || _logger.getLogLevel() >= BatfishLogger.LEVEL_INFO) {
        WorkStatusCode status = response.getFirst();
        _logger.outputf("status: %s\n", status);
        Task task;
        try {
            task = BatfishObjectMapper.mapper().readValue(response.getSecond(), Task.class);
        } catch (IOException e) {
            _logger.errorf("Could not deserialize task object: %s\n", e);
            return;
        }
        if (task == null) {
            _logger.outputf(".... no task information\n");
            return;
        }
        List<Batch> batches = task.getBatches();
        // else print all
        for (int i = 0; i < batches.size(); i++) {
            if (i == batches.size() - 1 || status.isTerminated()) {
                _logger.outputf(".... %s\n", batches.get(i));
            } else {
                _logger.debugf(".... %s\n", batches.get(i));
            }
        }
        if (status.isTerminated()) {
            _logger.outputf(".... %s: %s\n", task.getTerminated(), status);
        }
    }
}
Also used : Task(org.batfish.common.Task) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) Batch(org.batfish.common.Task.Batch) IOException(java.io.IOException)

Example 2 with WorkStatusCode

use of org.batfish.common.CoordConsts.WorkStatusCode in project batfish by batfish.

the class Client method pollWork.

private boolean pollWork(UUID wItemId, @Nullable FileWriter outWriter) {
    Pair<WorkStatusCode, String> response;
    try (ActiveSpan workStatusSpan = GlobalTracer.get().buildSpan("Waiting for work status").startActive()) {
        // avoid unused warning
        assert workStatusSpan != null;
        // Poll the work item until it finishes or fails.
        response = _workHelper.getWorkStatus(wItemId);
        if (response == null) {
            return false;
        }
        WorkStatusCode status = response.getFirst();
        Backoff backoff = Backoff.builder().withMaximumBackoff(Duration.ofSeconds(1)).build();
        while (!status.isTerminated() && backoff.hasNext()) {
            printWorkStatusResponse(response, false);
            try {
                Thread.sleep(backoff.nextBackoff().toMillis());
            } catch (InterruptedException e) {
                throw new BatfishException("Interrupted while waiting for work item to complete", e);
            }
            response = _workHelper.getWorkStatus(wItemId);
            if (response == null) {
                return false;
            }
            status = response.getFirst();
        }
        printWorkStatusResponse(response, false);
    }
    return true;
}
Also used : BatfishException(org.batfish.common.BatfishException) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) ActiveSpan(io.opentracing.ActiveSpan) Backoff(org.batfish.common.util.Backoff)

Example 3 with WorkStatusCode

use of org.batfish.common.CoordConsts.WorkStatusCode in project batfish by batfish.

the class Client method getWorkStatus.

private boolean getWorkStatus(@Nullable FileWriter outWriter, List<String> options, List<String> parameters) {
    if (!isValidArgument(options, parameters, 0, 1, 1, Command.GET_WORK_STATUS)) {
        return false;
    }
    UUID workId = UUID.fromString(parameters.get(0));
    Pair<WorkStatusCode, String> response = _workHelper.getWorkStatus(workId);
    if (response == null) {
        return false;
    }
    printWorkStatusResponse(response, true);
    logOutput(outWriter, "status: " + response.getFirst());
    return true;
}
Also used : WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) UUID(java.util.UUID)

Example 4 with WorkStatusCode

use of org.batfish.common.CoordConsts.WorkStatusCode in project batfish by batfish.

the class BfCoordWorkHelper method getWorkStatus.

@Nullable
public Pair<WorkStatusCode, String> getWorkStatus(UUID workId) {
    try {
        WebTarget webTarget = getTarget(CoordConsts.SVC_RSC_GET_WORKSTATUS);
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_API_KEY, _settings.getApiKey());
        addTextMultiPart(multiPart, CoordConsts.SVC_KEY_WORKID, workId.toString());
        JSONObject jObj = postData(webTarget, multiPart);
        if (jObj == null) {
            return null;
        }
        if (!jObj.has(CoordConsts.SVC_KEY_WORKSTATUS)) {
            _logger.errorf("workstatus key not found in: %s\n", jObj);
            return null;
        }
        WorkStatusCode workStatus = WorkStatusCode.valueOf(jObj.getString(CoordConsts.SVC_KEY_WORKSTATUS));
        if (!jObj.has(CoordConsts.SVC_KEY_TASKSTATUS)) {
            _logger.errorf("taskstatus key not found in: %s\n", jObj);
        }
        String taskStr = jObj.getString(CoordConsts.SVC_KEY_TASKSTATUS);
        return new Pair<>(workStatus, taskStr);
    } catch (Exception e) {
        _logger.errorf("exception: ");
        _logger.error(ExceptionUtils.getStackTrace(e) + "\n");
        return null;
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) JSONObject(org.codehaus.jettison.json.JSONObject) WorkStatusCode(org.batfish.common.CoordConsts.WorkStatusCode) WebTarget(javax.ws.rs.client.WebTarget) BatfishException(org.batfish.common.BatfishException) ProcessingException(javax.ws.rs.ProcessingException) Pair(org.batfish.common.Pair) Nullable(javax.annotation.Nullable)

Aggregations

WorkStatusCode (org.batfish.common.CoordConsts.WorkStatusCode)4 BatfishException (org.batfish.common.BatfishException)2 ActiveSpan (io.opentracing.ActiveSpan)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebTarget (javax.ws.rs.client.WebTarget)1 Pair (org.batfish.common.Pair)1 Task (org.batfish.common.Task)1 Batch (org.batfish.common.Task.Batch)1 Backoff (org.batfish.common.util.Backoff)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)1