void triggerTermination() { LOGGER.debug("Requested Enrichment Requestor worker termination."); this.terminated=true; final RequestJob job = new RequestJob(null); LOGGER.trace("Created job {}",job.description()); queueJob(job); }
private void cancelPendingJobs() { final List<RequestJob> pendingJobs=Lists.newArrayList(); this.queue.drainTo(pendingJobs); for(final RequestJob job:pendingJobs) { job.cancel(); } logCancelledRequestJobs(pendingJobs); }
private void logAbortedRequestJobs(final List<Runnable> unfinished) { if(LOGGER.isTraceEnabled()) { final List<URI> aborted=Lists.newArrayList(); for(final Runnable runnable:unfinished) { final RequestJob job=this.executor.unwrap(runnable,RequestJob.class); if(job!=null && !job.requiresTermination()) { aborted.add(job.context().targetExecution().executionId()); } } if(!aborted.isEmpty()) { LOGGER.trace("Aborted {} pending execution enrichment requests ({})",aborted.size(),aborted); } else { LOGGER.trace("All scheduled pending execution enrichment requests were queued."); } } }
private void processJobs() { while(!this.terminated) { try { final RequestJob job=this.queue.take(); if(!job.requiresTermination()) { processJob(job); } } catch (final InterruptedException e) { // Ignore interruption. Worker can only be terminated via // the triggerTermination method LOGGER.trace("Interrupted while waiting for job",e); Thread.currentThread().interrupt(); } } cancelPendingJobs(); }
private void logCancelledRequestJobs(final List<RequestJob> pendingJobs) { if(LOGGER.isTraceEnabled()) { final List<URI> executionIds=Lists.newArrayList(); for(final RequestJob job:pendingJobs) { if(!job.requiresTermination()) { executionIds.add(job.context().targetExecution().executionId()); } } if(!executionIds.isEmpty()) { LOGGER.trace("Cancelled {} pending execution enrichment request jobs ({})",pendingJobs.size(),executionIds); } else { LOGGER.trace("All execution enrichment jobs were executed"); } } }
void queueJob(final RequestJob job) { if(!job.requiresTermination() && this.terminated) { LOGGER.info("Rejected request job {} for execution {}",job.description(),job.context().targetExecution().executionId()); return; } while(true) { try { this.queue.put(job); LOGGER.trace("Queued job {}",job.description()); break; } catch (final InterruptedException e) { LOGGER.info("Enrichment Requestor interrupted while awaiting for enqueueing job {}",job.description(),e); } } }
private void processJob(final RequestJob job) { final URI executionResource = this.resolver. resolveExecution( job.context().targetExecution()); if(executionResource==null) { retryJob(job); } else { completeJob(job, executionResource); } }
private void completeJob(final RequestJob job, final URI executionResource) { final EnrichmentContext ctx = job.context(); boolean completed=false; try { submitEnrichmentRequest(ctx,UseCase.createRequest(executionResource,ctx)); completed=true; } catch (final Exception e) { LOGGER.error("Could not process {} ({}). Full stacktrace follows",ctx.pendingEnrichment(),executionResource,e); } finally { LOGGER.trace("{} processing job {}",completed?"Completed ":"Failed ",job.description()); EnrichmentRequestor.this.metrics.jobProcessed(job,completed); } }
void enqueueRequest(final EnrichmentContext context) { if(context.requiresCommit()) { final RequestJob job = new RequestJob(context); LOGGER.trace("Created job {}",job.description()); job.retry(1,TimeUnit.SECONDS); } }