use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessResource method disable.
/**
* Disable a process.
*
* @param instanceId
* @param disabled
*/
@POST
@ApiOperation("Disable a process")
@javax.ws.rs.Path("/{id}/disable/{disabled}")
@WithTimer
public void disable(@ApiParam @PathParam("id") UUID instanceId, @ApiParam @PathParam("disabled") boolean disabled) {
ProcessKey processKey = assertProcessKey(instanceId);
processManager.disable(processKey, disabled);
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessResource method downloadAttachment.
/**
* Returns a process' attachment file.
*/
@GET
@ApiOperation(value = "Download a process' attachment", response = File.class)
@javax.ws.rs.Path("/{id}/attachment/{name:.*}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadAttachment(@ApiParam @PathParam("id") UUID instanceId, @PathParam("name") @NotNull @Size(min = 1) String attachmentName) {
ProcessEntry processEntry = processManager.assertProcess(instanceId);
assertProcessAccess(processEntry, "attachment");
PartialProcessKey processKey = new ProcessKey(processEntry.instanceId(), processEntry.createdAt());
// TODO replace with javax.validation
if (attachmentName.endsWith("/")) {
throw new ConcordApplicationException("Invalid attachment name: " + attachmentName, Status.BAD_REQUEST);
}
String resource = path(Constants.Files.JOB_ATTACHMENTS_DIR_NAME, attachmentName);
Optional<Path> o = stateManager.get(processKey, resource, src -> {
try {
Path tmp = IOUtils.createTempFile("attachment", ".bin");
try (OutputStream dst = Files.newOutputStream(tmp)) {
IOUtils.copy(src, dst);
}
return Optional.of(tmp);
} catch (IOException e) {
throw new ConcordApplicationException("Error while exporting an attachment: " + attachmentName, e);
}
});
if (!o.isPresent()) {
return Response.status(Status.NOT_FOUND).build();
}
Path tmp = o.get();
return Response.ok((StreamingOutput) out -> {
try (InputStream in = Files.newInputStream(tmp)) {
IOUtils.copy(in, out);
} finally {
Files.delete(tmp);
}
}).build();
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class PayloadBuilder method with.
/**
* Parses the provided {@code input} using the following logic:
* <ul>
* <li>part names should not start with '/' or contain '..'</li>
* <li>any {@code text/plain} is converted into the process' configuration value.
* Dots '.' in the part's name are used as the property's path. E.g. {@code x.y.z} is
* converted into a nested Map object {@code {"x": {"y": {"z": ...}}}}</li>
* <li>parts of other types are saved as files in the payload's {@code ${workDir}}.
* The part's name will be used as the file's path.</li>
* </ul>
*/
public PayloadBuilder with(MultipartInput input) throws IOException {
Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION);
cfg = new HashMap<>(cfg != null ? cfg : Collections.emptyMap());
ProcessKey pk = payload.getProcessKey();
for (InputPart p : input.getParts()) {
String name = MultipartUtils.extractName(p);
if (name == null || name.startsWith("/") || name.contains("..")) {
throw new ProcessException(pk, "Invalid attachment name: " + name, Response.Status.BAD_REQUEST);
}
if (p.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
String v = p.getBodyAsString().trim();
Map<String, Object> m = ConfigurationUtils.toNested(name, v);
cfg = ConfigurationUtils.deepMerge(cfg, m);
} else {
try (InputStream in = p.getBody(InputStream.class, null)) {
addAttachment(name, in);
}
}
}
payload = payload.putHeader(Payload.CONFIGURATION, cfg).putAttachments(attachments);
return this;
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessQueueManager method insert.
/**
* Creates the initial queue record for the specified process payload.
*/
public void insert(Payload payload, ProcessStatus status) {
ProcessKey processKey = payload.getProcessKey();
ProcessKind kind = payload.getHeader(Payload.PROCESS_KIND, ProcessKind.DEFAULT);
UUID projectId = payload.getHeader(Payload.PROJECT_ID);
UUID repoId = payload.getHeader(Payload.REPOSITORY_ID);
UUID parentInstanceId = payload.getHeader(Payload.PARENT_INSTANCE_ID);
UUID initiatorId = payload.getHeader(Payload.INITIATOR_ID);
Map<String, Object> cfg = getCfg(payload);
Map<String, Object> meta = getMeta(cfg);
TriggeredByEntry triggeredBy = payload.getHeader(Payload.TRIGGERED_BY);
String branchOrTag = MapUtils.getString(cfg, Constants.Request.REPO_BRANCH_OR_TAG);
String commitId = MapUtils.getString(cfg, Constants.Request.REPO_COMMIT_ID);
queueDao.tx(tx -> {
queueDao.insert(tx, processKey, status, kind, parentInstanceId, projectId, repoId, branchOrTag, commitId, initiatorId, meta, triggeredBy);
notifyStatusChange(tx, processKey, status);
processLogManager.createSystemSegment(tx, payload.getProcessKey());
});
}
use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.
the class ProcessQueueManager method enqueue.
/**
* Updates an existing record, moving the process into the ENQUEUED status.
*/
public boolean enqueue(Payload payload) {
ProcessKey processKey = payload.getProcessKey();
ProcessStatus s = queueDao.getStatus(processKey);
if (s == null) {
throw new ProcessException(processKey, "Process not found: " + processKey);
}
if (s == ProcessStatus.CANCELLED) {
// (e.g. it was a process in an "exclusive" group)
return false;
}
if (!TO_ENQUEUED_STATUSES.contains(s)) {
// something's wrong (e.g. someone tried to change the process' status directly in the DB and was unlucky)
throw new ProcessException(processKey, "Invalid process status: " + s);
}
Set<String> tags = payload.getHeader(Payload.PROCESS_TAGS);
OffsetDateTime startAt = PayloadUtils.getStartAt(payload);
Map<String, Object> requirements = PayloadUtils.getRequirements(payload);
Long processTimeout = getProcessTimeout(payload);
Long suspendTimeout = getSuspendTimeout(payload);
Set<String> handlers = payload.getHeader(Payload.PROCESS_HANDLERS);
Map<String, Object> meta = getMeta(getCfg(payload));
Imports imports = payload.getHeader(Payload.IMPORTS);
ExclusiveMode exclusive = PayloadUtils.getExclusive(payload);
String runtime = payload.getHeader(Payload.RUNTIME);
List<String> dependencies = payload.getHeader(Payload.DEPENDENCIES);
return queueDao.txResult(tx -> {
boolean updated = queueDao.enqueue(tx, processKey, tags, startAt, requirements, processTimeout, handlers, meta, imports, exclusive, runtime, dependencies, suspendTimeout, TO_ENQUEUED_STATUSES);
if (updated) {
notifyStatusChange(tx, processKey, ProcessStatus.ENQUEUED);
}
return updated;
});
}
Aggregations