requireNonNull(config);
requireNonNull(session);
checkNotBlank(config.getInstanceIds());
final IJobKey jobKey = JobKeys.assertValid(IJobKey.build(config.getKey()));
final ITaskConfig task;
try {
sessionValidator.checkAuthenticated(session, ImmutableSet.of(jobKey.getRole()));
task = ConfigurationManager.validateAndPopulate(
ITaskConfig.build(config.getTaskConfig()));
} catch (AuthFailedException e) {
return errorResponse(AUTH_FAILED, e);
} catch (TaskDescriptionException e) {
return errorResponse(INVALID_REQUEST, e);
}
return storage.write(new MutateWork.Quiet<Response>() {
@Override
public Response apply(MutableStoreProvider storeProvider) {
try {
if (cronJobManager.hasJob(jobKey)) {
return invalidResponse("Instances may not be added to cron jobs.");
}
lockManager.validateIfLocked(
ILockKey.build(LockKey.job(jobKey.newBuilder())),
Optional.fromNullable(mutableLock).transform(ILock.FROM_BUILDER));
ImmutableSet<IScheduledTask> currentTasks = storeProvider.getTaskStore().fetchTasks(
Query.jobScoped(task.getJob()).active());
validateTaskLimits(
task,
currentTasks.size() + config.getInstanceIdsSize(),
quotaManager.checkInstanceAddition(task, config.getInstanceIdsSize()));