mirror of
https://github.com/google/nomulus.git
synced 2025-07-06 03:03:34 +02:00
Make a few quality-of-life improvements in CloudTasksUtils (#1521)
* Make a few quality-of-life improvements in CloudTasksUtils 1. Update the method names. There are too many overloaded methods and it is hard to figure out which one does which without checking the javadoc. 2. Added a method in the task matcher to specify the delay time in DateTime, so the caller does not need to convert it to Timestamp. 3. Remove the expilict dependency on a clock when enqueueing a task with delay, the clock is now injected directly into the util instance itself.
This commit is contained in:
parent
88f274a601
commit
d355da362f
31 changed files with 119 additions and 123 deletions
|
@ -96,7 +96,7 @@ public final class CommitLogCheckpointAction implements Runnable {
|
|||
// Enqueue a diff task between previous and current checkpoints.
|
||||
cloudTasksUtils.enqueue(
|
||||
QUEUE_NAME,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
ExportCommitLogDiffAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(
|
||||
|
|
|
@ -304,7 +304,7 @@ public class RdeIO {
|
|||
if (key.mode() == RdeMode.FULL) {
|
||||
cloudTasksUtils.enqueue(
|
||||
RDE_UPLOAD_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
RdeUploadAction.PATH,
|
||||
Service.BACKEND.getServiceId(),
|
||||
ImmutableMultimap.of(
|
||||
|
@ -315,7 +315,7 @@ public class RdeIO {
|
|||
} else {
|
||||
cloudTasksUtils.enqueue(
|
||||
BRDA_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
BrdaCopyAction.PATH,
|
||||
Service.BACKEND.getServiceId(),
|
||||
ImmutableMultimap.of(
|
||||
|
|
|
@ -21,6 +21,7 @@ import dagger.Module;
|
|||
import dagger.Provides;
|
||||
import google.registry.config.CredentialModule.DefaultCredential;
|
||||
import google.registry.config.RegistryConfig.Config;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.CloudTasksUtils;
|
||||
import google.registry.util.CloudTasksUtils.GcpCloudTasksClient;
|
||||
import google.registry.util.CloudTasksUtils.SerializableCloudTasksClient;
|
||||
|
@ -46,8 +47,9 @@ public abstract class CloudTasksUtilsModule {
|
|||
@Config("projectId") String projectId,
|
||||
@Config("locationId") String locationId,
|
||||
SerializableCloudTasksClient client,
|
||||
Retrier retrier) {
|
||||
return new CloudTasksUtils(retrier, projectId, locationId, client);
|
||||
Retrier retrier,
|
||||
Clock clock) {
|
||||
return new CloudTasksUtils(retrier, clock, projectId, locationId, client);
|
||||
}
|
||||
|
||||
// Provides a supplier instead of using a Dagger @Provider because the latter is not serializable.
|
||||
|
|
|
@ -20,7 +20,6 @@ import google.registry.request.Action;
|
|||
import google.registry.request.Action.Service;
|
||||
import google.registry.request.Parameter;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.CloudTasksUtils;
|
||||
import java.util.Optional;
|
||||
import javax.inject.Inject;
|
||||
|
@ -35,7 +34,6 @@ public final class CommitLogFanoutAction implements Runnable {
|
|||
|
||||
public static final String BUCKET_PARAM = "bucket";
|
||||
|
||||
@Inject Clock clock;
|
||||
@Inject CloudTasksUtils cloudTasksUtils;
|
||||
|
||||
@Inject @Parameter("endpoint") String endpoint;
|
||||
|
@ -43,18 +41,15 @@ public final class CommitLogFanoutAction implements Runnable {
|
|||
@Inject @Parameter("jitterSeconds") Optional<Integer> jitterSeconds;
|
||||
@Inject CommitLogFanoutAction() {}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (int bucketId : CommitLogBucket.getBucketIds()) {
|
||||
cloudTasksUtils.enqueue(
|
||||
queue,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTaskWithJitter(
|
||||
endpoint,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(BUCKET_PARAM, Integer.toString(bucketId)),
|
||||
clock,
|
||||
jitterSeconds));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ import google.registry.request.ParameterMap;
|
|||
import google.registry.request.RequestParameters;
|
||||
import google.registry.request.Response;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.CloudTasksUtils;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -98,7 +97,6 @@ public final class TldFanoutAction implements Runnable {
|
|||
|
||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||
|
||||
@Inject Clock clock;
|
||||
@Inject CloudTasksUtils cloudTasksUtils;
|
||||
@Inject Response response;
|
||||
@Inject @Parameter(ENDPOINT_PARAM) String endpoint;
|
||||
|
@ -159,7 +157,7 @@ public final class TldFanoutAction implements Runnable {
|
|||
params = ArrayListMultimap.create(params);
|
||||
params.put(RequestParameters.PARAM_TLD, tld);
|
||||
}
|
||||
return CloudTasksUtils.createPostTask(
|
||||
endpoint, Service.BACKEND.toString(), params, clock, jitterSeconds);
|
||||
return cloudTasksUtils.createPostTaskWithJitter(
|
||||
endpoint, Service.BACKEND.toString(), params, jitterSeconds);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -334,7 +334,8 @@ public class LoadTestAction implements Runnable {
|
|||
tasks.add(
|
||||
Task.newBuilder()
|
||||
.setAppEngineHttpRequest(
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils
|
||||
.createPostTask(
|
||||
"/_dr/epptool",
|
||||
Service.TOOLS.toString(),
|
||||
ImmutableMultimap.of(
|
||||
|
|
|
@ -233,14 +233,14 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
|
|||
if (mode == RdeMode.FULL) {
|
||||
cloudTasksUtils.enqueue(
|
||||
"rde-upload",
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
RdeUploadAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(RequestParameters.PARAM_TLD, tld)));
|
||||
} else {
|
||||
cloudTasksUtils.enqueue(
|
||||
"brda",
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
BrdaCopyAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(
|
||||
|
|
|
@ -134,7 +134,7 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
|
|||
}
|
||||
cloudTasksUtils.enqueue(
|
||||
RDE_REPORT_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
RdeReportAction.PATH, Service.BACKEND.getServiceId(), params));
|
||||
}
|
||||
|
||||
|
|
|
@ -151,7 +151,7 @@ public class GenerateInvoicesAction implements Runnable {
|
|||
if (shouldPublish) {
|
||||
cloudTasksUtils.enqueue(
|
||||
ReportingModule.BEAM_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTaskWithDelay(
|
||||
PublishInvoicesAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(
|
||||
|
@ -159,7 +159,6 @@ public class GenerateInvoicesAction implements Runnable {
|
|||
jobId,
|
||||
ReportingModule.PARAM_YEAR_MONTH,
|
||||
yearMonth.toString()),
|
||||
clock,
|
||||
Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES)));
|
||||
}
|
||||
response.setStatus(SC_OK);
|
||||
|
|
|
@ -125,7 +125,7 @@ public class PublishInvoicesAction implements Runnable {
|
|||
private void enqueueCopyDetailReportsTask() {
|
||||
cloudTasksUtils.enqueue(
|
||||
BillingModule.CRON_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
CopyDetailReportsAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(PARAM_YEAR_MONTH, yearMonth.toString())));
|
||||
|
|
|
@ -34,7 +34,6 @@ import google.registry.request.Action.Service;
|
|||
import google.registry.request.Parameter;
|
||||
import google.registry.request.Response;
|
||||
import google.registry.request.auth.Auth;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.CloudTasksUtils;
|
||||
import google.registry.util.EmailMessage;
|
||||
import google.registry.util.Retrier;
|
||||
|
@ -86,7 +85,6 @@ public final class IcannReportingStagingAction implements Runnable {
|
|||
@Inject @Config("gSuiteOutgoingEmailAddress") InternetAddress sender;
|
||||
@Inject @Config("alertRecipientEmailAddress") InternetAddress recipient;
|
||||
@Inject SendEmailService emailService;
|
||||
@Inject Clock clock;
|
||||
@Inject CloudTasksUtils cloudTasksUtils;
|
||||
|
||||
@Inject IcannReportingStagingAction() {}
|
||||
|
@ -123,11 +121,10 @@ public final class IcannReportingStagingAction implements Runnable {
|
|||
logger.atInfo().log("Enqueueing report upload.");
|
||||
cloudTasksUtils.enqueue(
|
||||
CRON_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTaskWithDelay(
|
||||
IcannReportingUploadAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
null,
|
||||
clock,
|
||||
Duration.standardMinutes(2)));
|
||||
return null;
|
||||
},
|
||||
|
|
|
@ -144,7 +144,7 @@ public class GenerateSpec11ReportAction implements Runnable {
|
|||
if (sendEmail) {
|
||||
cloudTasksUtils.enqueue(
|
||||
ReportingModule.BEAM_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTaskWithDelay(
|
||||
PublishSpec11ReportAction.PATH,
|
||||
Service.BACKEND.toString(),
|
||||
ImmutableMultimap.of(
|
||||
|
@ -152,7 +152,6 @@ public class GenerateSpec11ReportAction implements Runnable {
|
|||
jobId,
|
||||
ReportingModule.PARAM_DATE,
|
||||
date.toString()),
|
||||
clock,
|
||||
Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES)));
|
||||
}
|
||||
response.setStatus(SC_OK);
|
||||
|
|
|
@ -133,7 +133,7 @@ final class GenerateEscrowDepositCommand implements CommandWithRemoteApi {
|
|||
}
|
||||
cloudTasksUtils.enqueue(
|
||||
RDE_REPORT_QUEUE,
|
||||
CloudTasksUtils.createPostTask(
|
||||
cloudTasksUtils.createPostTask(
|
||||
RdeStagingAction.PATH, Service.BACKEND.toString(), paramsBuilder.build()));
|
||||
}
|
||||
|
||||
|
|
|
@ -641,7 +641,7 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
|
|||
// there's an update besides the lastUpdateTime
|
||||
cloudTasksUtils.enqueue(
|
||||
SyncRegistrarsSheetAction.QUEUE,
|
||||
CloudTasksUtils.createGetTask(
|
||||
cloudTasksUtils.createGetTask(
|
||||
SyncRegistrarsSheetAction.PATH, Service.BACKEND.toString(), ImmutableMultimap.of()));
|
||||
}
|
||||
String environment = Ascii.toLowerCase(String.valueOf(RegistryEnvironment.get()));
|
||||
|
|
|
@ -33,7 +33,7 @@ class CommitLogFanoutActionTest {
|
|||
|
||||
private static final String ENDPOINT = "/the/servlet";
|
||||
private static final String QUEUE = "the-queue";
|
||||
private final CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
|
||||
private final CloudTasksHelper cloudTasksHelper = new CloudTasksHelper(new FakeClock());
|
||||
|
||||
@RegisterExtension
|
||||
final AppEngineExtension appEngineExtension =
|
||||
|
@ -58,7 +58,6 @@ class CommitLogFanoutActionTest {
|
|||
action.endpoint = ENDPOINT;
|
||||
action.queue = QUEUE;
|
||||
action.jitterSeconds = Optional.empty();
|
||||
action.clock = new FakeClock();
|
||||
action.run();
|
||||
List<TaskMatcher> matchers = new ArrayList<>();
|
||||
for (int bucketId : CommitLogBucket.getBucketIds()) {
|
||||
|
|
|
@ -45,7 +45,7 @@ class TldFanoutActionTest {
|
|||
private static final String ENDPOINT = "/the/servlet";
|
||||
private static final String QUEUE = "the-queue";
|
||||
private final FakeResponse response = new FakeResponse();
|
||||
private final CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
|
||||
private final CloudTasksHelper cloudTasksHelper = new CloudTasksHelper(new FakeClock());
|
||||
|
||||
@RegisterExtension
|
||||
final AppEngineExtension appEngine =
|
||||
|
@ -61,7 +61,6 @@ class TldFanoutActionTest {
|
|||
|
||||
private void run(ImmutableListMultimap<String, String> params) {
|
||||
TldFanoutAction action = new TldFanoutAction();
|
||||
action.clock = new FakeClock();
|
||||
action.params = params;
|
||||
action.endpoint = ENDPOINT;
|
||||
action.queue = QUEUE;
|
||||
|
|
|
@ -23,7 +23,6 @@ import static org.mockito.Mockito.when;
|
|||
|
||||
import com.google.cloud.tasks.v2.HttpMethod;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.google.protobuf.util.Timestamps;
|
||||
import google.registry.beam.BeamActionTestBase;
|
||||
import google.registry.model.common.DatabaseMigrationStateSchedule.PrimaryDatabase;
|
||||
import google.registry.reporting.ReportingModule;
|
||||
|
@ -83,11 +82,9 @@ class GenerateInvoicesActionTest extends BeamActionTestBase {
|
|||
.param("jobId", "jobid")
|
||||
.param("yearMonth", "2017-10")
|
||||
.scheduleTime(
|
||||
Timestamps.fromMillis(
|
||||
clock
|
||||
.nowUtc()
|
||||
.plus(Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES))
|
||||
.getMillis())));
|
||||
clock
|
||||
.nowUtc()
|
||||
.plus(Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES))));
|
||||
}
|
||||
|
||||
@TestOfyAndSql
|
||||
|
|
|
@ -24,7 +24,6 @@ import static org.mockito.Mockito.when;
|
|||
import com.google.cloud.tasks.v2.HttpMethod;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.protobuf.util.Timestamps;
|
||||
import google.registry.bigquery.BigqueryJobFailureException;
|
||||
import google.registry.reporting.icann.IcannReportingModule.ReportType;
|
||||
import google.registry.request.HttpException.BadRequestException;
|
||||
|
@ -54,7 +53,8 @@ class IcannReportingStagingActionTest {
|
|||
private YearMonth yearMonth = new YearMonth(2017, 6);
|
||||
private String subdir = "default/dir";
|
||||
private IcannReportingStagingAction action;
|
||||
private CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
|
||||
private FakeClock clock = new FakeClock(DateTime.parse("2021-01-02T11:00:00Z"));
|
||||
private CloudTasksHelper cloudTasksHelper = new CloudTasksHelper(clock);
|
||||
|
||||
@RegisterExtension
|
||||
final AppEngineExtension appEngine =
|
||||
|
@ -77,7 +77,6 @@ class IcannReportingStagingActionTest {
|
|||
action.recipient = new InternetAddress("recipient@example.com");
|
||||
action.emailService = mock(SendEmailService.class);
|
||||
action.cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
|
||||
action.clock = new FakeClock(DateTime.parse("2021-01-02T11:00:00Z"));
|
||||
|
||||
when(stager.stageReports(yearMonth, subdir, ReportType.ACTIVITY))
|
||||
.thenReturn(ImmutableList.of("a", "b"));
|
||||
|
@ -91,9 +90,7 @@ class IcannReportingStagingActionTest {
|
|||
new TaskMatcher()
|
||||
.url("/_dr/task/icannReportingUpload")
|
||||
.method(HttpMethod.POST)
|
||||
.scheduleTime(
|
||||
Timestamps.fromMillis(
|
||||
action.clock.nowUtc().plus(Duration.standardMinutes(2)).getMillis())));
|
||||
.scheduleTime(clock.nowUtc().plus(Duration.standardMinutes(2))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -21,7 +21,6 @@ import static org.mockito.Mockito.when;
|
|||
|
||||
import com.google.cloud.tasks.v2.HttpMethod;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.google.protobuf.util.Timestamps;
|
||||
import google.registry.beam.BeamActionTestBase;
|
||||
import google.registry.model.common.DatabaseMigrationStateSchedule.PrimaryDatabase;
|
||||
import google.registry.reporting.ReportingModule;
|
||||
|
@ -44,7 +43,7 @@ class GenerateSpec11ReportActionTest extends BeamActionTestBase {
|
|||
AppEngineExtension.builder().withDatastoreAndCloudSql().withTaskQueue().build();
|
||||
|
||||
private final FakeClock clock = new FakeClock(DateTime.parse("2018-06-11T12:23:56Z"));
|
||||
private CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
|
||||
private CloudTasksHelper cloudTasksHelper = new CloudTasksHelper(clock);
|
||||
private CloudTasksUtils cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
|
||||
private GenerateSpec11ReportAction action;
|
||||
|
||||
|
@ -101,11 +100,9 @@ class GenerateSpec11ReportActionTest extends BeamActionTestBase {
|
|||
.param("jobId", "jobid")
|
||||
.param("date", "2018-06-11")
|
||||
.scheduleTime(
|
||||
Timestamps.fromMillis(
|
||||
clock
|
||||
.nowUtc()
|
||||
.plus(Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES))
|
||||
.getMillis())));
|
||||
clock
|
||||
.nowUtc()
|
||||
.plus(Duration.standardMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -40,6 +40,7 @@ import com.google.common.net.HttpHeaders;
|
|||
import com.google.common.net.MediaType;
|
||||
import com.google.common.truth.Truth8;
|
||||
import com.google.protobuf.Timestamp;
|
||||
import com.google.protobuf.util.Timestamps;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.util.CloudTasksUtils;
|
||||
import google.registry.util.Retrier;
|
||||
|
@ -60,6 +61,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import javax.annotation.Nonnull;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* Static utility functions for testing task queues.
|
||||
|
@ -92,13 +94,22 @@ public class CloudTasksHelper implements Serializable {
|
|||
private static final String PROJECT_ID = "test-project";
|
||||
private static final String LOCATION_ID = "test-location";
|
||||
|
||||
private final Retrier retrier = new Retrier(new FakeSleeper(new FakeClock()), 1);
|
||||
private final int instanceId = nextInstanceId.getAndIncrement();
|
||||
private final CloudTasksUtils cloudTasksUtils =
|
||||
new CloudTasksUtils(retrier, PROJECT_ID, LOCATION_ID, new FakeCloudTasksClient());
|
||||
private final CloudTasksUtils cloudTasksUtils;
|
||||
|
||||
public CloudTasksHelper(FakeClock clock) {
|
||||
this.cloudTasksUtils =
|
||||
new CloudTasksUtils(
|
||||
new Retrier(new FakeSleeper(clock), 1),
|
||||
clock,
|
||||
PROJECT_ID,
|
||||
LOCATION_ID,
|
||||
new FakeCloudTasksClient());
|
||||
testTasks.put(instanceId, Multimaps.synchronizedListMultimap(LinkedListMultimap.create()));
|
||||
}
|
||||
|
||||
public CloudTasksHelper() {
|
||||
testTasks.put(instanceId, Multimaps.synchronizedListMultimap(LinkedListMultimap.create()));
|
||||
this(new FakeClock());
|
||||
}
|
||||
|
||||
public CloudTasksUtils getTestCloudTasksUtils() {
|
||||
|
@ -302,6 +313,10 @@ public class CloudTasksHelper implements Serializable {
|
|||
return this;
|
||||
}
|
||||
|
||||
public TaskMatcher scheduleTime(DateTime scheduleTime) {
|
||||
return scheduleTime(Timestamps.fromMillis(scheduleTime.getMillis()));
|
||||
}
|
||||
|
||||
public TaskMatcher param(String key, String value) {
|
||||
checkNotNull(value, "Test error: A param can never have a null value, so don't assert it");
|
||||
expected.params.put(key, value);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue