Search in sources :

Example 1 with Failure

use of edu.harvard.iq.dataverse.workflow.step.Failure in project dataverse by IQSS.

the class WorkflowServiceBean method executeSteps.

/**
 * Execute the passed workflow, starting from {@code initialStepIdx}.
 * @param wf    The workflow to run.
 * @param ctxt  Execution context to run the workflow in.
 * @param initialStepIdx 0-based index of the first step to run.
 */
private void executeSteps(Workflow wf, WorkflowContext ctxt, int initialStepIdx) {
    final List<WorkflowStepData> steps = wf.getSteps();
    for (int stepIdx = initialStepIdx; stepIdx < steps.size(); stepIdx++) {
        WorkflowStepData wsd = steps.get(stepIdx);
        WorkflowStep step = createStep(wsd);
        WorkflowStepResult res = runStep(step, ctxt);
        try {
            if (res == WorkflowStepResult.OK) {
                logger.log(Level.INFO, "Workflow {0} step {1}: OK", new Object[] { ctxt.getInvocationId(), stepIdx });
            } else if (res instanceof Failure) {
                logger.log(Level.WARNING, "Workflow {0} failed: {1}", new Object[] { ctxt.getInvocationId(), ((Failure) res).getReason() });
                rollback(wf, ctxt, (Failure) res, stepIdx - 1);
                return;
            } else if (res instanceof Pending) {
                pauseAndAwait(wf, ctxt, (Pending) res, stepIdx);
                return;
            }
        } catch (Exception e) {
            logger.log(Level.WARNING, "Workflow {0} step {1}: Uncought exception:", new Object[] { ctxt.getInvocationId(), e.getMessage() });
            logger.log(Level.WARNING, "Trace:", e);
            rollback(wf, ctxt, (Failure) res, stepIdx - 1);
            return;
        }
    }
    workflowCompleted(wf, ctxt);
}
Also used : WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) WorkflowStep(edu.harvard.iq.dataverse.workflow.step.WorkflowStep) WorkflowStepResult(edu.harvard.iq.dataverse.workflow.step.WorkflowStepResult) Failure(edu.harvard.iq.dataverse.workflow.step.Failure) Pending(edu.harvard.iq.dataverse.workflow.step.Pending) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException)

Example 2 with Failure

use of edu.harvard.iq.dataverse.workflow.step.Failure in project dataverse by IQSS.

the class HttpSendReceiveClientStep method resume.

@Override
public WorkflowStepResult resume(WorkflowContext context, Map<String, String> internalData, String externalData) {
    Pattern pat = Pattern.compile(params.get("expectedResponse"));
    String response = externalData.trim();
    if (pat.matcher(response).matches()) {
        return OK;
    } else {
        logger.log(Level.WARNING, "Remote system returned a bad reposonse: {0}", externalData);
        return new Failure("Response from remote server did not match expected one (response:" + response + ")");
    }
}
Also used : Pattern(java.util.regex.Pattern) Failure(edu.harvard.iq.dataverse.workflow.step.Failure)

Example 3 with Failure

use of edu.harvard.iq.dataverse.workflow.step.Failure in project dataverse by IQSS.

the class PauseStep method resume.

@Override
public WorkflowStepResult resume(WorkflowContext context, Map<String, String> internalData, String externalData) {
    logger.log(Level.INFO, "local parameters match: {0}", internalData.equals(params));
    logger.log(Level.INFO, "externalData: \"{0}\"", externalData);
    return externalData.trim().equals(FAILURE_RESPONSE) ? new Failure("Simulated fail") : WorkflowStepResult.OK;
}
Also used : Failure(edu.harvard.iq.dataverse.workflow.step.Failure)

Example 4 with Failure

use of edu.harvard.iq.dataverse.workflow.step.Failure in project dataverse by IQSS.

the class WorkflowServiceBean method doResume.

private void doResume(PendingWorkflowInvocation pending, String body) {
    Workflow wf = pending.getWorkflow();
    List<WorkflowStepData> stepsLeft = wf.getSteps().subList(pending.getPendingStepIdx(), wf.getSteps().size());
    WorkflowStep pendingStep = createStep(stepsLeft.get(0));
    final WorkflowContext ctxt = pending.reCreateContext(roleAssignees);
    WorkflowStepResult res = pendingStep.resume(ctxt, pending.getLocalData(), body);
    if (res instanceof Failure) {
        rollback(wf, ctxt, (Failure) res, pending.getPendingStepIdx() - 1);
    } else if (res instanceof Pending) {
        pauseAndAwait(wf, ctxt, (Pending) res, pending.getPendingStepIdx());
    } else {
        executeSteps(wf, ctxt, pending.getPendingStepIdx() + 1);
    }
}
Also used : WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) WorkflowStep(edu.harvard.iq.dataverse.workflow.step.WorkflowStep) WorkflowStepResult(edu.harvard.iq.dataverse.workflow.step.WorkflowStepResult) Failure(edu.harvard.iq.dataverse.workflow.step.Failure) Pending(edu.harvard.iq.dataverse.workflow.step.Pending)

Example 5 with Failure

use of edu.harvard.iq.dataverse.workflow.step.Failure in project dataverse by IQSS.

the class HttpSendReceiveClientStep method run.

@Override
public WorkflowStepResult run(WorkflowContext context) {
    HttpClient client = new HttpClient();
    try {
        // build method
        HttpMethodBase mtd = buildMethod(false, context);
        // execute
        int responseStatus = client.executeMethod(mtd);
        if (responseStatus >= 200 && responseStatus < 300) {
            // HTTP OK range
            return new Pending();
        } else {
            String responseBody = mtd.getResponseBodyAsString();
            return new Failure("Error communicating with server. Server response: " + responseBody + " (" + responseStatus + ").");
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Error communicating with remote server: " + ex.getMessage(), ex);
        return new Failure("Error executing request: " + ex.getLocalizedMessage(), "Cannot communicate with remote server.");
    }
}
Also used : HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) HttpClient(org.apache.commons.httpclient.HttpClient) Pending(edu.harvard.iq.dataverse.workflow.step.Pending) Failure(edu.harvard.iq.dataverse.workflow.step.Failure) URIException(org.apache.commons.httpclient.URIException)

Aggregations

Failure (edu.harvard.iq.dataverse.workflow.step.Failure)5 Pending (edu.harvard.iq.dataverse.workflow.step.Pending)3 WorkflowStep (edu.harvard.iq.dataverse.workflow.step.WorkflowStep)2 WorkflowStepData (edu.harvard.iq.dataverse.workflow.step.WorkflowStepData)2 WorkflowStepResult (edu.harvard.iq.dataverse.workflow.step.WorkflowStepResult)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 Pattern (java.util.regex.Pattern)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpMethodBase (org.apache.commons.httpclient.HttpMethodBase)1 URIException (org.apache.commons.httpclient.URIException)1