Search in sources :

Example 1 with JobExistsException

use of com.spotify.helios.master.JobExistsException in project helios by spotify.

the class JobsResource method post.

/**
   * Create a job given the definition in {@code job}.
   *
   * @param job The job to create.
   * @param username The user creating the job.
   * @return The response.
   */
@POST
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public CreateJobResponse post(@Valid final Job job, @RequestUser final String username) {
    final Job.Builder clone = job.toBuilder().setCreatingUser(username).setCreated(clock.now().getMillis()).setHash(job.getId().getHash());
    final Job actualJob = clone.build();
    final Collection<String> errors = jobValidator.validate(actualJob);
    final String jobIdString = actualJob.getId().toString();
    if (!errors.isEmpty()) {
        throw badRequest(new CreateJobResponse(INVALID_JOB_DEFINITION, ImmutableList.copyOf(errors), jobIdString));
    }
    try {
        model.addJob(actualJob);
    } catch (JobExistsException e) {
        throw badRequest(new CreateJobResponse(JOB_ALREADY_EXISTS, ImmutableList.<String>of(), jobIdString));
    }
    log.info("created job: {}", actualJob);
    return new CreateJobResponse(CreateJobResponse.Status.OK, ImmutableList.<String>of(), jobIdString);
}
Also used : CreateJobResponse(com.spotify.helios.common.protocol.CreateJobResponse) JobExistsException(com.spotify.helios.master.JobExistsException) Job(com.spotify.helios.common.descriptors.Job) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Aggregations

ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 Job (com.spotify.helios.common.descriptors.Job)1 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)1 JobExistsException (com.spotify.helios.master.JobExistsException)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1