/** * Perform a batch poll for tasks by task type. Batch size is configurable by count. * Returns an iterator that streams tasks as they become available through GRPC. * * @param taskType Type of task to poll for * @param workerId Name of the client worker. Used for logging. * @param count Maximum number of tasks to be returned. Actual number of tasks returned can be less than this number. * @param timeoutInMillisecond Long poll wait timeout. * @return Iterator of tasks awaiting to be executed. */ public Iterator<Task> batchPollTasksByTaskTypeAsync(String taskType, String workerId, int count, int timeoutInMillisecond) { Preconditions.checkArgument(StringUtils.isNotBlank(taskType), "Task type cannot be blank"); Preconditions.checkArgument(StringUtils.isNotBlank(workerId), "Worker id cannot be blank"); Preconditions.checkArgument(count > 0, "Count must be greater than 0"); Iterator<TaskPb.Task> it = stub.batchPoll( TaskServicePb.BatchPollRequest.newBuilder() .setTaskType(taskType) .setWorkerId(workerId) .setCount(count) .setTimeout(timeoutInMillisecond) .build() ); return Iterators.transform(it, protoMapper::fromProto); }
@Override public void batchPoll(TaskServicePb.BatchPollRequest req, StreamObserver<TaskPb.Task> response) { final int count = GRPC_HELPER.optionalOr(req.getCount(), 1); final int timeout = GRPC_HELPER.optionalOr(req.getTimeout(), POLL_TIMEOUT_MS); if (timeout > MAX_POLL_TIMEOUT_MS) { response.onError(Status.INVALID_ARGUMENT .withDescription("longpoll timeout cannot be longer than " + MAX_POLL_TIMEOUT_MS + "ms") .asRuntimeException() ); return; } try { List<Task> polledTasks = taskService.batchPoll(req.getTaskType(), req.getWorkerId(), GRPC_HELPER.optional(req.getDomain()), count, timeout); LOGGER.info("polled tasks: "+polledTasks); polledTasks.stream().map(PROTO_MAPPER::toProto).forEach(response::onNext); response.onCompleted(); } catch (Exception e) { GRPC_HELPER.onError(response, e); } }