int maxSleepMillis = 1;
while (remainingNanos > 0) {
// todo we need a much better way to determine if a task is unknown (e.g. not scheduled yet), done, or there is current no more data
try {
BufferResult result = taskManager.getTaskResults(taskId, outputId, token, DEFAULT_MAX_SIZE, new Duration(remainingNanos, NANOSECONDS));
List<Page> pages = result.getPages();
if (!pages.isEmpty()) {
GenericEntity<?> entity = new GenericEntity<>(pages, new TypeToken<List<Page>>() {}.getType());
return Response.ok(entity)
.header(PRESTO_PAGE_TOKEN, result.getToken())
.header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken())
.build();
}
else if (result.isBufferClosed()) {
return Response.status(Status.GONE)
.header(PRESTO_PAGE_TOKEN, result.getToken())
.header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken())
.build();
}
else {
return Response.status(Status.NO_CONTENT)
.header(PRESTO_PAGE_TOKEN, result.getToken())
.header(PRESTO_PAGE_NEXT_TOKEN, result.getNextToken())
.build();
}
}
catch (NoSuchElementException | NoSuchBufferException ignored) {
}