use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestManager method save.
public SingularityCreateResult save(SingularityRequest request, RequestState state, RequestHistoryType eventType, long timestamp, Optional<String> user, Optional<String> message) {
saveHistory(new SingularityRequestHistory(timestamp, user, eventType, request, message));
leaderCache.putRequest(new SingularityRequestWithState(request, state, timestamp));
return save(getRequestPath(request.getId()), new SingularityRequestWithState(request, state, timestamp), requestTranscoder);
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestResource method scale.
public SingularityRequestParent scale(String requestId, SingularityScaleRequest scaleRequest, SingularityUser user) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
SingularityRequest oldRequest = oldRequestWithState.getRequest();
authorizationHelper.checkForAuthorization(oldRequest, user, SingularityAuthorizationScope.WRITE);
validator.checkActionEnabled(SingularityAction.SCALE_REQUEST);
SingularityRequest newRequest = oldRequest.toBuilder().setInstances(scaleRequest.getInstances()).build();
validator.checkScale(newRequest, Optional.<Integer>absent());
checkBadRequest(oldRequest.getInstancesSafe() != newRequest.getInstancesSafe(), "Scale request has no affect on the # of instances (%s)", newRequest.getInstancesSafe());
String scaleMessage = String.format("Scaling from %d -> %d", oldRequest.getInstancesSafe(), newRequest.getInstancesSafe());
if (scaleRequest.getMessage().isPresent()) {
scaleMessage = String.format("%s -- %s", scaleRequest.getMessage().get(), scaleMessage);
} else {
scaleMessage = String.format("%s", scaleMessage);
}
if (scaleRequest.getBounce().or(newRequest.getBounceAfterScale().or(false))) {
validator.checkActionEnabled(SingularityAction.BOUNCE_REQUEST);
checkBadRequest(newRequest.isLongRunning(), "Can not bounce a %s request (%s)", newRequest.getRequestType(), newRequest);
checkConflict(oldRequestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to bounce (it must be manually unpaused first)", newRequest.getId());
checkConflict(!requestManager.cleanupRequestExists(newRequest.getId(), RequestCleanupType.BOUNCE), "Request %s is already bouncing cannot bounce again", newRequest.getId());
final boolean isIncrementalBounce = scaleRequest.getIncremental().or(true);
validator.checkResourcesForBounce(newRequest, isIncrementalBounce);
validator.checkRequestForPriorityFreeze(newRequest);
SingularityBounceRequest bounceRequest = new SingularityBounceRequest(Optional.of(isIncrementalBounce), scaleRequest.getSkipHealthchecks(), Optional.<Long>absent(), Optional.of(UUID.randomUUID().toString()), Optional.<String>absent(), Optional.<SingularityShellCommand>absent());
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.of(bounceRequest), user);
} else {
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.absent(), user);
}
if (scaleRequest.getDurationMillis().isPresent()) {
requestManager.saveExpiringObject(new SingularityExpiringScale(requestId, user.getEmail(), System.currentTimeMillis(), scaleRequest, oldRequest.getInstances(), scaleRequest.getActionId().or(UUID.randomUUID().toString()), scaleRequest.getBounce()));
} else {
requestManager.deleteExpiringObject(SingularityExpiringScale.class, requestId);
}
if (!scaleRequest.getSkipEmailNotification().isPresent() || !scaleRequest.getSkipEmailNotification().get()) {
mailer.sendRequestScaledMail(newRequest, Optional.of(scaleRequest), oldRequest.getInstances(), user.getEmail());
}
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestResource method updateAuthorizedGroups.
public SingularityRequestParent updateAuthorizedGroups(SingularityUser user, String requestId, SingularityUpdateGroupsRequest updateGroupsRequest) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
authorizationHelper.checkForAuthorization(oldRequestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
SingularityRequest newRequest = oldRequestWithState.getRequest().toBuilder().setGroup(updateGroupsRequest.getGroup()).setReadWriteGroups(Optional.of(updateGroupsRequest.getReadWriteGroups())).setReadOnlyGroups(Optional.of(updateGroupsRequest.getReadOnlyGroups())).build();
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.UPDATED), Optional.absent(), updateGroupsRequest.getMessage(), Optional.absent(), user);
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestResource method pause.
public SingularityRequestParent pause(String requestId, Optional<SingularityPauseRequest> pauseRequest, SingularityUser user) {
SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
checkConflict(requestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to pause (it must be manually unpaused first)", requestWithState.getRequest().getId());
Optional<Boolean> killTasks = Optional.absent();
Optional<String> message = Optional.absent();
Optional<String> actionId = Optional.absent();
Optional<SingularityShellCommand> runBeforeKill = Optional.absent();
if (pauseRequest.isPresent()) {
killTasks = pauseRequest.get().getKillTasks();
message = pauseRequest.get().getMessage();
if (pauseRequest.get().getRunShellCommandBeforeKill().isPresent()) {
validator.checkValidShellCommand(pauseRequest.get().getRunShellCommandBeforeKill().get());
runBeforeKill = pauseRequest.get().getRunShellCommandBeforeKill();
}
if (pauseRequest.get().getDurationMillis().isPresent() && !actionId.isPresent()) {
actionId = Optional.of(UUID.randomUUID().toString());
}
}
final long now = System.currentTimeMillis();
Optional<Boolean> removeFromLoadBalancer = Optional.absent();
SingularityCreateResult result = requestManager.createCleanupRequest(new SingularityRequestCleanup(user.getEmail(), RequestCleanupType.PAUSING, now, killTasks, removeFromLoadBalancer, requestId, Optional.<String>absent(), Optional.<Boolean>absent(), message, actionId, runBeforeKill));
checkConflict(result == SingularityCreateResult.CREATED, "%s is already pausing - try again soon", requestId, result);
mailer.sendRequestPausedMail(requestWithState.getRequest(), pauseRequest, user.getEmail());
requestManager.pause(requestWithState.getRequest(), now, user.getEmail(), message);
if (pauseRequest.isPresent() && pauseRequest.get().getDurationMillis().isPresent()) {
requestManager.saveExpiringObject(new SingularityExpiringPause(requestId, user.getEmail(), System.currentTimeMillis(), pauseRequest.get(), actionId.get()));
}
return fillEntireRequest(new SingularityRequestWithState(requestWithState.getRequest(), RequestState.PAUSED, now));
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestResource method scheduleImmediately.
@POST
@Path("/request/{requestId}/run")
@Consumes({ MediaType.APPLICATION_JSON })
@ApiOperation(value = "Schedule a one-off or scheduled Singularity request for immediate or delayed execution.", response = SingularityRequestParent.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Singularity Request is not scheduled or one-off") })
public SingularityPendingRequestParent scheduleImmediately(@Auth SingularityUser user, @ApiParam("The request ID to run") @PathParam("requestId") String requestId, SingularityRunNowRequest runNowRequest) {
final Optional<SingularityRunNowRequest> maybeRunNowRequest = Optional.fromNullable(runNowRequest);
SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
checkConflict(requestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to run now (it must be manually unpaused first)", requestWithState.getRequest().getId());
final SingularityPendingRequest pendingRequest = validator.checkRunNowRequest(getAndCheckDeployId(requestId), user.getEmail(), requestWithState.getRequest(), maybeRunNowRequest, taskManager.getActiveTaskIdsForRequest(requestId), taskManager.getPendingTaskIdsForRequest(requestId));
SingularityCreateResult result = requestManager.addToPendingQueue(pendingRequest);
checkConflict(result != SingularityCreateResult.EXISTED, "%s is already pending, please try again soon", requestId);
return SingularityPendingRequestParent.fromSingularityRequestParent(fillEntireRequest(requestWithState), pendingRequest);
}
Aggregations