com.google.appengine.api.taskqueue.Queue Java Examples

The following examples show how to use com.google.appengine.api.taskqueue.Queue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaskQueuesLogic.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the given task, to be run after the specified time, to the specified queue.
 *
 * @param task the task object containing the details of task to be added
 * @param countdownTime the time delay for the task to be executed
 */
public void addDeferredTask(TaskWrapper task, long countdownTime) {
    Queue requiredQueue = QueueFactory.getQueue(task.getQueueName());
    TaskOptions taskToBeAdded = TaskOptions.Builder.withUrl(task.getWorkerUrl());
    if (countdownTime > 0) {
        taskToBeAdded.countdownMillis(countdownTime);
    }

    for (Map.Entry<String, String[]> entry : task.getParamMap().entrySet()) {
        String name = entry.getKey();
        String[] values = entry.getValue();

        for (String value : values) {
            taskToBeAdded = taskToBeAdded.param(name, value);
        }
    }

    requiredQueue.add(taskToBeAdded);
}
 
Example #2
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValueParams() throws Exception {
    class ParamHandler implements PrintServlet.RequestHandler {
        private String[] paramValues;

        public void handleRequest(ServletRequest req) {
            paramValues = req.getParameterValues("multi_value");
        }
    }

    ParamHandler handler = new ParamHandler();
    PrintServlet.setRequestHandler(handler);

    final Queue queue = QueueFactory.getQueue("tasks-queue");
    waitOnFuture(queue.addAsync(
        withUrl(URL)
            .param("multi_value", "param_value1")
            .param("multi_value", "param_value2")));
    sync();

    assertNotNull(handler.paramValues);
    assertEquals(
        new HashSet<String>(Arrays.asList("param_value1", "param_value2")),
        new HashSet<String>(Arrays.asList(handler.paramValues)));
}
 
Example #3
Source File: WorkerServlet.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  Worker worker = new Worker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example #4
Source File: NordnUploadAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
private void processLordnTasks() throws IOException {
  checkArgument(phase.equals(PARAM_LORDN_PHASE_SUNRISE)
      || phase.equals(PARAM_LORDN_PHASE_CLAIMS),
      "Invalid phase specified to Nordn servlet: %s.", phase);
  DateTime now = clock.nowUtc();
  Queue queue =
      getQueue(
          phase.equals(PARAM_LORDN_PHASE_SUNRISE)
              ? LordnTaskUtils.QUEUE_SUNRISE
              : LordnTaskUtils.QUEUE_CLAIMS);
  String columns = phase.equals(PARAM_LORDN_PHASE_SUNRISE) ? COLUMNS_SUNRISE : COLUMNS_CLAIMS;
  List<TaskHandle> tasks = loadAllTasks(queue, tld);
  if (!tasks.isEmpty()) {
    String csvData = convertTasksToCsv(tasks, now, columns);
    uploadCsvToLordn(String.format("/LORDN/%s/%s", tld, phase), csvData);
    taskQueueUtils.deleteTasks(queue, tasks);
  }
}
 
Example #5
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetry() throws Exception {
    long numTimesToFail = 1;
    String key = "testRetry-" + System.currentTimeMillis();

    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(withUrl("/_ah/retryTest")
        .param("testdata-key", key)
        .param("times-to-fail", String.valueOf(numTimesToFail))
        .retryOptions(RetryOptions.Builder.withTaskRetryLimit(5)));

    String countKey = RetryTestServlet.getInvocationCountKey(key);
    long expectedAttempts = (numTimesToFail + 1);
    long actualAttempts = waitForTestData(countKey, expectedAttempts);
    assertEquals(expectedAttempts, actualAttempts);

    String requestKey1 = RetryTestServlet.getRequestDataKey(key, 1);
    RequestData request1 = waitForTestDataToExist(requestKey1);
    assertEquals("0", request1.getHeader(TASK_RETRY_COUNT));
    assertEquals("0", request1.getHeader(TASK_EXECUTION_COUNT));

    String requestKey2 = RetryTestServlet.getRequestDataKey(key, 2);
    RequestData request2 = waitForTestDataToExist(requestKey2);
    assertEquals("1", request2.getHeader(TASK_RETRY_COUNT));
    assertEquals("1", request2.getHeader(TASK_EXECUTION_COUNT));
}
 
Example #6
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private void leaseAndDeleteTasks(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  int numTasks = Integer.parseInt(req.getParameter("numTasks"));
  Double lease = Double.parseDouble(req.getParameter("lease"));
  String queue = req.getParameter("queue");
  Queue q = QueueFactory.getQueue(queue);
  Boolean doDelete = Boolean.parseBoolean(req.getParameter("doDelete"));

  List<TaskHandle> tasks = q.leaseTasks(lease.intValue() * 1000, TimeUnit.MILLISECONDS, numTasks);

  for (TaskHandle task : tasks) {
    if (doDelete) {
      q.deleteTask(task.getName());
    }
  }
  resp.getWriter().print(queue + "," + tasks.size());
}
 
Example #7
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryLimitIsHonored() throws Exception {
    long numTimesToFail = 10;
    int retryLimit = 2;
    String key = "testRetryLimitIsHonored-" + System.currentTimeMillis();

    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(withUrl("/_ah/retryTest")
        .param("testdata-key", key)
        .param("times-to-fail", String.valueOf(numTimesToFail))
        .retryOptions(RetryOptions.Builder.withTaskRetryLimit(retryLimit)));

    long expectedAttempts = (long) (retryLimit + 1);
    String countKey = RetryTestServlet.getInvocationCountKey(key);
    Long actualAttempts = waitForTestData(countKey, expectedAttempts);

    // Ideally this would be the assert, but when a task fails with 500, the test framework
    // cannot capture it for the attempt count.
    // assertEquals(expectedAttempts, actualAttempts);

    // Allow room for one task to fail with 500.
    assertTrue("Task retries lower than specified via withTaskRetryLimit()",
        actualAttempts == expectedAttempts || actualAttempts == expectedAttempts - 1);
}
 
Example #8
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testParams() throws Exception {
    class ParamHandler implements PrintServlet.RequestHandler {
        private String paramValue;

        public void handleRequest(ServletRequest req) {
            paramValue = req.getParameter("single_value");
        }
    }

    ParamHandler handler = new ParamHandler();
    PrintServlet.setRequestHandler(handler);

    final Queue queue = QueueFactory.getQueue("tasks-queue");
    waitOnFuture(queue.addAsync(withUrl(URL).param("single_value", "param_value")));
    sync();

    assertEquals("param_value", handler.paramValue);
}
 
Example #9
Source File: SmokeTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasics() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    sync(2000L);
    TaskHandle th = queue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL).param("foo", "bar".getBytes()));
    try {
        List<TaskHandle> handles = queue.leaseTasks(30, TimeUnit.MINUTES, 100);
        Assert.assertFalse(handles.isEmpty());
        Assert.assertEquals(1, handles.size());
        TaskHandle lh = handles.get(0);
        Assert.assertEquals(th.getName(), lh.getName());
        sync(5000L);
    } finally {
        queue.deleteTask(th);
    }
}
 
Example #10
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiValueParams() throws Exception {
    class ParamHandler implements PrintServlet.RequestHandler {
        private String[] paramValues;

        public void handleRequest(ServletRequest req) {
            paramValues = req.getParameterValues("multi_value");
        }
    }

    ParamHandler handler = new ParamHandler();
    PrintServlet.setRequestHandler(handler);

    final Queue queue = QueueFactory.getQueue("tasks-queue");
    queue.add(
        withUrl(URL)
            .param("multi_value", "param_value1")
            .param("multi_value", "param_value2"));
    sync();

    assertNotNull(handler.paramValues);
    assertEquals(
        new HashSet<>(Arrays.asList("param_value1", "param_value2")),
        new HashSet<>(Arrays.asList(handler.paramValues)));
}
 
Example #11
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testTaskHandleContainsAllNecessaryProperties() throws Exception {
    String name = "testTaskHandleContainsAllNecessaryProperties-" + System.currentTimeMillis();
    Queue queue = QueueFactory.getDefaultQueue();

    TaskOptions options = withTaskName(name).payload("payload");
    options.etaMillis(0); // TODO -- remove this once NPE is fixewd

    TaskHandle handle = queue.add(options);

    assertEquals("default", handle.getQueueName());
    assertEquals(name, handle.getName());
    assertEquals("payload", new String(handle.getPayload(), "UTF-8"));
    assertNotNull(handle.getEtaMillis());
    assertEquals(0, (int) handle.getRetryCount());
}
 
Example #12
Source File: TaskQueueUtilsTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteTasks_usesMultipleBatches() {
  Queue defaultQ = QueueFactory.getQueue("default");
  TaskOptions taskOptA = withUrl("/a").taskName("a");
  TaskOptions taskOptB = withUrl("/b").taskName("b");
  TaskOptions taskOptC = withUrl("/c").taskName("c");
  taskQueueUtils.enqueue(defaultQ, ImmutableList.of(taskOptA, taskOptB, taskOptC));
  assertThat(getQueueInfo("default").getTaskInfo()).hasSize(3);

  taskQueueUtils.deleteTasks(
      defaultQ,
      ImmutableList.of(
          new TaskHandle(taskOptA, "default"),
          new TaskHandle(taskOptB, "default"),
          new TaskHandle(taskOptC, "default")));
  assertThat(getQueueInfo("default").getTaskInfo()).hasSize(0);
}
 
Example #13
Source File: PushNotificationWorkerServlet.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  PushNotificationWorker worker = new PushNotificationWorker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example #14
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmoke() throws Exception {
    final Queue queue = QueueFactory.getQueue("tasks-queue");
    waitOnFuture(queue.addAsync(withUrl(URL)));
    sync();
    assertNotNull(PrintServlet.getLastRequest());
}
 
Example #15
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnlyPullTasksCanHaveTag() {
    Queue pullQueue = QueueFactory.getQueue("pull-queue");
    pullQueue.add(withMethod(PULL).tag("foo"));

    Queue pushQueue = QueueFactory.getDefaultQueue();
    try {
        pushQueue.add(withTag("foo"));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // pass
    }
}
 
Example #16
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskWithoutUrlIsSubmittedToDefaultUrl() throws Exception {
    Queue defaultQueue = QueueFactory.getDefaultQueue();
    waitOnFuture(defaultQueue.addAsync(withMethod(POST)));
    sync();
    assertTrue("DefaultQueueServlet was not invoked", DefaultQueueServlet.wasInvoked());

    Queue testQueue = QueueFactory.getQueue("test");
    waitOnFuture(testQueue.addAsync(withMethod(POST)));
    sync();
    assertTrue("TestQueueServlet was not invoked", TestQueueServlet.wasInvoked());
}
 
Example #17
Source File: SubscriptionUtility.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueues subscription ids in task queue for deletion.
 *
 * @param subIds Psi subscription ids to be deleted
 */
protected static void enqueueDeletePsiSubscription(String[] subIds) {
  Queue deviceTokenCleanupQueue = QueueFactory.getQueue("subscription-removal");
  deviceTokenCleanupQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/devicesubscription/delete")
      .param("subIds", new Gson().toJson(subIds, String[].class))
      .param("type", SubscriptionUtility.REQUEST_TYPE_PSI_SUB));
}
 
Example #18
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaders() throws Exception {
    Queue queue = QueueFactory.getDefaultQueue();
    waitOnFuture(queue.addAsync(withHeader("header_key", "header_value")));
    sync();

    RequestData lastRequest = DefaultQueueServlet.getLastRequest();
    assertEquals("header_value", lastRequest.getHeader("header_key"));
}
 
Example #19
Source File: TaskQueueTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test(expected = TaskAlreadyExistsException.class)
public void testAddingTwoTasksWithSameNameThrowsException() {
    String taskName = "sameName";
    Queue queue = getDefaultQueue();
    // TODO -- perhaps change this with delay on servlet side?
    queue.add(TaskOptions.Builder.withTaskName(taskName).countdownMillis(10 * 1000L));
    queue.add(TaskOptions.Builder.withTaskName(taskName));
}
 
Example #20
Source File: DeviceSubscription.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueues device subscription entity to be deleted.
 *
 * @param time Threshold time before which entities created will be deleted
 * @param cursor Query cursor indicates query result position
 */
protected void enqueueDeleteDeviceSubscription(Date time, String cursor) {
  Queue deviceTokenCleanupQueue = QueueFactory.getQueue("subscription-removal");
  deviceTokenCleanupQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/devicesubscription/delete")
      .param("timeStamp", new Gson().toJson(time, Date.class))
      .param("cursor", cursor)
      .param("type", SubscriptionUtility.REQUEST_TYPE_DEVICE_SUB));
}
 
Example #21
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPullParams() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th = queue.add(withMethod(PULL).param("foo", "bar").etaMillis(15000));
    try {
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksAsync(30, TimeUnit.MINUTES, 100));
        assertFalse(handles.isEmpty());
        TaskHandle lh = handles.get(0);
        assertEquals(th.getName(), lh.getName());
    } finally {
        queue.deleteTask(th);
    }
}
 
Example #22
Source File: DeviceSubscription.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueues device subscription entity to be deleted.
 *
 * @param time Threshold time before which entities created will be deleted
 * @param cursor Query cursor indicates query result position
 */
protected void enqueueDeleteDeviceSubscription(Date time, String cursor) {
  Queue deviceTokenCleanupQueue = QueueFactory.getQueue("subscription-removal");
  deviceTokenCleanupQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/devicesubscription/delete")
      .param("timeStamp", new Gson().toJson(time, Date.class))
      .param("cursor", cursor)
      .param("type", SubscriptionUtility.REQUEST_TYPE_DEVICE_SUB));
}
 
Example #23
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void assertServletReceivesCorrectMethod(TaskOptions.Method method) {
    MethodRequestHandler handler = new MethodRequestHandler();
    PrintServlet.setRequestHandler(handler);

    Queue queue = QueueFactory.getQueue("tasks-queue");
    waitOnFuture(queue.addAsync(withUrl(URL).method(method)));
    sync();

    assertEquals("Servlet received invalid HTTP method.", method.name(), handler.method);
}
 
Example #24
Source File: QueueTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
/**
 * Purge queues, then wait so purge call doesn't overlap with tests.
 *
 * @param queues to be purged.
 */
protected void purgeAndPause(Queue... queues) {
    for (Queue queue : queues) {
        queue.purge();
    }

    sync(3000);
}
 
Example #25
Source File: StatsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
/**
 * Because the stats processing is approximate, this test only verifies the basics.
 * The dev_appserver generates random statistics.
 */
@Test
public void testStatsApiBasic() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    QueueStatistics stats = queue.fetchStatistics();
    Assert.assertNotNull(stats);
    Assert.assertEquals("pull-queue", stats.getQueueName());
    Assert.assertTrue(stats.getEnforcedRate() >= 0);

    Assert.assertTrue(stats.getNumTasks() >= 0);
    Assert.assertTrue(stats.getExecutedLastMinute() >= 0);
    Assert.assertTrue(stats.getOldestEtaUsec() == null || stats.getOldestEtaUsec() >= 0);
    Assert.assertTrue(stats.getRequestsInFlight() >= 0);
}
 
Example #26
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnlyPullTasksCanBeAddedToPullQueue() {
    Queue pullQueue = QueueFactory.getQueue("pull-queue");
    pullQueue.add(withMethod(PULL));
    assertAddThrowsExceptionForMethod(DELETE, pullQueue);
    assertAddThrowsExceptionForMethod(GET, pullQueue);
    assertAddThrowsExceptionForMethod(HEAD, pullQueue);
    assertAddThrowsExceptionForMethod(PUT, pullQueue);
    assertAddThrowsExceptionForMethod(POST, pullQueue);
}
 
Example #27
Source File: CheckInEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
private void pushPersonalizedOffers(String placeId, User user) {
  // insert a task to a queue
  log.info("adding a task to recommendations-queue");
  Queue queue = QueueFactory.getQueue("recommendations-queue");

  try {
    String userEmail = user.getEmail();
    queue.add(withUrl("/tasks/recommendations")
        .param("userEmail", userEmail).param("placeId", placeId));
    log.info("task added");
  } catch (RuntimeException e) {
    log.severe(e.getMessage());
  }
}
 
Example #28
Source File: FanoutTask.java    From yawp with MIT License 5 votes vote down vote up
private void fanout() {
    Queue queue = QueueHelper.getPipeForkQueue(pipe);

    for (IdRef<?> sinkId : getSinkIds()) {
        payload.setSinkUri(sinkId);
        queue.add(TaskOptions.Builder.withPayload(new ForkTask(payload)));
    }
}
 
Example #29
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private void addPullTasks(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  int numTasks = Integer.parseInt(req.getParameter("numTasks"));
  String queue = req.getParameter("queue");
  Queue q = QueueFactory.getQueue(queue);

  List<TaskOptions> options = new ArrayList<TaskOptions>();
  for (int i = 0; i < numTasks; i++) {
    options.add(
        TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
            .payload(String.format("payload-%03d", i).getBytes()));
  }
  List<TaskHandle> tasks = q.add(options);
  resp.getWriter().print(queue + "," + tasks.size());
}
 
Example #30
Source File: PullTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeaseTasksOnlyReturnsSpecifiedNumberOfTasks() {
    Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th1 = queue.add(withMethod(PULL));
    TaskHandle th2 = queue.add(withMethod(PULL));
    sync();
    try {
        int countLimit = 1;
        List<TaskHandle> handles = queue.leaseTasks(10, TimeUnit.SECONDS, countLimit);
        assertEquals(countLimit, handles.size());
    } finally {
        queue.deleteTask(th1);
        queue.deleteTask(th2);
    }
}