Search in sources :

Example 26 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessEventDao method insert.

/**
 * Batch inserts the provided {@code events}.
 *
 * @return the same events updated with autogenerated IDs
 */
public List<ProcessEvent> insert(DSLContext tx, List<NewProcessEvent> events) {
    if (events == null || events.isEmpty()) {
        return Collections.emptyList();
    }
    if (events.size() == 1) {
        NewProcessEvent ev = events.get(0);
        ProcessKey processKey = ev.processKey();
        Field<OffsetDateTime> ts = ev.eventDate() != null ? value(ev.eventDate()) : currentOffsetDateTime();
        Map<String, Object> m = ev.data() != null ? ev.data() : Collections.emptyMap();
        String eventType = ev.eventType();
        ProcessEventsRecord r = tx.insertInto(PROCESS_EVENTS).set(PROCESS_EVENTS.INSTANCE_ID, processKey.getInstanceId()).set(PROCESS_EVENTS.INSTANCE_CREATED_AT, processKey.getCreatedAt()).set(PROCESS_EVENTS.EVENT_TYPE, eventType).set(PROCESS_EVENTS.EVENT_DATE, ts).set(PROCESS_EVENTS.EVENT_DATA, objectMapper.toJSONB(m)).returning(PROCESS_EVENTS.EVENT_DATE, PROCESS_EVENTS.EVENT_SEQ).fetchOne();
        return Collections.singletonList(ProcessEvent.builder().eventSeq(r.getEventSeq()).eventDate(r.getEventDate()).eventType(eventType).processKey(processKey).data(m).build());
    }
    InsertSetStep<ProcessEventsRecord> q = tx.insertInto(PROCESS_EVENTS);
    InsertSetMoreStep<ProcessEventsRecord> qq = null;
    for (Iterator<NewProcessEvent> i = events.iterator(); i.hasNext(); ) {
        NewProcessEvent ev = i.next();
        ProcessKey pk = ev.processKey();
        ProcessEventsRecord r = new ProcessEventsRecord();
        r.setInstanceId(pk.getInstanceId());
        r.setInstanceCreatedAt(pk.getCreatedAt());
        r.setEventType(ev.eventType());
        // TODO replace with default = now()?
        OffsetDateTime eventDate = ev.eventDate() != null ? ev.eventDate() : OffsetDateTime.now();
        r.setEventDate(eventDate);
        Map<String, Object> m = ev.data() != null ? ev.data() : Collections.emptyMap();
        r.setEventData(objectMapper.toJSONB(m));
        qq = q.set(r);
        if (i.hasNext()) {
            qq.newRecord();
        }
    }
    // return only a subset of columns to save some traffic
    Result<ProcessEventsRecord> records = qq.returning(PROCESS_EVENTS.EVENT_DATE, PROCESS_EVENTS.EVENT_SEQ).fetch();
    if (records.size() != events.size()) {
        throw new IllegalStateException("Invalid result. Returning records count doesn't match the number of events: " + records.size() + " != " + events.size());
    }
    List<ProcessEvent> result = new ArrayList<>();
    for (int i = 0; i < events.size(); i++) {
        ProcessEventsRecord r = records.get(i);
        NewProcessEvent ev = events.get(i);
        Map<String, Object> data = ev.data();
        result.add(ProcessEvent.builder().eventDate(r.getEventDate()).eventSeq(r.getEventSeq()).eventType(ev.eventType()).processKey(ev.processKey()).data(data != null ? data : Collections.emptyMap()).build());
    }
    return result;
}
Also used : ProcessEvent(com.walmartlabs.concord.server.sdk.events.ProcessEvent) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) OffsetDateTime(java.time.OffsetDateTime) ProcessEventsRecord(com.walmartlabs.concord.server.jooq.tables.records.ProcessEventsRecord)

Example 27 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessEventDao method insert.

public void insert(DSLContext tx, List<ProcessKey> processKeys, String eventType, Map<String, Object> data) {
    String sql = tx.insertInto(PROCESS_EVENTS).set(PROCESS_EVENTS.INSTANCE_ID, (UUID) null).set(PROCESS_EVENTS.INSTANCE_CREATED_AT, (OffsetDateTime) null).set(PROCESS_EVENTS.EVENT_TYPE, (String) null).set(PROCESS_EVENTS.EVENT_DATE, currentOffsetDateTime()).set(PROCESS_EVENTS.EVENT_DATA, (JSONB) null).returning(PROCESS_EVENTS.EVENT_SEQ).getSQL();
    tx.connection(conn -> {
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            for (ProcessKey pk : processKeys) {
                ps.setObject(1, pk.getInstanceId());
                ps.setObject(2, pk.getCreatedAt());
                ps.setString(3, eventType);
                ps.setString(4, objectMapper.toJSONB(data).toString());
                ps.addBatch();
            }
            ps.executeBatch();
        }
    });
}
Also used : OffsetDateTime(java.time.OffsetDateTime) PreparedStatement(java.sql.PreparedStatement) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey)

Example 28 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessEventResource method list.

/**
 * List process events.
 */
@GET
@ApiOperation(value = "List process events", responseContainer = "list", response = ProcessEventEntry.class)
@Path("/{processInstanceId}/event")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public List<ProcessEventEntry> list(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam @QueryParam("type") String eventType, @ApiParam @QueryParam("after") OffsetDateTimeParam after, @ApiParam @QueryParam("fromId") Long fromId, @ApiParam @QueryParam("eventCorrelationId") UUID eventCorrelationId, @ApiParam @QueryParam("eventPhase") EventPhase eventPhase, @ApiParam @QueryParam("includeAll") @DefaultValue("false") boolean includeAll, @ApiParam @QueryParam("limit") @DefaultValue("-1") int limit) {
    ProcessKey processKey = assertProcessKey(processInstanceId);
    if (includeAll) {
        // verify that the user can access potentially sensitive data
        assertAccessRights(processKey);
    }
    ProcessEventFilter f = ProcessEventFilter.builder().processKey(processKey).after(unwrap(after)).eventType(eventType).eventCorrelationId(eventCorrelationId).eventPhase(eventPhase).limit(limit).fromId(fromId).build();
    List<ProcessEventEntry> l = eventManager.list(f);
    if (!includeAll) {
        l = filterOutSensitiveData(l);
    }
    return l;
}
Also used : ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 29 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessEventResource method batchEvent.

/**
 * Register multiple events for the specified process.
 */
@POST
@ApiOperation(value = "Register multiple events for the specified process", authorizations = { @Authorization("session_key"), @Authorization("api_key") })
@Path("/{processInstanceId}/eventBatch")
@Consumes(MediaType.APPLICATION_JSON)
@WithTimer
public void batchEvent(@ApiParam @PathParam("processInstanceId") UUID processInstanceId, @ApiParam List<ProcessEventRequest> data) {
    ProcessKey processKey = assertProcessKey(processInstanceId);
    List<NewProcessEvent> events = data.stream().map(req -> NewProcessEvent.builder().processKey(processKey).eventType(req.getEventType()).eventDate(req.getEventDate()).data(req.getData()).build()).collect(Collectors.toList());
    eventManager.event(events);
}
Also used : Resource(org.sonatype.siesta.Resource) java.util(java.util) ApiParam(io.swagger.annotations.ApiParam) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ProcessKeyCache(com.walmartlabs.concord.server.process.queue.ProcessKeyCache) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) ProcessQueueDao(com.walmartlabs.concord.server.process.queue.ProcessQueueDao) MediaType(javax.ws.rs.core.MediaType) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) Utils.unwrap(com.walmartlabs.concord.server.Utils.unwrap) OffsetDateTimeParam(com.walmartlabs.concord.server.OffsetDateTimeParam) Api(io.swagger.annotations.Api) Named(javax.inject.Named) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) Roles(com.walmartlabs.concord.server.security.Roles) UserPrincipal(com.walmartlabs.concord.server.security.UserPrincipal) ProjectAccessManager(com.walmartlabs.concord.server.org.project.ProjectAccessManager) Collectors(java.util.stream.Collectors) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ResourceAccessLevel(com.walmartlabs.concord.server.org.ResourceAccessLevel) ProjectIdAndInitiator(com.walmartlabs.concord.server.process.queue.ProcessQueueDao.ProjectIdAndInitiator) Authorization(io.swagger.annotations.Authorization) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 30 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class ProcessResource method appendLog.

/**
 * Appends a process' log.
 *
 * @param instanceId
 * @param data
 * @see ProcessLogResourceV2
 * @deprecated in favor of the /api/v2/process/{id}/log* endpoints
 */
@POST
@javax.ws.rs.Path("{id}/log")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@WithTimer
@Deprecated
public void appendLog(@PathParam("id") UUID instanceId, InputStream data) {
    ProcessKey processKey = assertProcessKey(instanceId);
    try {
        byte[] ab = IOUtils.toByteArray(data);
        int upper = logManager.log(processKey, ab);
        // whenever we accept logs from an external source (e.g. from an Agent) we need to check
        // the log size limits
        int logSizeLimit = processCfg.getLogSizeLimit();
        if (upper >= logSizeLimit) {
            logManager.error(processKey, "Maximum log size reached: {}. Process cancelled.", logSizeLimit);
            processManager.kill(processKey);
        }
    } catch (IOException e) {
        throw new ConcordApplicationException("Error while appending a log: " + e.getMessage());
    }
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) EntryPoint(com.walmartlabs.concord.server.process.PayloadManager.EntryPoint) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Aggregations

ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)69 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)28 Path (java.nio.file.Path)27 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)25 ApiOperation (io.swagger.annotations.ApiOperation)22 ProcessException (com.walmartlabs.concord.server.process.ProcessException)20 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)16 IOException (java.io.IOException)16 UUID (java.util.UUID)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)8 Inject (javax.inject.Inject)8 Named (javax.inject.Named)8 ProcessKeyCache (com.walmartlabs.concord.server.process.queue.ProcessKeyCache)7 ProcessQueueDao (com.walmartlabs.concord.server.process.queue.ProcessQueueDao)7 ProcessStateManager (com.walmartlabs.concord.server.process.state.ProcessStateManager)7 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)7 ProcessLogManager (com.walmartlabs.concord.server.process.logs.ProcessLogManager)6 HttpUtils (com.walmartlabs.concord.server.HttpUtils)5 ResourceAccessLevel (com.walmartlabs.concord.server.org.ResourceAccessLevel)5