Search in sources :

Example 1 with ProcessEventRequest

use of com.walmartlabs.concord.client.ProcessEventRequest in project concord by walmartlabs.

the class DefaultElementEventProcessor method process.

@Override
public void process(ElementEvent event, EventParamsBuilder builder, Predicate<AbstractElement> filter) throws ExecutionException {
    ProcessDefinition pd = processDefinitionProvider.getById(event.getProcessDefinitionId());
    if (pd == null) {
        throw new RuntimeException("Process definition not found: " + event.getProcessDefinitionId());
    }
    if (!(pd instanceof SourceAwareProcessDefinition)) {
        return;
    }
    Map<String, SourceMap> sourceMaps = ((SourceAwareProcessDefinition) pd).getSourceMaps();
    SourceMap source = sourceMaps.get(event.getElementId());
    if (source == null) {
        return;
    }
    AbstractElement element = ProcessDefinitionUtils.findElement(pd, event.getElementId());
    if (filter != null && !filter.test(element)) {
        return;
    }
    try {
        Map<String, Object> e = new HashMap<>();
        e.put("processDefinitionId", event.getProcessDefinitionId());
        e.put("elementId", event.getElementId());
        e.put("line", source.getLine());
        e.put("column", source.getColumn());
        e.put("description", source.getDescription());
        e.putAll(builder.build(element));
        ProcessEventRequest req = new ProcessEventRequest();
        // TODO should it be in the constants?
        req.setEventType("ELEMENT");
        req.setData(e);
        req.setEventDate(Instant.now().atOffset(ZoneOffset.UTC));
        ProcessEventsApi client = new ProcessEventsApi(apiClientFactory.create(ApiClientConfiguration.builder().sessionToken(event.getSessionToken()).txId(UUID.fromString(event.getInstanceId())).build()));
        client.event(UUID.fromString(event.getInstanceId()), req);
    } catch (Exception e) {
        log.warn("process ['{}'] -> transfer error: {}", event.getInstanceId(), e.getMessage());
    }
}
Also used : AbstractElement(io.takari.bpm.model.AbstractElement) HashMap(java.util.HashMap) SourceAwareProcessDefinition(io.takari.bpm.model.SourceAwareProcessDefinition) ProcessDefinition(io.takari.bpm.model.ProcessDefinition) ExecutionException(io.takari.bpm.api.ExecutionException) ProcessEventRequest(com.walmartlabs.concord.client.ProcessEventRequest) SourceMap(io.takari.bpm.model.SourceMap) SourceAwareProcessDefinition(io.takari.bpm.model.SourceAwareProcessDefinition) ProcessEventsApi(com.walmartlabs.concord.client.ProcessEventsApi)

Example 2 with ProcessEventRequest

use of com.walmartlabs.concord.client.ProcessEventRequest in project concord by walmartlabs.

the class EventRecordingExecutionListener method afterCommand.

@Override
public Result afterCommand(Runtime runtime, VM vm, State state, ThreadId threadId, Command cmd) {
    if (!eventConfiguration.recordEvents()) {
        return Result.CONTINUE;
    }
    if (!(cmd instanceof StepCommand)) {
        return Result.CONTINUE;
    }
    StepCommand<?> s = (StepCommand<?>) cmd;
    if (s.getStep() instanceof TaskCall || s.getStep() instanceof Expression) {
        return Result.CONTINUE;
    }
    ProcessDefinition pd = runtime.getService(ProcessDefinition.class);
    Location loc = s.getStep().getLocation();
    Map<String, Object> m = new HashMap<>();
    m.put("processDefinitionId", ProcessDefinitionUtils.getCurrentFlowName(pd, s.getStep()));
    m.put("fileName", loc.fileName());
    m.put("line", loc.lineNum());
    m.put("column", loc.column());
    m.put("description", getDescription(s.getStep()));
    m.put("correlationId", s.getCorrelationId());
    ProcessEventRequest req = new ProcessEventRequest();
    // TODO constants
    req.setEventType("ELEMENT");
    req.setData(m);
    req.setEventDate(Instant.now().atOffset(ZoneOffset.UTC));
    try {
        eventsApi.event(processInstanceId.getValue(), req);
    } catch (ApiException e) {
        log.warn("afterCommand [{}] -> error while sending an event to the server: {}", cmd, e.getMessage());
    }
    return Result.CONTINUE;
}
Also used : StepCommand(com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand) ProcessEventRequest(com.walmartlabs.concord.client.ProcessEventRequest) HashMap(java.util.HashMap) ApiException(com.walmartlabs.concord.ApiException)

Example 3 with ProcessEventRequest

use of com.walmartlabs.concord.client.ProcessEventRequest in project concord by walmartlabs.

the class TaskCallEventRecordingListener method send.

private void send(Map<String, Object> event) {
    ProcessEventRequest req = new ProcessEventRequest();
    // TODO should it be in the constants?
    req.setEventType("ELEMENT");
    req.setData(event);
    req.setEventDate(Instant.now().atOffset(ZoneOffset.UTC));
    try {
        eventsApi.event(processInstanceId.getValue(), req);
    } catch (ApiException e) {
        log.warn("send [{}] -> error while sending an event to the server: {}", event, e.getMessage());
    }
}
Also used : ProcessEventRequest(com.walmartlabs.concord.client.ProcessEventRequest) ApiException(com.walmartlabs.concord.ApiException)

Example 4 with ProcessEventRequest

use of com.walmartlabs.concord.client.ProcessEventRequest in project concord by walmartlabs.

the class EventSender method doRun.

public void doRun() {
    if (debug) {
        log.info("run -> started...");
    }
    try (RandomAccessFile f = new RandomAccessFile(eventsFile.toFile(), "r")) {
        Batch batch = new Batch(instanceId, eventsApi);
        long t1 = System.currentTimeMillis();
        while (true) {
            String line = f.readLine();
            if (line == null || line.isEmpty()) {
                if (stop) {
                    // don't stop until we reach the end of the file
                    if (Files.size(eventsFile) <= f.getFilePointer()) {
                        break;
                    }
                }
                // wait for more data
                sleep(NO_DATA_DELAY);
            } else {
                if (line.endsWith(EOL_MARKER)) {
                    String data = line.substring(0, line.length() - EOL_MARKER.length());
                    ProcessEventRequest req = objectMapper.readValue(data, ProcessEventRequest.class);
                    batch.add(req);
                } else {
                    // partial line, re-read next time
                    f.seek(f.getFilePointer() - line.length());
                }
            }
            long t2 = System.currentTimeMillis();
            if (batch.size() >= MAX_BATCH_SIZE || t2 - t1 >= MAX_BATCH_AGE) {
                flush(batch);
                t1 = t2;
            }
        }
        flush(batch);
    } catch (IOException e) {
        log.error("Error while reading the event file: {}", e.getMessage(), e);
    }
    if (debug) {
        log.info("run -> stopped...");
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ProcessEventRequest(com.walmartlabs.concord.client.ProcessEventRequest) IOException(java.io.IOException)

Aggregations

ProcessEventRequest (com.walmartlabs.concord.client.ProcessEventRequest)4 ApiException (com.walmartlabs.concord.ApiException)2 HashMap (java.util.HashMap)2 ProcessEventsApi (com.walmartlabs.concord.client.ProcessEventsApi)1 StepCommand (com.walmartlabs.concord.runtime.v2.runner.vm.StepCommand)1 ExecutionException (io.takari.bpm.api.ExecutionException)1 AbstractElement (io.takari.bpm.model.AbstractElement)1 ProcessDefinition (io.takari.bpm.model.ProcessDefinition)1 SourceAwareProcessDefinition (io.takari.bpm.model.SourceAwareProcessDefinition)1 SourceMap (io.takari.bpm.model.SourceMap)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1