Search in sources :

Example 1 with WorkflowStepData

use of edu.harvard.iq.dataverse.workflow.step.WorkflowStepData 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 WorkflowStepData

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

the class WorkflowServiceBean method rollback.

@Asynchronous
private void rollback(Workflow wf, WorkflowContext ctxt, Failure failure, int lastCompletedStepIdx) {
    ctxt = refresh(ctxt);
    final List<WorkflowStepData> steps = wf.getSteps();
    for (int stepIdx = lastCompletedStepIdx; stepIdx >= 0; --stepIdx) {
        WorkflowStepData wsd = steps.get(stepIdx);
        WorkflowStep step = createStep(wsd);
        try {
            logger.log(Level.INFO, "Workflow {0} step {1}: Rollback", new Object[] { ctxt.getInvocationId(), stepIdx });
            rollbackStep(step, ctxt, failure);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Workflow " + ctxt.getInvocationId() + " step " + stepIdx + ": Rollback error: " + e.getMessage(), e);
        }
    }
    logger.log(Level.INFO, "Removing workflow lock");
    try {
        engine.submit(new RemoveLockCommand(ctxt.getRequest(), ctxt.getDataset(), DatasetLock.Reason.Workflow));
        // Corner case - delete locks generated within this same transaction.
        Query deleteQuery = em.createQuery("DELETE from DatasetLock l WHERE l.dataset.id=:id AND l.reason=:reason");
        deleteQuery.setParameter("id", ctxt.getDataset().getId());
        deleteQuery.setParameter("reason", DatasetLock.Reason.Workflow);
        deleteQuery.executeUpdate();
    } catch (CommandException ex) {
        logger.log(Level.SEVERE, "Error restoring dataset locks state after rollback: " + ex.getMessage(), ex);
    }
}
Also used : Query(javax.persistence.Query) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) WorkflowStep(edu.harvard.iq.dataverse.workflow.step.WorkflowStep) RemoveLockCommand(edu.harvard.iq.dataverse.engine.command.impl.RemoveLockCommand) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) Asynchronous(javax.ejb.Asynchronous)

Example 3 with WorkflowStepData

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

the class JsonParser method parseStepData.

public WorkflowStepData parseStepData(JsonObject json) throws JsonParseException {
    WorkflowStepData wsd = new WorkflowStepData();
    validate("step", json, "provider", ValueType.STRING);
    validate("step", json, "stepType", ValueType.STRING);
    wsd.setProviderId(json.getString("provider"));
    wsd.setStepType(json.getString("stepType"));
    if (json.containsKey("parameters")) {
        JsonObject params = json.getJsonObject("parameters");
        Map<String, String> paramMap = new HashMap<>();
        params.keySet().forEach(k -> paramMap.put(k, jsonValueToString(params.get(k))));
        wsd.setStepParameters(paramMap);
    }
    return wsd;
}
Also used : WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString)

Example 4 with WorkflowStepData

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

the class JsonParser method parseWorkflow.

public Workflow parseWorkflow(JsonObject json) throws JsonParseException {
    Workflow retVal = new Workflow();
    validate("", json, "name", ValueType.STRING);
    validate("", json, "steps", ValueType.ARRAY);
    retVal.setName(json.getString("name"));
    JsonArray stepArray = json.getJsonArray("steps");
    List<WorkflowStepData> steps = new ArrayList<>(stepArray.size());
    for (JsonValue jv : stepArray) {
        steps.add(parseStepData((JsonObject) jv));
    }
    retVal.setSteps(steps);
    return retVal;
}
Also used : JsonArray(javax.json.JsonArray) WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) JsonObject(javax.json.JsonObject)

Example 5 with WorkflowStepData

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

the class JsonPrinter method json.

public static JsonObjectBuilder json(Workflow wf) {
    JsonObjectBuilder bld = jsonObjectBuilder();
    bld.add("name", wf.getName());
    if (wf.getId() != null) {
        bld.add("id", wf.getId());
    }
    if (wf.getSteps() != null && !wf.getSteps().isEmpty()) {
        JsonArrayBuilder arr = Json.createArrayBuilder();
        for (WorkflowStepData stp : wf.getSteps()) {
            arr.add(jsonObjectBuilder().add("stepType", stp.getStepType()).add("provider", stp.getProviderId()).add("parameters", mapToObject(stp.getStepParameters())));
        }
        bld.add("steps", arr);
    }
    return bld;
}
Also used : WorkflowStepData(edu.harvard.iq.dataverse.workflow.step.WorkflowStepData) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Aggregations

WorkflowStepData (edu.harvard.iq.dataverse.workflow.step.WorkflowStepData)6 WorkflowStep (edu.harvard.iq.dataverse.workflow.step.WorkflowStep)3 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 Failure (edu.harvard.iq.dataverse.workflow.step.Failure)2 Pending (edu.harvard.iq.dataverse.workflow.step.Pending)2 WorkflowStepResult (edu.harvard.iq.dataverse.workflow.step.WorkflowStepResult)2 JsonObject (javax.json.JsonObject)2 RemoveLockCommand (edu.harvard.iq.dataverse.engine.command.impl.RemoveLockCommand)1 Workflow (edu.harvard.iq.dataverse.workflow.Workflow)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Asynchronous (javax.ejb.Asynchronous)1 JsonArray (javax.json.JsonArray)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonString (javax.json.JsonString)1 JsonValue (javax.json.JsonValue)1 Query (javax.persistence.Query)1