Search in sources :

Example 11 with ProcessKey

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

the class ProcessLocksResource method tryLock.

/**
 * Acquires the lock if it is available and returns the LockResult.acquired = true.
 * If the lock is not available then this method will return the LockResult.acquired = false.
 */
@POST
@ApiOperation("Try lock")
@Path("/{processInstanceId}/lock/{lockName}")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public LockResult tryLock(@PathParam("processInstanceId") UUID instanceId, @PathParam("lockName") String lockName, @QueryParam("scope") @DefaultValue("PROJECT") ProcessLockScope scope) {
    ProcessEntry e = assertProcess(instanceId);
    LockEntry lock = dao.tryLock(new ProcessKey(e.instanceId(), e.createdAt()), e.orgId(), e.projectId(), scope, lockName);
    boolean acquired = lock.instanceId().equals(instanceId);
    return LockResult.builder().acquired(acquired).info(lock).build();
}
Also used : ProcessEntry(com.walmartlabs.concord.server.process.ProcessEntry) 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 12 with ProcessKey

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

the class FormFilesStoringProcessor method process.

@Override
@SuppressWarnings("unchecked")
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION);
    if (cfg == null) {
        return chain.process(payload);
    }
    Map<String, String> formFiles = (Map<String, String>) cfg.get(Constants.Files.FORM_FILES);
    if (formFiles == null) {
        return chain.process(payload);
    }
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    try {
        for (Map.Entry<String, String> e : formFiles.entrySet()) {
            Path formFile = workspace.resolve(e.getKey());
            Path tmpFile = Paths.get(e.getValue());
            Path p = formFile.getParent();
            if (!Files.exists(p)) {
                Files.createDirectories(p);
            }
            Files.move(tmpFile, formFile, StandardCopyOption.REPLACE_EXISTING);
        }
    } catch (IOException e) {
        logManager.error(processKey, "Error while saving form files", e);
        throw new ProcessException(processKey, "Error while saving form files", e);
    }
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException) Map(java.util.Map)

Example 13 with ProcessKey

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

the class ResumeMarkerStoringProcessor method process.

@Override
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Set<String> events = payload.getHeader(Payload.RESUME_EVENTS, Collections.emptySet());
    if (events.isEmpty()) {
        return chain.process(payload);
    }
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    Path stateDir = workspace.resolve(Constants.Files.JOB_ATTACHMENTS_DIR_NAME).resolve(Constants.Files.JOB_STATE_DIR_NAME);
    try {
        if (!Files.exists(stateDir)) {
            Files.createDirectories(stateDir);
        }
        Path resumeMarker = stateDir.resolve(Constants.Files.RESUME_MARKER_FILE_NAME);
        Files.write(resumeMarker, events);
    } catch (IOException e) {
        logManager.error(processKey, "Error while saving resume event", e);
        throw new ProcessException(processKey, "Error while saving resume event", e);
    }
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException)

Example 14 with ProcessKey

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

the class ResumingProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    boolean updated = queueManager.updateExpectedStatus(processKey, ProcessStatus.SUSPENDED, ProcessStatus.RESUMING);
    if (updated) {
        return chain.process(payload);
    }
    log.warn("process ['{}'] -> process is not suspended, can't resume", processKey);
    throw new InvalidProcessStateException("Process is not suspended, can't resume");
}
Also used : ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 15 with ProcessKey

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

the class ProcessStateManagerTest method testUpdateState.

@Test
public void testUpdateState() throws Exception {
    ProcessKey processKey = new ProcessKey(UUID.randomUUID(), OffsetDateTime.now());
    Path baseDir = Files.createTempDirectory("testImport");
    writeTempFile(baseDir.resolve("file-1"), "123".getBytes());
    writeTempFile(baseDir.resolve("file-2"), "456".getBytes());
    // 
    ProcessKeyCache processKeyCache = new ProcessKeyCache(new ProcessQueueDao(getConfiguration(), new ConcordObjectMapper(new ObjectMapper())));
    ProcessConfiguration stateCfg = new ProcessConfiguration(Duration.of(24, ChronoUnit.HOURS), Collections.singletonList(Constants.Files.CONFIGURATION_FILE_NAME));
    ProcessStateManager stateManager = new ProcessStateManager(getConfiguration(), mock(SecretStoreConfiguration.class), stateCfg, mock(PolicyManager.class), mock(ProcessLogManager.class), processKeyCache);
    stateManager.importPath(processKey, null, baseDir, (p, attrs) -> true);
    Path tmpDir = Files.createTempDirectory("testExport");
    boolean result = stateManager.export(processKey, copyTo(tmpDir));
    assertTrue(result);
    assertFileContent("123", tmpDir.resolve("file-1"));
    assertFileContent("456", tmpDir.resolve("file-2"));
    // --- update
    writeTempFile(baseDir.resolve("file-1"), "123-up".getBytes());
    stateManager.importPath(processKey, null, baseDir, (p, attrs) -> true);
    result = stateManager.export(processKey, copyTo(tmpDir));
    assertTrue(result);
    assertFileContent("123-up", tmpDir.resolve("file-1"));
    assertFileContent("456", tmpDir.resolve("file-2"));
}
Also used : Path(java.nio.file.Path) ProcessKeyCache(com.walmartlabs.concord.server.process.queue.ProcessKeyCache) PolicyManager(com.walmartlabs.concord.server.policy.PolicyManager) ProcessConfiguration(com.walmartlabs.concord.server.cfg.ProcessConfiguration) ConcordObjectMapper(com.walmartlabs.concord.server.ConcordObjectMapper) SecretStoreConfiguration(com.walmartlabs.concord.server.cfg.SecretStoreConfiguration) ProcessLogManager(com.walmartlabs.concord.server.process.logs.ProcessLogManager) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) ConcordObjectMapper(com.walmartlabs.concord.server.ConcordObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessQueueDao(com.walmartlabs.concord.server.process.queue.ProcessQueueDao) Test(org.junit.jupiter.api.Test) AbstractDaoTest(com.walmartlabs.concord.server.AbstractDaoTest)

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