Search in sources :

Example 1 with ProcessStatus

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

the class CustomFormServiceV2 method continueSession.

private Response continueSession(UriInfo uriInfo, HttpHeaders headers, ProcessKey processKey, String formName, Map<String, Object> data) {
    // TODO locking
    Form form = assertForm(processKey, formName);
    boolean yield = form.options().yield();
    Path dst = cfg.getBaseDir().resolve(processKey.toString()).resolve(formName);
    Path formDir = dst.resolve(FORM_DIR_NAME);
    try {
        Map<String, Object> m = new HashMap<>();
        try {
            m = FormUtils.convert(new ExternalFileFormValidatorLocaleV2(processKey, formName, stateManager), form, data);
            FormSubmitResult r = formService.submit(processKey, formName, m);
            if (r.isValid()) {
                if (yield) {
                    // this was the last "interactive" form. The process will continue in "background"
                    // and users should get a success page.
                    writeData(formDir, success(form, m, processKey.getInstanceId()));
                } else {
                    while (true) {
                        ProcessStatus s = queueDao.getStatus(processKey);
                        if (s == ProcessStatus.SUSPENDED) {
                            String nextFormId = formService.nextFormId(processKey);
                            if (nextFormId == null) {
                                writeData(formDir, success(form, m, processKey.getInstanceId()));
                                break;
                            } else {
                                FormSessionResponse nextSession = startSession(processKey, nextFormId);
                                return redirectTo(nextSession.getUri());
                            }
                        } else if (s == ProcessStatus.FAILED || s == ProcessStatus.CANCELLED || s == ProcessStatus.TIMED_OUT) {
                            writeData(formDir, processFailed(form, m, processKey.getInstanceId()));
                            break;
                        } else if (s == ProcessStatus.FINISHED) {
                            writeData(formDir, success(form, m, processKey.getInstanceId()));
                            break;
                        }
                        try {
                            // TODO exp back off?
                            Thread.sleep(STATUS_REFRESH_DELAY);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            } else {
                writeData(formDir, prepareData(form, m, r.getErrors(), processKey.getInstanceId()));
            }
        } catch (FormUtils.ValidationException e) {
            ValidationError err = ValidationError.of(e.getField().name(), e.getMessage());
            FormData d = prepareData(form, m, Collections.singletonList(err), processKey.getInstanceId());
            writeData(formDir, d);
        }
    } catch (Exception e) {
        throw new ConcordApplicationException("Error while submitting a form", e);
    }
    return redirectToForm(uriInfo, headers, processKey, formName);
}
Also used : Path(java.nio.file.Path) FormData(com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData) Form(com.walmartlabs.concord.forms.Form) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) FormSubmitResult(com.walmartlabs.concord.server.process.form.FormSubmitResult) FormUtils(com.walmartlabs.concord.forms.FormUtils) IOException(java.io.IOException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ValidationError(com.walmartlabs.concord.forms.ValidationError) ExternalFileFormValidatorLocaleV2(com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocaleV2)

Example 2 with ProcessStatus

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

the class ProcessManager method disable.

public void disable(ProcessKey processKey, boolean disabled) {
    ProcessEntry e = queueDao.get(processKey);
    if (e == null) {
        throw new ProcessException(null, "Process not found: " + processKey, Status.NOT_FOUND);
    }
    assertKillOrDisableRights(e);
    ProcessStatus s = e.status();
    if (TERMINATED_PROCESS_STATUSES.contains(s)) {
        queueDao.disable(processKey, disabled);
    }
}
Also used : ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus)

Example 3 with ProcessStatus

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

the class ProcessManager method restoreFromCheckpoint.

public void restoreFromCheckpoint(ProcessKey processKey, UUID checkpointId) {
    ProcessEntry entry = queueDao.get(processKey);
    checkpointManager.assertProcessAccess(entry);
    if (checkpointId == null) {
        throw new ConcordApplicationException("'checkpointId' is mandatory");
    }
    if (entry.disabled()) {
        throw new ConcordApplicationException("Checkpoint can not be restored as process is disabled -> " + entry.instanceId());
    }
    ProcessStatus s = entry.status();
    if (!RESTORE_ALLOWED_STATUSES.contains(s)) {
        throw new ConcordApplicationException("Unable to restore a checkpoint, the process is " + s);
    }
    ProcessCheckpointManager.CheckpointInfo checkpointInfo = checkpointManager.restoreCheckpoint(processKey, checkpointId);
    if (checkpointInfo == null) {
        throw new ConcordApplicationException("Checkpoint " + checkpointId + " not found");
    }
    Payload payload;
    try {
        payload = payloadManager.createResumePayload(processKey, checkpointInfo.eventName(), null);
    } catch (IOException e) {
        log.error("restore ['{}', '{}'] -> error creating a payload: {}", processKey, checkpointInfo.name(), e);
        throw new ConcordApplicationException("Error creating a payload", e);
    }
    queueManager.restore(processKey, checkpointId, entry.status());
    logManager.info(processKey, "Restoring from checkpoint '{}'", checkpointInfo.name());
    resume(payload);
}
Also used : ProcessCheckpointManager(com.walmartlabs.concord.server.process.state.ProcessCheckpointManager) ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) IOException(java.io.IOException)

Example 4 with ProcessStatus

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

the class ProcessResource method waitForCompletion.

/**
 * Waits for completion of a process.
 *
 * @param instanceId
 * @param timeout
 * @return
 */
@GET
@ApiOperation("Wait for a process to finish")
@Produces(MediaType.APPLICATION_JSON)
@javax.ws.rs.Path("/{id}/waitForCompletion")
public ProcessEntry waitForCompletion(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @QueryParam("timeout") @DefaultValue("-1") long timeout) {
    log.info("waitForCompletion ['{}', {}] -> waiting...", instanceId, timeout);
    long t1 = System.currentTimeMillis();
    ProcessEntry r;
    while (true) {
        r = get(instanceId);
        ProcessStatus s = r.status();
        if (s == ProcessStatus.FINISHED || s == ProcessStatus.FAILED || s == ProcessStatus.CANCELLED || s == ProcessStatus.TIMED_OUT) {
            return r;
        }
        if (timeout > 0) {
            long t2 = System.currentTimeMillis();
            if (t2 - t1 >= timeout) {
                log.warn("waitForCompletion ['{}', {}] -> timeout, last status: {}", instanceId, timeout, s);
                throw new ConcordApplicationException(Response.status(Status.REQUEST_TIMEOUT).entity(r).build());
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // NOSONAR
            throw new ConcordApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Request was interrputed").build());
        }
    }
}
Also used : ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with ProcessStatus

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

the class ProcessQueueDao method buildSelect.

private SelectQuery<Record> buildSelect(DSLContext tx, ProcessKey key, ProcessFilter filter) {
    SelectQuery<Record> query = tx.selectQuery();
    // process_queue
    query.addSelect(PROCESS_QUEUE_FIELDS);
    query.addFrom(PROCESS_QUEUE);
    // users
    query.addSelect(USERS.USERNAME);
    query.addJoin(USERS, JoinType.LEFT_OUTER_JOIN, USERS.USER_ID.eq(PROCESS_QUEUE.INITIATOR_ID));
    // repositories
    query.addSelect(REPOSITORIES.REPO_NAME);
    query.addJoin(REPOSITORIES, JoinType.LEFT_OUTER_JOIN, REPOSITORIES.REPO_ID.eq(PROCESS_QUEUE.REPO_ID));
    // organizations
    Field<String> orgNameField = select(ORGANIZATIONS.ORG_NAME).from(ORGANIZATIONS).where(ORGANIZATIONS.ORG_ID.eq(Tables.PROJECTS.ORG_ID)).asField(ORGANIZATIONS.ORG_NAME.getName());
    query.addSelect(orgNameField);
    // projects
    query.addSelect(Tables.PROJECTS.PROJECT_NAME, Tables.PROJECTS.ORG_ID);
    query.addJoin(Tables.PROJECTS, JoinType.LEFT_OUTER_JOIN, Tables.PROJECTS.PROJECT_ID.eq(PROCESS_QUEUE.PROJECT_ID));
    Set<UUID> orgIds = filter.orgIds();
    if (orgIds != null && !orgIds.isEmpty()) {
        if (filter.includeWithoutProject()) {
            query.addConditions(PROJECTS.ORG_ID.in(filter.orgIds()).or(PROCESS_QUEUE.PROJECT_ID.isNull()));
        } else {
            query.addConditions(PROJECTS.ORG_ID.in(filter.orgIds()));
        }
    }
    if (filter.projectId() != null) {
        if (filter.includeWithoutProject()) {
            query.addConditions(PROCESS_QUEUE.PROJECT_ID.eq(filter.projectId()).or(PROCESS_QUEUE.PROJECT_ID.isNull()));
        } else {
            query.addConditions(PROCESS_QUEUE.PROJECT_ID.eq(filter.projectId()));
        }
    }
    if (filter.repoId() != null) {
        if (filter.includeWithoutProject()) {
            query.addConditions(PROCESS_QUEUE.REPO_ID.eq(filter.repoId()).or(PROCESS_QUEUE.REPO_ID.isNull()));
        } else {
            query.addConditions(PROCESS_QUEUE.REPO_ID.eq(filter.repoId()));
        }
    }
    if (filter.repoName() != null && filter.repoId() == null) {
        SelectConditionStep<Record1<UUID>> repoIdSelect = select(REPOSITORIES.REPO_ID).from(REPOSITORIES).where(REPOSITORIES.REPO_NAME.startsWith(filter.repoName()));
        if (filter.projectId() != null) {
            repoIdSelect = repoIdSelect.and(REPOSITORIES.PROJECT_ID.eq(filter.projectId()));
        }
        query.addConditions(PROCESS_QUEUE.REPO_ID.in(repoIdSelect));
    }
    if (filter.initiator() != null) {
        query.addConditions(USERS.USERNAME.startsWith(filter.initiator()));
    }
    if (filter.afterCreatedAt() != null) {
        query.addConditions(PROCESS_QUEUE.CREATED_AT.greaterThan(filter.afterCreatedAt()));
    }
    if (filter.beforeCreatedAt() != null) {
        query.addConditions(PROCESS_QUEUE.CREATED_AT.lessThan(filter.beforeCreatedAt()));
    }
    ProcessStatus status = filter.status();
    if (status != null) {
        query.addConditions(PROCESS_QUEUE.CURRENT_STATUS.eq(status.name()));
    }
    if (filter.parentId() != null) {
        query.addConditions(PROCESS_QUEUE.PARENT_INSTANCE_ID.eq(filter.parentId()));
    }
    MetadataUtils.apply(query, PROCESS_QUEUE.META, filter.metaFilters());
    filterByTags(query, filter.tags());
    FilterUtils.applyDate(query, PROCESS_QUEUE.START_AT, filter.startAt());
    FilterUtils.applyJson(query, PROCESS_QUEUE.REQUIREMENTS, filter.requirements());
    Set<ProcessDataInclude> includes = filter.includes();
    if (includes.contains(ProcessDataInclude.CHILDREN_IDS)) {
        ProcessQueue pq = PROCESS_QUEUE.as("pq");
        SelectConditionStep<Record1<UUID>> childIds = DSL.select(pq.INSTANCE_ID).from(pq).where(pq.PARENT_INSTANCE_ID.eq(PROCESS_QUEUE.INSTANCE_ID));
        Field<UUID[]> childIdsField = DSL.field("array({0})", UUID[].class, childIds).as("children_ids");
        query.addSelect(childIdsField);
    }
    if (includes.contains(ProcessDataInclude.CHECKPOINTS)) {
        ProcessCheckpoints pc = PROCESS_CHECKPOINTS.as("pc");
        SelectJoinStep<Record1<JSONB>> checkpoints = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, jsonbStripNulls(jsonbBuildObject(inline("id"), pc.CHECKPOINT_ID, inline("name"), pc.CHECKPOINT_NAME, inline("correlationId"), pc.CORRELATION_ID, inline("createdAt"), toJsonDate(pc.CHECKPOINT_DATE)))))).from(pc);
        if (key != null) {
            checkpoints.where(pc.INSTANCE_ID.eq(key.getInstanceId()).and(pc.INSTANCE_CREATED_AT.eq(key.getCreatedAt())));
        } else {
            checkpoints.where(pc.INSTANCE_ID.eq(PROCESS_QUEUE.INSTANCE_ID).and(pc.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT)));
        }
        query.addSelect(checkpoints.asField("checkpoints"));
    }
    if (includes.contains(ProcessDataInclude.CHECKPOINTS_HISTORY)) {
        ProcessEvents pe = PROCESS_EVENTS.as("pe");
        SelectJoinStep<Record1<JSONB>> history = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, checkpointHistoryEntryToJsonb(pe)))).from(pe);
        if (key != null) {
            history.where(pe.INSTANCE_ID.eq(key.getInstanceId()).and(pe.INSTANCE_CREATED_AT.eq(key.getCreatedAt()).and(pe.EVENT_TYPE.eq(EventType.CHECKPOINT_RESTORE.name()))));
        } else {
            history.where(PROCESS_QUEUE.INSTANCE_ID.eq(pe.INSTANCE_ID).and(pe.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT).and(pe.EVENT_TYPE.eq(EventType.CHECKPOINT_RESTORE.name()))));
        }
        query.addSelect(history.asField("checkpoints_history"));
    }
    if (includes.contains(ProcessDataInclude.STATUS_HISTORY)) {
        ProcessEvents pe = PROCESS_EVENTS.as("pe");
        SelectJoinStep<Record1<JSONB>> history = tx.select(function("to_jsonb", JSONB.class, function("array_agg", Object.class, statusHistoryEntryToJsonb(pe)))).from(pe);
        if (key != null) {
            history.where(pe.INSTANCE_ID.eq(key.getInstanceId()).and(pe.INSTANCE_CREATED_AT.eq(key.getCreatedAt()).and(pe.EVENT_TYPE.eq(EventType.PROCESS_STATUS.name()))));
        } else {
            history.where(PROCESS_QUEUE.INSTANCE_ID.eq(pe.INSTANCE_ID).and(pe.INSTANCE_CREATED_AT.eq(PROCESS_QUEUE.CREATED_AT).and(pe.EVENT_TYPE.eq(EventType.PROCESS_STATUS.name()))));
        }
        query.addSelect(history.asField("status_history"));
    }
    Integer limit = filter.limit();
    if (limit != null && limit > 0) {
        query.addLimit(limit);
    }
    Integer offset = filter.offset();
    if (offset != null && offset > 0) {
        query.addOffset(offset);
    }
    return query;
}
Also used : ProcessStatus(com.walmartlabs.concord.server.sdk.ProcessStatus) ProcessQueue(com.walmartlabs.concord.server.jooq.tables.ProcessQueue) ProcessCheckpoints(com.walmartlabs.concord.server.jooq.tables.ProcessCheckpoints) ProcessEvents(com.walmartlabs.concord.server.jooq.tables.ProcessEvents) ProcessQueueRecord(com.walmartlabs.concord.server.jooq.tables.records.ProcessQueueRecord) Record(org.jooq.Record)

Aggregations

ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)9 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)5 IOException (java.io.IOException)3 ValidationError (com.walmartlabs.concord.forms.ValidationError)2 FormSubmitResult (com.walmartlabs.concord.server.process.form.FormSubmitResult)2 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)2 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)2 Path (java.nio.file.Path)2 CachedGauge (com.codahale.metrics.CachedGauge)1 Form (com.walmartlabs.concord.forms.Form)1 FormUtils (com.walmartlabs.concord.forms.FormUtils)1 Imports (com.walmartlabs.concord.imports.Imports)1 ExclusiveMode (com.walmartlabs.concord.runtime.v2.model.ExclusiveMode)1 FormData (com.walmartlabs.concord.server.console.CustomFormServiceV1.FormData)1 ProcessCheckpoints (com.walmartlabs.concord.server.jooq.tables.ProcessCheckpoints)1 ProcessEvents (com.walmartlabs.concord.server.jooq.tables.ProcessEvents)1 ProcessQueue (com.walmartlabs.concord.server.jooq.tables.ProcessQueue)1 ProcessQueueRecord (com.walmartlabs.concord.server.jooq.tables.records.ProcessQueueRecord)1 ExternalFileFormValidatorLocale (com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocale)1 ExternalFileFormValidatorLocaleV2 (com.walmartlabs.concord.server.process.form.ExternalFileFormValidatorLocaleV2)1