Search in sources :

Example 16 with ExecutionResults

use of org.kie.api.executor.ExecutionResults in project jbpm by kiegroup.

the class AutoAckErrorCommand method execute.

@Override
public ExecutionResults execute(CommandContext ctx) throws Exception {
    ExecutionResults executionResults = new ExecutionResults();
    String emfName = (String) ctx.getData("EmfName");
    if (emfName == null) {
        emfName = "org.jbpm.domain";
    }
    String singleRun = (String) ctx.getData("SingleRun");
    if ("true".equalsIgnoreCase(singleRun)) {
        // disable rescheduling
        this.nextScheduleTimeAdd = -1;
    }
    String nextRun = (String) ctx.getData("NextRun");
    if (nextRun != null) {
        nextScheduleTimeAdd = DateTimeUtils.parseDateAsDuration(nextRun);
    }
    // get hold of persistence and create instance of audit service
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(emfName);
    EntityManager em = emf.createEntityManager();
    try {
        List<ExecutionErrorInfo> errorsToAck = findErrorsToAck(em);
        logger.debug("Found {} jobs that can be auto ack", errorsToAck.size());
        errorsToAck.forEach(error -> {
            AbstractRuntimeManager manager = (AbstractRuntimeManager) RuntimeManagerRegistry.get().getManager(error.getDeploymentId());
            if (manager != null) {
                ExecutionErrorManager errorManager = manager.getExecutionErrorManager();
                errorManager.getStorage().acknowledge("SYSTEM", error.getErrorId());
                logger.debug("Error {} has been auto acknowledged by system based on {}", error.getErrorId(), getAckRule());
            } else {
                logger.warn("Unable to ack error {} due missing runtime manager for '{}'", error.getErrorId(), error.getDeploymentId());
            }
        });
    } finally {
        em.close();
    }
    return executionResults;
}
Also used : EntityManager(javax.persistence.EntityManager) ExecutionErrorInfo(org.jbpm.runtime.manager.impl.jpa.ExecutionErrorInfo) ExecutionResults(org.kie.api.executor.ExecutionResults) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager)

Example 17 with ExecutionResults

use of org.kie.api.executor.ExecutionResults in project jbpm by kiegroup.

the class LogCleanupCommand method execute.

@Override
public ExecutionResults execute(CommandContext ctx) throws Exception {
    boolean skipProcessLog = ctx.getData().containsKey("SkipProcessLog") ? Boolean.parseBoolean((String) ctx.getData("SkipProcessLog")) : false;
    boolean skipTaskLog = ctx.getData().containsKey("SkipTaskLog") ? Boolean.parseBoolean((String) ctx.getData("SkipTaskLog")) : false;
    ;
    boolean skipExecutorLog = ctx.getData().containsKey("SkipExecutorLog") ? Boolean.parseBoolean((String) ctx.getData("SkipExecutorLog")) : false;
    ;
    SimpleDateFormat formatToUse = DATE_FORMAT;
    String dataFormat = (String) ctx.getData("DateFormat");
    if (dataFormat != null) {
        formatToUse = new SimpleDateFormat(dataFormat);
    }
    ExecutionResults executionResults = new ExecutionResults();
    String emfName = (String) ctx.getData("EmfName");
    if (emfName == null) {
        emfName = "org.jbpm.domain";
    }
    String singleRun = (String) ctx.getData("SingleRun");
    if ("true".equalsIgnoreCase(singleRun)) {
        // disable rescheduling
        this.nextScheduleTimeAdd = -1;
    }
    String nextRun = (String) ctx.getData("NextRun");
    if (nextRun != null) {
        nextScheduleTimeAdd = DateTimeUtils.parseDateAsDuration(nextRun);
    }
    // get hold of persistence and create instance of audit service
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(emfName);
    ExecutorJPAAuditService auditLogService = new ExecutorJPAAuditService(emf);
    // collect parameters
    String olderThan = (String) ctx.getData("OlderThan");
    String olderThanPeriod = (String) ctx.getData("OlderThanPeriod");
    String forProcess = (String) ctx.getData("ForProcess");
    String forDeployment = (String) ctx.getData("ForDeployment");
    if (olderThanPeriod != null) {
        long olderThanDuration = DateTimeUtils.parseDateAsDuration(olderThanPeriod);
        Date olderThanDate = new Date(System.currentTimeMillis() - olderThanDuration);
        olderThan = formatToUse.format(olderThanDate);
    }
    if (!skipTaskLog) {
        // task tables
        long taLogsRemoved = 0l;
        taLogsRemoved = auditLogService.auditTaskDelete().processId(forProcess).dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).deploymentId(forDeployment).build().execute();
        logger.info("TaskAuditLogRemoved {}", taLogsRemoved);
        executionResults.setData("TaskAuditLogRemoved", taLogsRemoved);
        long teLogsRemoved = 0l;
        teLogsRemoved = auditLogService.taskEventInstanceLogDelete().dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).build().execute();
        logger.info("TaskEventLogRemoved {}", teLogsRemoved);
        executionResults.setData("TaskEventLogRemoved", teLogsRemoved);
    }
    if (!skipProcessLog) {
        // process tables
        long niLogsRemoved = 0l;
        niLogsRemoved = auditLogService.nodeInstanceLogDelete().processId(forProcess).dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).externalId(forDeployment).build().execute();
        logger.info("NodeInstanceLogRemoved {}", niLogsRemoved);
        executionResults.setData("NodeInstanceLogRemoved", niLogsRemoved);
        long viLogsRemoved = 0l;
        viLogsRemoved = auditLogService.variableInstanceLogDelete().processId(forProcess).dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).externalId(forDeployment).build().execute();
        logger.info("VariableInstanceLogRemoved {}", viLogsRemoved);
        executionResults.setData("VariableInstanceLogRemoved", viLogsRemoved);
        long piLogsRemoved = 0l;
        piLogsRemoved = auditLogService.processInstanceLogDelete().processId(forProcess).status(ProcessInstance.STATE_COMPLETED, ProcessInstance.STATE_ABORTED).endDateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).externalId(forDeployment).build().execute();
        logger.info("ProcessInstanceLogRemoved {}", piLogsRemoved);
        executionResults.setData("ProcessInstanceLogRemoved", piLogsRemoved);
    }
    if (!skipExecutorLog) {
        // executor tables
        long errorInfoLogsRemoved = 0l;
        errorInfoLogsRemoved = auditLogService.errorInfoLogDeleteBuilder().dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).build().execute();
        logger.info("ErrorInfoLogsRemoved {}", errorInfoLogsRemoved);
        executionResults.setData("ErrorInfoLogsRemoved", errorInfoLogsRemoved);
        long requestInfoLogsRemoved = 0l;
        requestInfoLogsRemoved = auditLogService.requestInfoLogDeleteBuilder().dateRangeEnd(olderThan == null ? null : formatToUse.parse(olderThan)).status(STATUS.CANCELLED, STATUS.DONE, STATUS.ERROR).build().execute();
        logger.info("RequestInfoLogsRemoved {}", requestInfoLogsRemoved);
        executionResults.setData("RequestInfoLogsRemoved", requestInfoLogsRemoved);
    }
    // bam tables
    long bamLogsRemoved = 0l;
    executionResults.setData("BAMLogRemoved", bamLogsRemoved);
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults) EntityManagerFactory(javax.persistence.EntityManagerFactory) ExecutorJPAAuditService(org.jbpm.executor.impl.jpa.ExecutorJPAAuditService) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 18 with ExecutionResults

use of org.kie.api.executor.ExecutionResults in project jbpm by kiegroup.

the class PrintOutCommand method execute.

public ExecutionResults execute(CommandContext ctx) {
    logger.info("Command executed on executor with data {}", ctx.getData());
    ExecutionResults executionResults = new ExecutionResults();
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults)

Example 19 with ExecutionResults

use of org.kie.api.executor.ExecutionResults in project jbpm by kiegroup.

the class BasicExecutorIntegrationTest method addAnotherCallbackTest.

@Test
public void addAnotherCallbackTest() throws InterruptedException {
    CommandContext commandContext = new CommandContext();
    commandContext.setData("businessKey", UUID.randomUUID().toString());
    cachedEntities.put((String) commandContext.getData("businessKey"), new AtomicLong(1));
    commandContext.setData("callbacks", "org.jbpm.executor.ejb.impl.test.SimpleIncrementCallback");
    executorService.scheduleRequest("org.jbpm.executor.ejb.impl.test.AddAnotherCallbackCommand", commandContext);
    Thread.sleep(10000);
    List<RequestInfo> inErrorRequests = executorService.getInErrorRequests(new QueryContext());
    assertEquals(0, inErrorRequests.size());
    List<RequestInfo> queuedRequests = executorService.getQueuedRequests(new QueryContext());
    assertEquals(0, queuedRequests.size());
    List<RequestInfo> executedRequests = executorService.getCompletedRequests(new QueryContext());
    assertEquals(1, executedRequests.size());
    assertEquals(2, ((AtomicLong) cachedEntities.get((String) commandContext.getData("businessKey"))).longValue());
    ExecutionResults results = null;
    byte[] responseData = executedRequests.get(0).getResponseData();
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new ByteArrayInputStream(responseData));
        results = (ExecutionResults) in.readObject();
    } catch (Exception e) {
        logger.warn("Exception while serializing context data", e);
        return;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String result = (String) results.getData("custom");
    assertNotNull(result);
    assertEquals("custom callback invoked", result);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CommandContext(org.kie.api.executor.CommandContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionResults(org.kie.api.executor.ExecutionResults) QueryContext(org.kie.api.runtime.query.QueryContext) IOException(java.io.IOException) RequestInfo(org.kie.api.executor.RequestInfo) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 20 with ExecutionResults

use of org.kie.api.executor.ExecutionResults in project jbpm by kiegroup.

the class UserCommandWithCallback method execute.

public ExecutionResults execute(CommandContext ctx) {
    logger.debug("Command executed on executor with {}", ctx.getData());
    WorkItem workItem = (WorkItem) ctx.getData("workItem");
    User user = (User) workItem.getParameter("UserIn");
    user.setName(user.getName() + " after command execution");
    ExecutionResults executionResults = new ExecutionResults();
    executionResults.setData("UserOut", user);
    String callbacks = (String) ctx.getData("callbacks");
    ctx.setData("callbacks", callbacks + ", org.jbpm.test.jobexec.UserCommandCallback");
    double item = 0;
    for (int i = 0; i < 99; i++) {
        logger.debug("User item: {}", item);
        item++;
    }
    return executionResults;
}
Also used : ExecutionResults(org.kie.api.executor.ExecutionResults) WorkItem(org.kie.api.runtime.process.WorkItem)

Aggregations

ExecutionResults (org.kie.api.executor.ExecutionResults)23 Date (java.util.Date)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)4 Test (org.junit.Test)4 WorkItem (org.kie.api.runtime.process.WorkItem)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ObjectInputStream (java.io.ObjectInputStream)3 HashMap (java.util.HashMap)3 CommandContext (org.kie.api.executor.CommandContext)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)3 QueryContext (org.kie.api.runtime.query.QueryContext)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Map (java.util.Map)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)2 TransactionalCommandService (org.jbpm.shared.services.impl.TransactionalCommandService)2 ExecutorService (org.kie.api.executor.ExecutorService)2