mirror of
https://github.com/google/nomulus.git
synced 2025-05-17 01:47:14 +02:00
Add response information to TldFanoutAction
TldFanoutAction now returns an HTTP response detailing the actions it's taking. The format is as follows: OK: Launched the following 3 tasks in queue the-queue - Task: task-a6ad250b-a9d8-427d-bbf7-eb736e6f4dcb, tld: com, endpoint: /the/servlet/com - Task: task-cf6c4bb4-0542-411e-ae4d-723beec09e9c, tld: net, endpoint: /the/servlet/net - Task: task-57899661-fc3f-4049-a265-d6604051406e, tld: org, endpoint: /the/servlet/org ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=152264954
This commit is contained in:
parent
87a9d27299
commit
69cf0d4b55
3 changed files with 50 additions and 7 deletions
|
@ -23,12 +23,14 @@ import static com.google.common.collect.Iterables.concat;
|
|||
import static com.google.common.collect.Iterables.getFirst;
|
||||
import static com.google.common.collect.Multimaps.filterKeys;
|
||||
import static com.google.common.collect.Sets.difference;
|
||||
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
|
||||
import static google.registry.model.registry.Registries.getTldsOfType;
|
||||
import static google.registry.model.registry.Registry.TldType.REAL;
|
||||
import static google.registry.model.registry.Registry.TldType.TEST;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
import com.google.appengine.api.taskqueue.Queue;
|
||||
import com.google.appengine.api.taskqueue.TaskHandle;
|
||||
import com.google.appengine.api.taskqueue.TaskOptions;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableListMultimap;
|
||||
|
@ -38,6 +40,7 @@ import google.registry.request.Action;
|
|||
import google.registry.request.Parameter;
|
||||
import google.registry.request.ParameterMap;
|
||||
import google.registry.request.RequestParameters;
|
||||
import google.registry.request.Response;
|
||||
import google.registry.util.TaskEnqueuer;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
@ -93,6 +96,7 @@ public final class TldFanoutAction implements Runnable {
|
|||
private static final Random random = new Random();
|
||||
|
||||
@Inject TaskEnqueuer taskEnqueuer;
|
||||
@Inject Response response;
|
||||
@Inject @Parameter(ENDPOINT_PARAM) String endpoint;
|
||||
@Inject @Parameter(QUEUE_PARAM) String queue;
|
||||
@Inject @Parameter(FOR_EACH_REAL_TLD_PARAM) boolean forEachRealTld;
|
||||
|
@ -105,15 +109,26 @@ public final class TldFanoutAction implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
Set<String> namespaces = ImmutableSet.copyOf(concat(
|
||||
Set<String> tlds =
|
||||
difference(
|
||||
ImmutableSet.copyOf(
|
||||
concat(
|
||||
runInEmpty ? ImmutableSet.of("") : ImmutableSet.<String>of(),
|
||||
forEachRealTld ? getTldsOfType(REAL) : ImmutableSet.<String>of(),
|
||||
forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.<String>of()));
|
||||
forEachTestTld ? getTldsOfType(TEST) : ImmutableSet.<String>of())),
|
||||
excludes);
|
||||
Multimap<String, String> flowThruParams = filterKeys(params, not(in(CONTROL_PARAMS)));
|
||||
Queue taskQueue = getQueue(queue);
|
||||
for (String namespace : difference(namespaces, excludes)) {
|
||||
taskEnqueuer.enqueue(taskQueue, createTaskOptions(namespace, flowThruParams));
|
||||
String outputPayload = String.format(
|
||||
"OK: Launched the following %d tasks in queue %s\n", tlds.size(), queue);
|
||||
for (String tld : tlds) {
|
||||
TaskOptions taskOptions = createTaskOptions(tld, flowThruParams);
|
||||
TaskHandle taskHandle = taskEnqueuer.enqueue(taskQueue, taskOptions);
|
||||
outputPayload += String.format(
|
||||
"- Task: %s, tld: %s, endpoint: %s\n", taskHandle.getName(), tld, taskOptions.getUrl());
|
||||
}
|
||||
response.setContentType(PLAIN_TEXT_UTF_8);
|
||||
response.setPayload(outputPayload);
|
||||
}
|
||||
|
||||
private TaskOptions createTaskOptions(String tld, Multimap<String, String> params) {
|
||||
|
|
|
@ -17,6 +17,8 @@ java_library(
|
|||
"//javatests/google/registry/testing",
|
||||
"//third_party/java/objectify:objectify-v4_1",
|
||||
"@com_google_appengine_api_1_0_sdk//:testonly",
|
||||
"@com_google_appengine_api_stubs",
|
||||
"@com_google_appengine_testing",
|
||||
"@com_google_guava",
|
||||
"@com_google_truth",
|
||||
"@javax_servlet_api",
|
||||
|
|
|
@ -16,12 +16,15 @@ package google.registry.cron;
|
|||
|
||||
import static com.google.common.collect.Iterables.getLast;
|
||||
import static com.google.common.collect.Lists.transform;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.DatastoreHelper.createTlds;
|
||||
import static google.registry.testing.DatastoreHelper.persistResource;
|
||||
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
|
||||
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
import com.google.appengine.api.taskqueue.dev.QueueStateInfo.TaskStateInfo;
|
||||
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
|
@ -32,9 +35,11 @@ import google.registry.model.registry.Registry;
|
|||
import google.registry.model.registry.Registry.TldType;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.ExceptionRule;
|
||||
import google.registry.testing.FakeResponse;
|
||||
import google.registry.testing.TaskQueueHelper.TaskMatcher;
|
||||
import google.registry.util.Retrier;
|
||||
import google.registry.util.TaskEnqueuer;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
@ -47,6 +52,7 @@ public class TldFanoutActionTest {
|
|||
|
||||
private static final String ENDPOINT = "/the/servlet";
|
||||
private static final String QUEUE = "the-queue";
|
||||
private final FakeResponse response = new FakeResponse();
|
||||
|
||||
@Rule
|
||||
public final ExceptionRule thrown = new ExceptionRule();
|
||||
|
@ -74,7 +80,7 @@ public class TldFanoutActionTest {
|
|||
return params.build();
|
||||
}
|
||||
|
||||
private static void run(ImmutableListMultimap<String, String> params) throws Exception {
|
||||
private void run(ImmutableListMultimap<String, String> params) throws Exception {
|
||||
TldFanoutAction action = new TldFanoutAction();
|
||||
action.params = params;
|
||||
action.endpoint = getLast(params.get("endpoint"));
|
||||
|
@ -83,6 +89,7 @@ public class TldFanoutActionTest {
|
|||
? ImmutableSet.copyOf(Splitter.on(',').split(params.get("exclude").get(0)))
|
||||
: ImmutableSet.<String>of();
|
||||
action.taskEnqueuer = new TaskEnqueuer(new Retrier(null, 1));
|
||||
action.response = response;
|
||||
action.runInEmpty = params.containsKey("runInEmpty");
|
||||
action.forEachRealTld = params.containsKey("forEachRealTld");
|
||||
action.forEachTestTld = params.containsKey("forEachTestTld");
|
||||
|
@ -208,4 +215,23 @@ public class TldFanoutActionTest {
|
|||
assertTasksEnqueued(QUEUE,
|
||||
new TaskMatcher().url("/the/servlet").param("newkey", "newval"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccess_returnHttpResponse() throws Exception {
|
||||
run(getParamsMap("forEachRealTld", "", "endpoint", "/the/servlet/:tld"));
|
||||
|
||||
List<TaskStateInfo> taskList =
|
||||
LocalTaskQueueTestConfig.getLocalTaskQueue().getQueueStateInfo().get(QUEUE).getTaskInfo();
|
||||
|
||||
assertThat(taskList).hasSize(3);
|
||||
String expectedResponse = String.format(
|
||||
"OK: Launched the following 3 tasks in queue the-queue\n"
|
||||
+ "- Task: %s, tld: com, endpoint: /the/servlet/com\n"
|
||||
+ "- Task: %s, tld: net, endpoint: /the/servlet/net\n"
|
||||
+ "- Task: %s, tld: org, endpoint: /the/servlet/org\n",
|
||||
taskList.get(0).getTaskName(),
|
||||
taskList.get(1).getTaskName(),
|
||||
taskList.get(2).getTaskName());
|
||||
assertThat(response.getPayload()).isEqualTo(expectedResponse);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue