public PolicyWorkerConverterInterface getConverter(String classifier, int version) throws InvalidTaskException { PolicyWorkerConverterInterface converter = workerInputConverters.get(classifier); if (converter == null) { throw new InvalidTaskException("No WorkerInputConverter found for classifier with the name " + classifier); } if (!isSupported(converter, classifier, version)) { throw new InvalidTaskException("No WorkerInputConverter found for classifier " + classifier + " version " + version); } return converter; }
private SequenceWorkflow getWorkflow(long id, WorkflowApi workflowApi) throws InvalidTaskException { try { logger.warn("Setting up cache: Retrieving SequenceWorkflow from Database."); return workflowApi.retrieveSequenceWorkflow(id); } catch (RuntimeException e) { if (e.getMessage().contains("Could not find a match for the SequenceWorkflow requested")) { throw new InvalidTaskException(e.getMessage(),e); } else { throw e; } } }
private void validateTaskMessage(TaskMessage tm) throws InvalidTaskException { // The task message must be present so that the framework can // callback with a valid message final String taskId = tm.getTaskId(); if (taskId == null) { throw new InvalidTaskException("Task identifier not specified"); } }
/** * Deserialise the given data into the specified class, and validate that any constraints specified have been met. */ @Nonnull public static <T> T deserialiseAndValidateTask(final Codec codec, final Class<T> taskType, final byte[] data) throws InvalidTaskException { final T documentWorkerTask; try { documentWorkerTask = codec.deserialise(data, taskType, DecodeMethod.STRICT); } catch (final CodecException e) { throw new InvalidTaskException("Invalid input message", e); } if (documentWorkerTask == null) { throw new InvalidTaskException("Invalid input message: no result from deserialisation"); } final Set<ConstraintViolation<T>> violations = validator.validate(documentWorkerTask); if (violations.size() > 0) { LOG.error("Task of type {} failed validation due to: {}", taskType.getSimpleName(), violations); throw new InvalidTaskException("Task failed validation"); } return documentWorkerTask; } }
/** * Create a Worker. The input task will be validated. * * @param task the input task for this Worker to operate on * @param resultQueue the reference to the queue that should take results from this type of Worker. This can be null if no resultQueue * is provided for this type of worker * @param codec used to serialising result data * @param workerTaskData The worker task data to use during operation * @throws InvalidTaskException if the input task does not validate successfully */ public AbstractWorker(final T task, final String resultQueue, final Codec codec, final WorkerTaskData workerTaskData) throws InvalidTaskException { this.workerTaskData = workerTaskData; this.task = Objects.requireNonNull(task); this.resultQueue = resultQueue; // resultQueue can be null for a dead end worker this.codec = Objects.requireNonNull(codec); Set<ConstraintViolation<T>> violations = validator.validate(task); if (violations.size() > 0) { LOG.error("Task of type {} failed validation due to: {}", task.getClass().getSimpleName(), violations); throw new InvalidTaskException("Task failed validation"); } }
private static void validateSourceData(Multimap<String, ReferencedData> sourceData) throws InvalidTaskException{ for (Map.Entry<String, ReferencedData> referencedDataEntry : sourceData.entries()) { String reference = referencedDataEntry.getValue().getReference(); byte[] data = referencedDataEntry.getValue().getData(); if ((reference == null || reference.isEmpty()) && (data == null)) { throw new InvalidTaskException("Task data reference is null or empty."); } } } }
@Nonnull private static byte[] validateVersionAndData(final WorkerTaskData workerTask, final int workerApiVersion) throws InvalidTaskException, TaskRejectedException { final int version = workerTask.getVersion(); if (workerApiVersion < version) { throw new TaskRejectedException("Found task version " + version + ", which is newer than " + workerApiVersion); } final byte[] data = workerTask.getData(); if (data == null) { throw new InvalidTaskException("Invalid input message: task not specified"); } return data; }
/** * Verify that the specified worker task has the right type and is a version that can be handled. */ private T verifyWorkerTask( final String classifier, final int version, final byte[] data ) throws TaskRejectedException, InvalidTaskException { // Reject tasks of the wrong type and tasks that require a newer version final String workerName = getWorkerName(); if (!workerName.equals(classifier)) { throw new InvalidTaskException("Task of type " + classifier + " found on queue for " + getWorkerName()); } final int workerApiVersion = getWorkerApiVersion(); if (workerApiVersion < version) { throw new TaskRejectedException("Found task version " + version + ", which is newer than " + workerApiVersion); } if (data == null) { throw new InvalidTaskException("Invalid input message: task not specified"); } try { return codec.deserialise(data, taskClass, DecodeMethod.LENIENT); } catch (CodecException e) { throw new InvalidTaskException("Invalid input message", e); } }
private List<BoilerplateExpression> validateExpressions(SelectedItems selectedItems, String tenantId) throws InvalidTaskException, TaskRejectedException { List<BoilerplateExpression> expressions = null; if (selectedItems instanceof SelectedExpressions) { try { expressions = ((SelectedExpressions) selectedItems) .getExpressionIds() .parallelStream() .map(u -> { try { return expressionCache.get(Pair.of(tenantId, u)); } catch (ExecutionException e) { //Check if execution exception wraps a boilerplate API exception. if (e.getCause() instanceof ApiException) { if (((ApiException) e.getCause()).getCode() == 404) { throw new RuntimeInvalidTaskException(new InvalidTaskException(e.getMessage(), e.getCause())); } } // Assume this is a transient error. throw new RuntimeTaskRejectedException(new TaskRejectedException(e.getMessage(), e.getCause())); } }) .collect(Collectors.toList()); } catch (RuntimeInvalidTaskException e) { throw e.getInvalidTaskException(); } catch (RuntimeTaskRejectedException e) { throw e.getTaskRejectedException(); } } return expressions; }
private Collection<ClassifyDocumentResult> classifyAndPossiblyExecuteDocument(List<Long> collectionSequenceIds, Document document, boolean execute) throws InvalidTaskException, TaskRejectedException { DocumentConverter documentConverter = new DocumentConverter(); List<ClassifyDocumentResult> classifyDocumentResults = new ArrayList<>(); // creating a single corepolicy document, to use across both methods, to avoid creation of it multiple times try (com.github.cafdataprocessing.corepolicy.common.Document doc = documentConverter.convert(document, dataStoreSource)) { // check if the previous policy failed, if it did, try to handle the previous policy with diagnostics // handling enabled. if (checkRetryPreviousFailureWithDiagnostics(collectionSequenceIds, doc, document, null)) return classifyDocumentResults; // Go through each of the collection sequences that we have been asked to evaluate, // skip any that we have already fully completed, or // work out which policies are yet to be executed, and pick up the next one in the list. if (evaluateAndExecuteNextPolicy(collectionSequenceIds, execute, classifyDocumentResults, doc)) return classifyDocumentResults; } catch (RuntimeException e) { if (e.getCause() instanceof SourceNotFoundException) { throw new InvalidTaskException(e.getMessage(), e.getCause()); } else if (e.getCause() instanceof DataSourceException) { // Cannot determine source of DataSourceException. throw new TaskRejectedException(e.getMessage(), e.getCause()); } else { throw e; } } catch (InvalidTaskException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return classifyDocumentResults; }
private List<BoilerplateExpression> validateTags(SelectedItems selectedItems, String tenantId) throws InvalidTaskException, TaskRejectedException { List<BoilerplateExpression> expressions = null; if (selectedItems instanceof SelectedTag) { SelectedTag selectedTag = (SelectedTag) selectedItems; try { Tag tag = tagCache.get(Pair.of(tenantId, selectedTag.getTagId())); if (tag.getDefaultReplacementText() != null) { this.defaultReplacementText = tag.getDefaultReplacementText(); } expressions = tagExpressionCache.get(Pair.of(tenantId, selectedTag.getTagId())); } catch (ExecutionException e) { //Check if execution exception wraps a runtime or boilerplate API exception. if (e.getCause() instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } else if (e.getCause() instanceof ApiException) { if (((ApiException) e.getCause()).getCode() == 404) { throw new InvalidTaskException(e.getMessage(), e.getCause()); } } // Assume this is a transient error. throw new TaskRejectedException(e.getMessage(), e.getCause()); } catch (ClientHandlerException e) { throw new TaskRejectedException("Transitory error encountered during tag retrieval", e); } } return expressions; }
return DocumentTask.create(application, workerTask, documentWorkerDocumentTask); } catch (final InvalidChangeLogException ex) { throw new InvalidTaskException("Invalid change log", ex); } catch (InvalidScriptException ex) { throw new InvalidTaskException("Invalid script", ex); throw new InvalidTaskException("Task of type " + classifier + " found on queue for " + workerName);
throw new InvalidTaskException("Task status not returned and no document to record error"); return converter.convert(baseRuntime); } catch (PolicyWorkerConverterException pwce) { throw new InvalidTaskException("Failed to retrieve taskdata", pwce); throw new InvalidTaskException("Invalid Task returned and no document to record error: " + errorMessage);
try { if (workerTask == null) { throw new InvalidTaskException("Cannot retrieve TaskData, the WorkerTaskData provided was null."); task = codec.deserialise(workerTask.getData(), TaskData.class); } else { throw new InvalidTaskException("Policy Worker Message Version not supported: " + version); throw new InvalidTaskException("Message could not be deserialized", e); throw new InvalidTaskException("Error occurred when converting the data", e); throw new InvalidTaskException( "Unexpected exception occurred", e);
private void registerNewTaskImpl(final String queueMsgId, final byte[] taskMessage, Map<String, Object> headers) throws InvalidTaskException, TaskRejectedException { try { TaskMessage tm = codec.deserialise(taskMessage, TaskMessage.class, DecodeMethod.LENIENT); LOG.debug("Received task {} (message id: {})", tm.getTaskId(), queueMsgId); boolean poison = isTaskPoisoned(headers); validateTaskMessage(tm); boolean taskIsActive = checkStatus(tm); if (taskIsActive) { if (tm.getTo() != null && tm.getTo().equalsIgnoreCase(workerQueue.getInputQueue())) { LOG.debug("Task {} (message id: {}) on input queue {} {}", tm.getTaskId(), queueMsgId, workerQueue.getInputQueue(), (tm.getTo() != null) ? "is intended for this worker" : "has no explicit destination, therefore assuming it is intended for this worker"); executor.executeTask(tm, queueMsgId, poison, headers, codec); } else { LOG.debug("Task {} (message id: {}) is not intended for this worker: input queue {} does not match message destination queue {}", tm.getTaskId(), queueMsgId, workerQueue.getInputQueue(), tm.getTo()); executor.forwardTask(tm, queueMsgId, headers); } } else { LOG.debug("Task {} is no longer active. The task message (message id: {}) will not be executed", tm.getTaskId(), queueMsgId); executor.discardTask(tm, queueMsgId); } } catch (CodecException e) { throw new InvalidTaskException("Queue data did not deserialise to a TaskMessage", e); } catch (InvalidJobTaskIdException ijte) { throw new InvalidTaskException("TaskMessage contains an invalid job task identifier", ijte); } }
} catch (InvalidFieldValueCpeException e) { throw new InvalidTaskException(e.getMessage(),e);
@Override protected WorkerHandlerResponse handleTaskPolicy(Document document, Policy policy, Long collectionSequenceID, TaskData currentTaskData) throws InvalidTaskException { if (currentTaskData.getOutputPartialReference() == null) { throw new InvalidTaskException("partialOutputReference was not set");
throw new InvalidTaskException("Context not present, could not be deserialised, or could not be replaced"); throw new InvalidTaskException("Context did not contain a document");