Search in sources :

Example 46 with Task

use of com.google.cloud.tasks.v2.Task in project java-docs-samples by GoogleCloudPlatform.

the class CreateTask method main.

public static void main(String... args) throws Exception {
    Options options = new Options();
    options.addOption(PROJECT_ID_OPTION);
    options.addOption(QUEUE_OPTION);
    options.addOption(LOCATION_OPTION);
    options.addOption(PAYLOAD_OPTION);
    options.addOption(IN_SECONDS_OPTION);
    if (args.length == 0) {
        printUsage(options);
        return;
    }
    CommandLineParser parser = new DefaultParser();
    CommandLine params = null;
    try {
        params = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }
    String projectId;
    if (params.hasOption("project-id")) {
        projectId = params.getOptionValue("project-id");
    } else {
        projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
    }
    if (Strings.isNullOrEmpty(projectId)) {
        printUsage(options);
        return;
    }
    String queueName = params.getOptionValue(QUEUE_OPTION.getOpt());
    String location = params.getOptionValue(LOCATION_OPTION.getOpt());
    String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Variables provided by the CLI.
        // projectId = "my-project-id";
        // queueName = "my-appengine-queue";
        // location = "us-central1";
        // payload = "hello";
        // Construct the fully qualified queue name.
        String queuePath = QueueName.of(projectId, location, queueName).toString();
        // Construct the task body.
        Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(payload, Charset.defaultCharset())).setRelativeUri("/tasks/create").setHttpMethod(HttpMethod.POST).build());
        if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
            // Add the scheduled time to the request.
            int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
            taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
        }
        // Send create task request.
        Task task = client.createTask(queuePath, taskBuilder.build());
        System.out.println("Task created: " + task.getName());
    }
// [END cloud_tasks_appengine_create_task]
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 47 with Task

use of com.google.cloud.tasks.v2.Task in project teammates by TEAMMATES.

the class GoogleCloudTasksService method addDeferredTask.

@Override
public void addDeferredTask(TaskWrapper task, long countdownTime) {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        String queuePath = QueueName.of(Config.APP_ID, Config.APP_REGION, task.getQueueName()).toString();
        AppEngineHttpRequest.Builder requestBuilder = AppEngineHttpRequest.newBuilder().setAppEngineRouting(AppEngineRouting.newBuilder().setVersion(Config.APP_VERSION).build()).setHttpMethod(HttpMethod.POST);
        if (task.getRequestBody() == null) {
            // the value is not important
            String relativeUrl = "http://place.holder";
            AppUrl url = new AppUrl(relativeUrl + task.getWorkerUrl());
            task.getParamMap().forEach((key, value) -> url.withParam(key, value));
            requestBuilder.setRelativeUri(url.toString());
        } else {
            String requestBody = JsonUtils.toCompactJson(task.getRequestBody());
            requestBuilder.putHeaders("Content-Type", "application/json; charset=UTF-8").setRelativeUri(task.getWorkerUrl()).setBody(ByteString.copyFrom(requestBody, Const.ENCODING));
        }
        Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(requestBuilder.build());
        if (countdownTime > 0) {
            taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now().plusMillis(countdownTime).getEpochSecond()));
        }
        client.createTask(queuePath, taskBuilder.build());
    } catch (IOException e) {
        log.severe("Cannot create Cloud Tasks client", e);
    }
}
Also used : AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Task(com.google.cloud.tasks.v2.Task) AppUrl(teammates.common.util.AppUrl) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 48 with Task

use of com.google.cloud.tasks.v2.Task in project nomulus by google.

the class CloudTasksUtilsTest method testSuccess_createGetTasks_withEmptyParams.

@Test
void testSuccess_createGetTasks_withEmptyParams() {
    Task task = cloudTasksUtils.createGetTask("/the/path", "myservice", ImmutableMultimap.of());
    assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.GET);
    assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
    assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
    assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
Also used : Task(com.google.cloud.tasks.v2.Task) Test(org.junit.jupiter.api.Test)

Example 49 with Task

use of com.google.cloud.tasks.v2.Task in project nomulus by google.

the class CloudTasksUtilsTest method testSuccess_createPostTasks_withZeroJitterSeconds.

@Test
void testSuccess_createPostTasks_withZeroJitterSeconds() {
    Task task = cloudTasksUtils.createPostTaskWithJitter("/the/path", "myservice", params, Optional.of(0));
    assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.POST);
    assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
    assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
    assertThat(task.getAppEngineHttpRequest().getHeadersMap().get("Content-Type")).isEqualTo("application/x-www-form-urlencoded");
    assertThat(task.getAppEngineHttpRequest().getBody().toString(StandardCharsets.UTF_8)).isEqualTo("key1=val1&key2=val2&key1=val3");
    assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
Also used : Task(com.google.cloud.tasks.v2.Task) Test(org.junit.jupiter.api.Test)

Example 50 with Task

use of com.google.cloud.tasks.v2.Task in project nomulus by google.

the class CloudTasksUtilsTest method testSuccess_createPostTasks_withNullParams.

@Test
void testSuccess_createPostTasks_withNullParams() {
    Task task = cloudTasksUtils.createPostTask("/the/path", "myservice", null);
    assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.POST);
    assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path");
    assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
    assertThat(task.getAppEngineHttpRequest().getBody().toString(StandardCharsets.UTF_8)).isEmpty();
    assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
Also used : Task(com.google.cloud.tasks.v2.Task) Test(org.junit.jupiter.api.Test)

Aggregations

Task (com.google.cloud.tasks.v2.Task)33 Test (org.junit.jupiter.api.Test)23 Task (org.eclipse.bpmn2.Task)17 Test (org.junit.Test)14 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)11 Task (io.fabric8.tekton.pipeline.v1beta1.Task)8 ByteString (com.google.protobuf.ByteString)7 IOException (java.io.IOException)6 Optional (java.util.Optional)5 FlowElement (org.eclipse.bpmn2.FlowElement)5 InputSet (org.eclipse.bpmn2.InputSet)5 AssignmentsInfo (org.kie.workbench.common.stunner.bpmn.definition.property.dataio.AssignmentsInfo)5 Queue (com.google.cloud.tasks.v2.Queue)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AdHocSubProcess (org.eclipse.bpmn2.AdHocSubProcess)4 BusinessRuleTask (org.eclipse.bpmn2.BusinessRuleTask)4 GlobalTask (org.eclipse.bpmn2.GlobalTask)4 OutputSet (org.eclipse.bpmn2.OutputSet)4 Process (org.eclipse.bpmn2.Process)4