Search in sources :

Example 26 with ProcessInstanceLog

use of org.jbpm.process.audit.ProcessInstanceLog in project jbpm by kiegroup.

the class SLATrackingCommand 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);
    // collect parameters
    String forDeployment = (String) ctx.getData("ForDeployment");
    // SLA Violations on nodes
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("now", new Date());
    StringBuilder lookupQuery = new StringBuilder();
    lookupQuery.append("select log from NodeInstanceLog log where ");
    lookupQuery.append("log.nodeInstanceId in ( select nil.nodeInstanceId from NodeInstanceLog nil where nil.slaDueDate < :now and nil.slaCompliance = 1 ");
    lookupQuery.append("GROUP BY nil.nodeInstanceId ");
    lookupQuery.append("HAVING sum(nil.type) = 0) ");
    lookupQuery.append("and log.type = 0 ");
    if (forDeployment != null && !forDeployment.isEmpty()) {
        lookupQuery.append(" and log.externalId = :forDeployment");
        parameters.put("forDeployment", forDeployment);
    }
    TransactionalCommandService commandService = new TransactionalCommandService(emf);
    List<NodeInstanceLog> nodeInstancesViolations = commandService.execute(new QueryStringCommand<List<NodeInstanceLog>>(lookupQuery.toString(), parameters));
    logger.debug("Number of node instances with violated SLA {}", nodeInstancesViolations.size());
    if (!nodeInstancesViolations.isEmpty()) {
        logger.debug("Signaling process instances that have SLA violations on nodes");
        int nodeSignals = 0;
        for (NodeInstanceLog niLog : nodeInstancesViolations) {
            RuntimeManager runtimeManager = RuntimeManagerRegistry.get().getManager(niLog.getExternalId());
            if (runtimeManager == null) {
                logger.debug("No runtime manager found for {}, not able to send SLA violation signal", niLog.getExternalId());
                continue;
            }
            RuntimeEngine engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(niLog.getProcessInstanceId()));
            try {
                engine.getKieSession().signalEvent("slaViolation:" + niLog.getNodeInstanceId(), null, niLog.getProcessInstanceId());
                nodeSignals++;
            } catch (Exception e) {
                logger.warn("Unexpected error when signalig process instance {} about SLA violation {}", niLog.getProcessInstanceId(), e.getMessage(), e);
            } finally {
                runtimeManager.disposeRuntimeEngine(engine);
            }
        }
        logger.info("SLA Violations JOB :: Number of nodes successfully signaled is {}", nodeSignals);
        executionResults.setData("NodeSLASignals", nodeSignals);
    }
    // SLA Violations on process instances
    parameters = new HashMap<>();
    parameters.put("now", new Date());
    lookupQuery = new StringBuilder();
    lookupQuery.append("select log from ProcessInstanceLog log where log.slaDueDate < :now and log.slaCompliance = 1 ");
    if (forDeployment != null && !forDeployment.isEmpty()) {
        lookupQuery.append(" and log.externalId = :forDeployment");
        parameters.put("forDeployment", forDeployment);
    }
    List<ProcessInstanceLog> processInstancesViolations = commandService.execute(new QueryStringCommand<List<ProcessInstanceLog>>(lookupQuery.toString(), parameters));
    logger.debug("Number of node instances with violated SLA {}", nodeInstancesViolations.size());
    if (!processInstancesViolations.isEmpty()) {
        logger.debug("Signaling process instances that have SLA violations");
        int processSignals = 0;
        for (ProcessInstanceLog piLog : processInstancesViolations) {
            RuntimeManager runtimeManager = RuntimeManagerRegistry.get().getManager(piLog.getExternalId());
            if (runtimeManager == null) {
                logger.debug("No runtime manager found for {}, not able to send SLA violation signal", piLog.getExternalId());
                continue;
            }
            RuntimeEngine engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(piLog.getProcessInstanceId()));
            try {
                engine.getKieSession().signalEvent("slaViolation", null, piLog.getProcessInstanceId());
                processSignals++;
            } catch (Exception e) {
                logger.warn("Unexpected error when signalig process instance {} about SLA violation {}", piLog.getProcessInstanceId(), e.getMessage(), e);
            } finally {
                runtimeManager.disposeRuntimeEngine(engine);
            }
        }
        logger.info("SLA Violations JOB :: Number of process instances successfully signaled is {}", processSignals);
        executionResults.setData("ProcessSLASignals", processSignals);
    }
    return executionResults;
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) NodeInstanceLog(org.jbpm.process.audit.NodeInstanceLog) HashMap(java.util.HashMap) ExecutionResults(org.kie.api.executor.ExecutionResults) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) Date(java.util.Date) EntityManagerFactory(javax.persistence.EntityManagerFactory) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) List(java.util.List) ProcessInstanceLog(org.jbpm.process.audit.ProcessInstanceLog)

Example 27 with ProcessInstanceLog

use of org.jbpm.process.audit.ProcessInstanceLog in project jbpm by kiegroup.

the class MigrationManager method validate.

private void validate() {
    if (migrationSpec == null) {
        report.addEntry(Type.ERROR, "no process data given for migration");
        return;
    }
    // source (active) process instance information
    if (isEmpty(migrationSpec.getDeploymentId())) {
        report.addEntry(Type.ERROR, "No deployment id set");
    }
    if (migrationSpec.getProcessInstanceId() == null) {
        report.addEntry(Type.ERROR, "No process instance id set");
    }
    // target process information
    if (isEmpty(migrationSpec.getToDeploymentId())) {
        report.addEntry(Type.ERROR, "No target deployment id set");
    }
    if (isEmpty(migrationSpec.getToProcessId())) {
        report.addEntry(Type.ERROR, "No target process id set");
    }
    // verify if given runtime manager exists - registered under source deployment id
    if (!RuntimeManagerRegistry.get().isRegistered(migrationSpec.getDeploymentId())) {
        report.addEntry(Type.ERROR, "No deployment found for " + migrationSpec.getDeploymentId());
    }
    // verify if given runtime manager exists - registered under target deployment id
    if (!RuntimeManagerRegistry.get().isRegistered(migrationSpec.getToDeploymentId())) {
        report.addEntry(Type.ERROR, "No target deployment found for " + migrationSpec.getToDeploymentId());
    }
    // verify if given target process id exists in target runtime manager
    InternalRuntimeManager manager = (InternalRuntimeManager) RuntimeManagerRegistry.get().getManager(migrationSpec.getToDeploymentId());
    if (manager.getEnvironment().getKieBase().getProcess(migrationSpec.getToProcessId()) == null) {
        report.addEntry(Type.ERROR, "No process found for " + migrationSpec.getToProcessId() + " in deployment " + migrationSpec.getToDeploymentId());
    }
    // verify that source and target runtime manager is of the same type - represent the same runtime strategy
    InternalRuntimeManager sourceManager = (InternalRuntimeManager) RuntimeManagerRegistry.get().getManager(migrationSpec.getDeploymentId());
    if (!sourceManager.getClass().isAssignableFrom(manager.getClass())) {
        report.addEntry(Type.ERROR, "Source (" + sourceManager.getClass().getName() + ") and target (" + manager.getClass().getName() + ") deployments are of different type (they represent different runtime strategies)");
    }
    String auditPu = manager.getDeploymentDescriptor().getAuditPersistenceUnit();
    EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(auditPu);
    JPAAuditLogService auditService = new JPAAuditLogService(emf);
    try {
        ProcessInstanceLog log = auditService.findProcessInstance(migrationSpec.getProcessInstanceId());
        if (log == null || log.getStatus() != ProcessInstance.STATE_ACTIVE) {
            report.addEntry(Type.ERROR, "No process instance found or it is not active (id " + migrationSpec.getProcessInstanceId() + " in status " + (log == null ? "-1" : log.getStatus()));
        }
    } finally {
        auditService.dispose();
    }
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) ProcessInstanceLog(org.jbpm.process.audit.ProcessInstanceLog)

Aggregations

ProcessInstanceLog (org.jbpm.process.audit.ProcessInstanceLog)27 Test (org.junit.Test)9 NodeInstanceLog (org.jbpm.process.audit.NodeInstanceLog)8 KieBase (org.kie.api.KieBase)8 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)8 JPAAuditLogService (org.jbpm.process.audit.JPAAuditLogService)7 HashMap (java.util.HashMap)6 EntityManager (javax.persistence.EntityManager)6 EntityManagerFactory (javax.persistence.EntityManagerFactory)6 AuditLogService (org.jbpm.process.audit.AuditLogService)6 KieSession (org.kie.api.runtime.KieSession)6 Calendar (java.util.Calendar)5 GregorianCalendar (java.util.GregorianCalendar)5 StandaloneJtaStrategy (org.jbpm.process.audit.strategy.StandaloneJtaStrategy)5 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)5 PersistenceUtil.createEnvironment (org.jbpm.persistence.util.PersistenceUtil.createEnvironment)4 AbstractAuditLogServiceTest.createKieSession (org.jbpm.process.audit.AbstractAuditLogServiceTest.createKieSession)4 AbstractAuditLogger (org.jbpm.process.audit.AbstractAuditLogger)4 Environment (org.kie.api.runtime.Environment)4 RequirePersistence (org.jbpm.bpmn2.test.RequirePersistence)3