IJobConfiguration job = IJobConfiguration.build(mutableJob);
IJobKey jobKey = JobKeys.assertValid(job.getKey());
requireNonNull(session);
Response response = Util.emptyResponse();
try {
sessionValidator.checkAuthenticated(session, ImmutableSet.of(job.getOwner().getRole()));
} catch (AuthFailedException e) {
return addMessage(response, AUTH_FAILED, e);
}
try {
SanitizedConfiguration sanitized = SanitizedConfiguration.fromUnsanitized(job);
lockManager.validateIfLocked(
ILockKey.build(LockKey.job(jobKey.newBuilder())),
Optional.fromNullable(mutableLock).transform(ILock.FROM_BUILDER));
if (!sanitized.isCron()) {
LOG.info("Invalid attempt to schedule non-cron job "
+ sanitized.getJobConfig().getKey()
+ " with cron.");
response.setResponseCode(INVALID_REQUEST);
return addMessage(
response,
"Job " + sanitized.getJobConfig().getKey() + " has no cron schedule");
}
try {
// TODO(mchucarroll): Merge CronJobManager.createJob/updateJob
if (cronJobManager.hasJob(sanitized.getJobConfig().getKey())) {
// The job already has a schedule: so update it.
cronJobManager.updateJob(SanitizedCronJob.from(sanitized));
} else {
cronJobManager.createJob(SanitizedCronJob.from(sanitized));
}
} catch (CronException e) {
addMessage(response, INVALID_REQUEST, e);
return response;
}
response.setResponseCode(OK);
} catch (LockException e) {
addMessage(response, LOCK_ERROR, e);
} catch (TaskDescriptionException e) {
addMessage(response, INVALID_REQUEST, e);
}