@Override public String toString() { return getName(); } }
/** * Constructor. * * @param description Description for the job status */ DefaultJobStatus(String description) { this.description = description; this.name = name().toLowerCase(Locale.ENGLISH); }
/** * The hash code of a job row is simply the hash code of its ID. * * @return The hash code of the job row */ @Override public int hashCode() { return Objects.hashCode(getId()); }
/** * Builds a JobRow with values for every field defined in {@link DefaultJobField}. * * @param request The request that is triggering this job * @param requestContext The context of the request triggering this job * * @return A JobRow with all the metadata for the job under construction */ @Override public JobRow buildJobRow(UriInfo request, ContainerRequestContext requestContext) { Map<JobField, String> jobMetadata = new LinkedHashMap<>(DefaultJobField.values().length); String userId = userIdExtractor.apply(requestContext.getSecurityContext()); jobMetadata.put(DefaultJobField.QUERY, request.getRequestUri().toString()); jobMetadata.put(DefaultJobField.STATUS, DefaultJobStatus.PENDING.getName()); String isoDateCreated = new DateTime(timestampGenerator.instant().toEpochMilli()).toDateTimeISO().toString(); jobMetadata.put(DefaultJobField.DATE_CREATED, isoDateCreated); jobMetadata.put(DefaultJobField.DATE_UPDATED, isoDateCreated); jobMetadata.put(DefaultJobField.USER_ID, userId); jobMetadata.put(DefaultJobField.JOB_TICKET, idGenerator.apply(jobMetadata)); return new JobRow(DefaultJobField.JOB_TICKET, jobMetadata); } }
String ticket = jobRow.getId(); job.put(QUERY.getName(), jobRow.get(QUERY)); job.put("results", JobPayloadBuilder.getResultsUrl(ticket, uriInfo)); job.put("syncResults", JobPayloadBuilder.getSyncResultsUrl(ticket, uriInfo)); job.put("self", JobPayloadBuilder.getSelfUrl(ticket, uriInfo)); job.put(STATUS.getName(), jobRow.get(STATUS)); job.put(JOB_TICKET.getName(), jobRow.get(JOB_TICKET)); job.put(DATE_CREATED.getName(), jobRow.get(DATE_CREATED)); job.put(DATE_UPDATED.getName(), jobRow.get(DATE_UPDATED)); job.put(USER_ID.getName(), jobRow.get(USER_ID)); .collect(Collectors.toSet()); String msg = ErrorMessageFormat.JOB_MAPPING_FAILED.format(ticket, jobRow.getRowMap(), missingFields); LOG.error(msg); throw new JobRequestFailedException(msg);
/** * Returns a copy of this JobRow with the specified field set to the specified value. * * @param field The field to set * @param value The value to set the field to * * @return A new JobRow identical this one, except for the specified field */ public JobRow withFieldValue(JobField field, String value) { JobRow newRow = new JobRow(jobId, new LinkedHashMap<>(this)); newRow.put(field, value); return newRow; }
/** * Initializes the service for building JobRows based on the information in a query. * * @return A factory for building JobRows, by default the factory builds a JobRow containing the fields * enumerated in {@link DefaultJobField}, using ids that are the concatenation of the user id with a uuid generated * by {@link UUID#randomUUID()}. */ protected JobRowBuilder buildJobRowBuilder() { return new DefaultJobRowBuilder( jobMetadata -> jobMetadata.get(DefaultJobField.USER_ID) + UUID.randomUUID().toString() ); }
fieldValueMap1.put(DefaultJobField.USER_ID, "momo"); JobRow jobRow1 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap1); apiJobStore.save(jobRow1); fieldValueMap2.put(DefaultJobField.USER_ID, "dodo"); JobRow jobRow2 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap2); apiJobStore.save(jobRow2); fieldValueMap3.put(DefaultJobField.USER_ID, "yoyo"); JobRow jobRow3 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap3); apiJobStore.save(jobRow3);
/** * Coerces the JobRow into a mapping from the names of JobFields to their values. * * @return A mapping from the name of each JobField to its associated value */ public Map<String, String> getRowMap() { return entrySet().stream() .collect(StreamUtils.toLinkedMap(entry -> entry.getKey().getName(), Map.Entry::getValue)); }
/** * Extracts the JobField to be examined from the tokenizedQuery. * * @param tokenizedQuery The tokenized filter expression. * * @return The JobField to be examined * @throws BadFilterException is the JobField does not exist */ private JobField extractJobField(Matcher tokenizedQuery) throws BadFilterException { String fieldName = tokenizedQuery.group(1); return Arrays.stream(DefaultJobField.values()) .filter(field -> field.getName().equals(fieldName)) .findFirst() .orElseThrow(() -> { LOG.debug(FILTER_JOBFIELD_UNDEFINED.logFormat(fieldName, DefaultJobField.values())); return new BadFilterException(FILTER_JOBFIELD_UNDEFINED.format( fieldName, DefaultJobField.values() )); }); }
@Override public String toString() { return String.join("'", "{jobId=", jobId, ", entries=", entrySet().toString(), "}"); } }
/** * Constructor. * * @param description Description of the job field */ DefaultJobField(String description) { this.serializedName = EnumUtils.camelCase(name()); this.description = description; }
/** * Returns a copy of the specified JobRow with the specified field set to the specified value. * The returned JobRow also has the DATE_UPDATED field set to the start of the current day. * * @param row The row to use as a prototype for the new row * @param field The field to update * @param value The value to assign to the field * * @return A JobRow that is identical to the specified row, except with the specified field set to the specified * value, and the DATE_UPDATED field set to the current instant according to timestampGenerator */ private JobRow updateField(JobRow row, JobField field, String value) { return row .withFieldValue(field, value) .withFieldValue( DATE_UPDATED, new DateTime(timestampGenerator.instant().toEpochMilli()).toDateTimeISO().toString() ); } }
@Override protected JobRowBuilder buildJobRowBuilder() { return new DefaultJobRowBuilder( jobMetadata -> jobMetadata.get(DefaultJobField.USER_ID) + UUID.randomUUID().toString(), ignored -> "greg", Clock.systemDefaultZone() ); }
fieldValueMap1.put(DefaultJobField.USER_ID, "momo"); JobRow jobRow1 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap1); apiJobStore.save(jobRow1); fieldValueMap2.put(DefaultJobField.USER_ID, "dodo"); JobRow jobRow2 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap2); apiJobStore.save(jobRow2); fieldValueMap3.put(DefaultJobField.USER_ID, "yoyo"); JobRow jobRow3 = new JobRow(DefaultJobField.JOB_TICKET, fieldValueMap3); apiJobStore.save(jobRow3);
@Override public Observable<JobRow> save(JobRow metadata) { store.put(metadata.getId(), metadata); return Observable.just(metadata); }
@Override protected JobRowBuilder buildJobRowBuilder() { return new DefaultJobRowBuilder( jobMetadata -> jobMetadata.get(DefaultJobField.USER_ID) + UUID.randomUUID().toString(), ignored -> "greg", Clock.systemDefaultZone() ); }
/** * Constructs the observable chains that handle processing the results of an asynchronous query. * <p> * When processing an asynchronous request, we must do the following: * <ol> * <li> Store the job metadata in the ApiJobStore. * <li> Store the results of the response in the PreResponseStore. * <li> Notify all long pollers when the response is available. * </ol> * * @param preResponseEmitter The observable that will eventually emit the PreResponse * @param asynchronousPayload The observable that will eventually emit the JobRow * @param jobRow A producer that generates an instance of the JobRow to save when needed * * @return A stream of notifications that the PreResponse has been successfully stored */ private Observable<String> buildStorePreResponseChain( Observable<PreResponse> preResponseEmitter, Observable<JobRow> asynchronousPayload, JobRow jobRow ) { // We don't want to store the result in the PreResponseStore unless the query is asynchronous. The query is // asynchronous iff the asynchronousPayload emits at least one item. return preResponseEmitter // Using zip as a gate. We don't let the preResponse from the preResponse emitter continue down the // chain until and unless the asynchronousPayload emits an item. .zipWith(asynchronousPayload, (preResponse, ignored) -> preResponse) .flatMap(preResponse -> preResponseStore.save(jobRow.getId(), preResponse)); }
/** * Given a JobRow, map it to the Job payload to be returned to the user. If the JobRow cannot be successfully * mapped to a Job View, JobRequestFailedException is thrown. * * @param jobRow The JobRow to be mapped to job payload * * @return Job payload to be returned to the user */ private Map<String, String> mapJobRowsToJobViews(JobRow jobRow) { try { return jobPayloadBuilder.buildPayload(jobRow, uriInfo); } catch (JobRequestFailedException ignored) { String msg = ErrorMessageFormat.JOBS_RETREIVAL_FAILED.format(jobRow.getId()); LOG.error(msg); throw new JobRequestFailedException(msg); } } }