Search in sources :

Example 1 with TaskQueueQueryAndOwnTasksResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testQueryAndOwnTasks.

@Test
public void testQueryAndOwnTasks() throws Exception {
    class MockClock implements Clock {

        @Override
        public long getCurrentTime() {
            // Force nowMillis of QueryAndOwnTasks is guaranteed to be bigger than task eta.
            return Clock.DEFAULT.getCurrentTime() + 1000;
        }
    }
    initLocalTaskQueue(new MockClock());
    TaskQueueBulkAddResponse sbresponse = localService.bulkAdd(new Status(), bulkAddPullRequest.build());
    assertThat(sbresponse).isEqualTo(expectedBulkAddResponse.build());
    TaskQueueQueryAndOwnTasksRequest request = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8("pull-queue")).setLeaseSeconds(10).setMaxTasks(5).build();
    TaskQueueQueryAndOwnTasksResponse response = localService.queryAndOwnTasks(new Status(), request);
    assertThat(response.getTaskCount()).isEqualTo(3);
    for (int i = 0; i < response.getTaskCount(); ++i) {
        assertThat(response.getTask(i).getTaskName().toStringUtf8()).isEqualTo("a-task-" + i);
        assertThat(response.getTask(i).getBody().toStringUtf8()).isEqualTo("payload" + i);
    }
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) TaskQueueQueryAndOwnTasksRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest) Clock(com.google.appengine.tools.development.Clock) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) Test(org.junit.Test)

Example 2 with TaskQueueQueryAndOwnTasksResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse in project appengine-java-standard by GoogleCloudPlatform.

the class QueueImpl method leaseTasksInternal.

private Future<List<TaskHandle>> leaseTasksInternal(LeaseOptions options) {
    long leaseMillis = options.getUnit().toMillis(options.getLease());
    if (leaseMillis > QueueConstants.maxLease(MILLISECONDS)) {
        throw new IllegalArgumentException(String.format("A lease period can be no longer than %d seconds", QueueConstants.maxLease(SECONDS)));
    }
    if (options.getCountLimit() > QueueConstants.maxLeaseCount()) {
        throw new IllegalArgumentException(String.format("No more than %d tasks can be leased in one call", QueueConstants.maxLeaseCount()));
    }
    TaskQueueQueryAndOwnTasksRequest.Builder leaseRequest = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8(queueName)).setLeaseSeconds(leaseMillis / 1000.0).setMaxTasks(options.getCountLimit());
    if (options.getGroupByTag()) {
        // You can groupByTag with a null tag. This means "return tasks grouped by the same
        // tag as the task of minimum eta".
        leaseRequest.setGroupByTag(true);
        if (options.getTag() != null) {
            leaseRequest.setTag(ByteString.copyFrom(options.getTag()));
        }
    }
    ApiConfig apiConfig = new ApiConfig();
    if (options.getDeadlineInSeconds() == null) {
        apiConfig.setDeadlineInSeconds(DEFAULT_LEASE_TASKS_DEADLINE_SECONDS);
    } else {
        apiConfig.setDeadlineInSeconds(options.getDeadlineInSeconds());
    }
    Future<TaskQueueQueryAndOwnTasksResponse> responseFuture = apiHelper.makeAsyncCall("QueryAndOwnTasks", leaseRequest.build(), TaskQueueQueryAndOwnTasksResponse.getDefaultInstance(), apiConfig);
    return new FutureAdapter<TaskQueueQueryAndOwnTasksResponse, List<TaskHandle>>(responseFuture) {

        @Override
        protected List<TaskHandle> wrap(TaskQueueQueryAndOwnTasksResponse leaseResponse) {
            List<TaskHandle> result = new ArrayList<>();
            for (TaskQueueQueryAndOwnTasksResponse.Task response : leaseResponse.getTaskList()) {
                TaskOptions taskOptions = TaskOptions.Builder.withTaskName(response.getTaskName().toStringUtf8()).payload(response.getBody().toByteArray()).method(TaskOptions.Method.PULL);
                if (response.hasTag()) {
                    taskOptions.tag(response.getTag().toByteArray());
                }
                TaskHandle handle = new TaskHandle(taskOptions, queueName, response.getRetryCount());
                result.add(handle.etaUsec(response.getEtaUsec()));
            }
            return result;
        }
    };
}
Also used : ArrayList(java.util.ArrayList) ApiConfig(com.google.apphosting.api.ApiProxy.ApiConfig) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) TaskQueueQueryAndOwnTasksRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest)

Example 3 with TaskQueueQueryAndOwnTasksResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testExtendTaskLease.

@Test
public void testExtendTaskLease() throws Exception {
    // QueryAndOwn tasks first, same as testQueryAndOwnTasks above
    class MockClock implements Clock {

        @Override
        public long getCurrentTime() {
            // So that nowMillis of QueryAndOwnTasks is guaranteed to be bigger than task eta.
            return Clock.DEFAULT.getCurrentTime() + 1000;
        }
    }
    initLocalTaskQueue(new MockClock());
    TaskQueueBulkAddResponse sbresponse = localService.bulkAdd(new Status(), bulkAddPullRequest.build());
    assertThat(sbresponse).isEqualTo(expectedBulkAddResponse.build());
    TaskQueueQueryAndOwnTasksRequest request = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8("pull-queue")).setLeaseSeconds(60).setMaxTasks(5).build();
    TaskQueueQueryAndOwnTasksResponse response = localService.queryAndOwnTasks(new Status(), request);
    assertThat(response.getTaskCount()).isEqualTo(3);
    for (int i = 0; i < response.getTaskCount(); ++i) {
        TaskQueueModifyTaskLeaseRequest extendRequest = TaskQueueModifyTaskLeaseRequest.newBuilder().setQueueName(ByteString.copyFromUtf8("pull-queue")).setTaskName(response.getTask(i).getTaskName()).setEtaUsec(response.getTask(i).getEtaUsec()).setLeaseSeconds(300).build();
        TaskQueueModifyTaskLeaseResponse extendResponse = localService.modifyTaskLease(new Status(), extendRequest);
        assertThat(extendResponse.getUpdatedEtaUsec()).isGreaterThan(response.getTask(i).getEtaUsec());
    }
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueModifyTaskLeaseResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseResponse) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) TaskQueueQueryAndOwnTasksRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest) Clock(com.google.appengine.tools.development.Clock) TaskQueueModifyTaskLeaseRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseRequest) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) Test(org.junit.Test)

Example 4 with TaskQueueQueryAndOwnTasksResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testQueryAndOwnTasksWithUnspecifiedTag.

@Test
public void testQueryAndOwnTasksWithUnspecifiedTag() throws Exception {
    class MockClock implements Clock {

        @Override
        public long getCurrentTime() {
            // Force nowMillis of QueryAndOwnTasks is guaranteed to be bigger than task eta.
            return Clock.DEFAULT.getCurrentTime() + 1000;
        }
    }
    initLocalTaskQueue(new MockClock());
    TaskQueueBulkAddResponse sbresponse = localService.bulkAdd(new Status(), bulkAddPullRequest.build());
    assertThat(sbresponse).isEqualTo(expectedBulkAddResponse.build());
    TaskQueueQueryAndOwnTasksRequest request = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8("pull-queue")).setLeaseSeconds(10).setMaxTasks(5).setGroupByTag(true).build();
    TaskQueueQueryAndOwnTasksResponse response = localService.queryAndOwnTasks(new Status(), request);
    assertThat(response.getTaskCount()).isEqualTo(2);
    assertThat(response.getTask(0).getTaskName().toStringUtf8()).isEqualTo("a-task-0");
    assertThat(response.getTask(0).getBody().toStringUtf8()).isEqualTo("payload0");
    assertThat(response.getTask(0).getTag().toStringUtf8()).isEqualTo("tag");
    assertThat(response.getTask(1).getTaskName().toStringUtf8()).isEqualTo("a-task-2");
    assertThat(response.getTask(1).getBody().toStringUtf8()).isEqualTo("payload2");
    assertThat(response.getTask(1).getTag().toStringUtf8()).isEqualTo("tag");
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) TaskQueueQueryAndOwnTasksRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest) Clock(com.google.appengine.tools.development.Clock) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) Test(org.junit.Test)

Example 5 with TaskQueueQueryAndOwnTasksResponse

use of com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse in project appengine-java-standard by GoogleCloudPlatform.

the class LocalTaskQueueTest method testQueryAndOwnTasksWithTags.

@Test
public void testQueryAndOwnTasksWithTags() throws Exception {
    class MockClock implements Clock {

        @Override
        public long getCurrentTime() {
            // Force nowMillis of QueryAndOwnTasks is guaranteed to be bigger than task eta.
            return Clock.DEFAULT.getCurrentTime() + 1000;
        }
    }
    initLocalTaskQueue(new MockClock());
    TaskQueueBulkAddResponse sbresponse = localService.bulkAdd(new Status(), bulkAddPullRequest.build());
    assertThat(sbresponse).isEqualTo(expectedBulkAddResponse.build());
    TaskQueueQueryAndOwnTasksRequest request = TaskQueueQueryAndOwnTasksRequest.newBuilder().setQueueName(ByteString.copyFromUtf8("pull-queue")).setLeaseSeconds(10).setMaxTasks(5).setGroupByTag(true).setTag(ByteString.copyFromUtf8("tag")).build();
    TaskQueueQueryAndOwnTasksResponse response = localService.queryAndOwnTasks(new Status(), request);
    assertThat(response.getTaskCount()).isEqualTo(2);
    assertThat(response.getTask(0).getTaskName().toStringUtf8()).isEqualTo("a-task-0");
    assertThat(response.getTask(0).getBody().toStringUtf8()).isEqualTo("payload0");
    assertThat(response.getTask(0).getTag().toStringUtf8()).isEqualTo("tag");
    assertThat(response.getTask(1).getTaskName().toStringUtf8()).isEqualTo("a-task-2");
    assertThat(response.getTask(1).getBody().toStringUtf8()).isEqualTo("payload2");
    assertThat(response.getTask(1).getTag().toStringUtf8()).isEqualTo("tag");
}
Also used : Status(com.google.appengine.tools.development.LocalRpcService.Status) TaskQueueBulkAddResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse) TaskQueueQueryAndOwnTasksRequest(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest) Clock(com.google.appengine.tools.development.Clock) TaskQueueQueryAndOwnTasksResponse(com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse) Test(org.junit.Test)

Aggregations

TaskQueueQueryAndOwnTasksResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse)6 TaskQueueQueryAndOwnTasksRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest)5 TaskQueueBulkAddResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse)4 Clock (com.google.appengine.tools.development.Clock)4 Status (com.google.appengine.tools.development.LocalRpcService.Status)4 Test (org.junit.Test)4 TaskQueueAddRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest)1 TaskQueueModifyTaskLeaseRequest (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseRequest)1 TaskQueueModifyTaskLeaseResponse (com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseResponse)1 LatencyPercentiles (com.google.appengine.tools.development.LatencyPercentiles)1 ApiConfig (com.google.apphosting.api.ApiProxy.ApiConfig)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1