Skip to content

Commit e4ee63b

Browse files
authored
Make Cloud Tasks Utils canary-aware (#2639)
1 parent f8407c7 commit e4ee63b

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

core/src/main/java/google/registry/batch/CloudTasksUtils.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkState;
1919
import static com.google.common.collect.ImmutableList.toImmutableList;
20+
import static google.registry.config.RegistryConfig.CANARY_HEADER;
2021
import static java.util.concurrent.TimeUnit.SECONDS;
2122

2223
import com.google.api.gax.rpc.ApiException;
@@ -190,6 +191,9 @@ protected Task createTask(
190191
requestBuilder.setOidcToken(oidcTokenBuilder.build());
191192
String totalPath = String.format("%s%s", service.getServiceUrl(), path);
192193
requestBuilder.setUrl(totalPath);
194+
if (RegistryEnvironment.isCanary()) {
195+
requestBuilder.putHeaders(CANARY_HEADER, "true");
196+
}
193197
return Task.newBuilder().setHttpRequest(requestBuilder.build()).build();
194198
}
195199

@@ -200,7 +204,7 @@ protected Task createTask(
200204
* default service account as the principal. That account must have permission to submit tasks to
201205
* Cloud Tasks.
202206
*
203-
* <p>Prefer this overload over the one where the path and service are explicit defined, as this
207+
* <p>Prefer this overload over the one where the path and service are explicitly defined, as this
204208
* class will automatically determine the service to use based on the action and the runtime.
205209
*
206210
* @param actionClazz the action class to run, must be annotated with {@link Action}.
@@ -269,7 +273,7 @@ public Task createTaskWithJitter(
269273
/**
270274
* Create a {@link Task} to be enqueued with a random delay up to {@code jitterSeconds}.
271275
*
272-
* <p>Prefer this overload over the one where the path and service are explicit defined, as this
276+
* <p>Prefer this overload over the one where the path and service are explicitly defined, as this
273277
* class will automatically determine the service to use based on the action and the runtime.
274278
*
275279
* @param actionClazz the action class to run, must be annotated with {@link Action}.
@@ -306,7 +310,7 @@ public Task createTaskWithJitter(
306310
* @param service the GAE/GKE service to route the request to.
307311
* @param params a multimap of URL query parameters. Duplicate keys are saved as is, and it is up
308312
* to the server to process the duplicate keys.
309-
* @param delay the amount of time that a task needs to delayed for.
313+
* @param delay the amount of time that a task needs to be delayed for.
310314
* @return the enqueued task.
311315
* @see <a
312316
* href=ttps://cloud.google.com/appengine/docs/standard/java/taskqueue/push/creating-tasks#target>Specifyinig
@@ -330,14 +334,14 @@ private Task createTaskWithDelay(
330334
/**
331335
* Create a {@link Task} to be enqueued with delay of {@code duration}.
332336
*
333-
* <p>Prefer this overload over the one where the path and service are explicit defined, as this
337+
* <p>Prefer this overload over the one where the path and service are explicitly defined, as this
334338
* class will automatically determine the service to use based on the action and the runtime.
335339
*
336340
* @param actionClazz the action class to run, must be annotated with {@link Action}.
337341
* @param method the HTTP method to be used for the request.
338342
* @param params a multimap of URL query parameters. Duplicate keys are saved as is, and it is up
339343
* to the server to process the duplicate keys.
340-
* @param delay the amount of time that a task needs to delayed for.
344+
* @param delay the amount of time that a task needs to be delayed for.
341345
* @return the enqueued task.
342346
* @see <a
343347
* href=ttps://cloud.google.com/appengine/docs/standard/java/taskqueue/push/creating-tasks#target>Specifyinig

core/src/main/java/google/registry/config/RegistryConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
*/
7272
public final class RegistryConfig {
7373

74+
public static final String CANARY_HEADER = "canary";
7475
private static final String ENVIRONMENT_CONFIG_FORMAT = "files/nomulus-config-%s.yaml";
7576
private static final String YAML_CONFIG_PROD =
7677
readResourceUtf8(RegistryConfig.class, "files/default-config.yaml");

core/src/main/java/google/registry/tools/ServiceConnection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.net.HttpHeaders.X_REQUESTED_WITH;
2121
import static com.google.common.net.MediaType.JSON_UTF_8;
2222
import static google.registry.config.ConfigUtils.makeUrl;
23+
import static google.registry.config.RegistryConfig.CANARY_HEADER;
2324
import static google.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
2425
import static java.nio.charset.StandardCharsets.UTF_8;
2526

@@ -58,8 +59,6 @@ public class ServiceConnection {
5859
/** Pattern to heuristically extract title tag contents in HTML responses. */
5960
protected static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>");
6061

61-
private static final String CANARY_HEADER = "canary";
62-
6362
private final Service service;
6463
private final boolean useCanary;
6564
private final HttpRequestFactory requestFactory;

util/src/main/java/google/registry/util/RegistryEnvironment.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ public enum RegistryEnvironment {
5858
*/
5959
private static final String JETTY_PROPERTY = "google.registry.jetty";
6060

61+
/** Name of the environmental variable of the container name. */
62+
private static final String CONTAINER_ENV = "CONTAINER_NAME";
63+
6164
private static final boolean ON_JETTY =
6265
Boolean.parseBoolean(System.getProperty(JETTY_PROPERTY, "false"));
6366

67+
private static final boolean IS_CANARY =
68+
System.getenv().getOrDefault(CONTAINER_ENV, "").endsWith("-canary");
69+
6470
/**
6571
* A thread local boolean that can be set in tests to indicate some code is running in a local
6672
* test server.
@@ -98,6 +104,10 @@ public static boolean isOnJetty() {
98104
return ON_JETTY;
99105
}
100106

107+
public static boolean isCanary() {
108+
return IS_CANARY;
109+
}
110+
101111
public static void setIsInTestDriver(boolean value) {
102112
checkState(RegistryEnvironment.get() == RegistryEnvironment.UNITTEST);
103113
IN_TEST_SERVER.set(value);

0 commit comments

Comments
 (0)