use of org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.SEND_REQUEST in project mod-oai-pmh by folio-org.
the class MarcWithHoldingsRequestHelper method handle.
/**
* Handle MarcWithHoldings request
*/
@Override
public Future<Response> handle(Request request, Context vertxContext) {
Promise<Response> oaipmhResponsePromise = Promise.promise();
metricsCollectingService.startMetric(request.getRequestId(), SEND_REQUEST);
try {
String resumptionToken = request.getResumptionToken();
List<OAIPMHerrorType> errors = validateListRequest(request);
if (!errors.isEmpty()) {
return buildResponseWithErrors(request, oaipmhResponsePromise, errors);
}
String requestId;
OffsetDateTime lastUpdateDate = OffsetDateTime.now(ZoneId.systemDefault());
RequestMetadataLb requestMetadata = new RequestMetadataLb().setLastUpdatedDate(lastUpdateDate);
Future<RequestMetadataLb> updateRequestMetadataFuture;
if (resumptionToken == null) {
requestId = request.getRequestId();
requestMetadata.setRequestId(UUID.fromString(requestId));
updateRequestMetadataFuture = instancesService.saveRequestMetadata(requestMetadata, request.getTenant());
} else {
requestId = request.getRequestId();
updateRequestMetadataFuture = instancesService.updateRequestUpdatedDate(requestId, lastUpdateDate, request.getTenant());
}
updateRequestMetadataFuture.onSuccess(res -> {
boolean isFirstBatch = resumptionToken == null;
processBatch(request, vertxContext, oaipmhResponsePromise, requestId, isFirstBatch);
if (isFirstBatch) {
saveInstancesExecutor.executeBlocking(downloadInstancesPromise -> downloadInstances(request, oaipmhResponsePromise, downloadInstancesPromise, downloadContext), downloadInstancesResult -> {
instancesService.updateRequestStreamEnded(requestId, true, request.getTenant());
if (downloadInstancesResult.succeeded()) {
logger.info("Downloading instances complete.");
} else {
logger.error("Downloading instances was canceled due to the error. ", downloadInstancesResult.cause());
if (!oaipmhResponsePromise.future().isComplete()) {
oaipmhResponsePromise.fail(new IllegalStateException(downloadInstancesResult.cause()));
}
}
});
}
}).onFailure(th -> handleException(oaipmhResponsePromise, th));
} catch (Exception e) {
handleException(oaipmhResponsePromise, e);
}
return oaipmhResponsePromise.future().onComplete(responseAsyncResult -> metricsCollectingService.endMetric(request.getRequestId(), SEND_REQUEST));
}
Aggregations