Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Tasks] Add HTTP push queue sample #1355

Merged
merged 13 commits into from
Apr 19, 2019
25 changes: 24 additions & 1 deletion appengine-java8/tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ location is "us-central1").
export LOCATION_ID=<YOUR_ZONE>
```

### Using App Engine Queues
Create a task, targeted at the `/tasks/create` endpoint, with a payload specified:

```
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these instructions still correct? It looks like you are using environment variables now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct. This readme is associated with the App Engine sample.

-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
--queue $QUEUE_ID --location $LOCATION_ID --payload hello"
--queue $QUEUE_ID \
--location $LOCATION_ID \
--payload hello"
```

The App Engine app serves as a target for the push requests. It has an
Expand All @@ -89,3 +92,23 @@ mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
--queue $QUEUE_ID --location $LOCATION_ID --payload hello --in-seconds 30"
```

### Using HTTP Push Queues
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in line with not having the samples be a CLI, I think we're also not really including run instructions in the README for running the samples from the command line, as these code snippets are really for hosting on c.g.c documentation and not meant to be run directly by the users after cloning the whole repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


Set an environment variable for the endpoint to your task handler. This is an
example url to send requests to the App Engine task handler:
```
export URL=https://${PROJECT_ID}.appspot.com/tasks/create
```

Running the sample will create a task and send the task to the specific URL
endpoint, with a payload specified:

```
mvn exec:java -Dexec.mainClass="com.example.task.CreateHttpTask" \
-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
--url $URL \
--queue $QUEUE_ID \
--location $LOCATION_ID \
--payload hello"
```
1 change: 0 additions & 1 deletion appengine-java8/tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ Copyright 2018 Google LLC
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.example.task.CreateTask</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.task;

import com.google.cloud.tasks.v2beta3.CloudTasksClient;
import com.google.cloud.tasks.v2beta3.HttpMethod;
import com.google.cloud.tasks.v2beta3.HttpRequest;
import com.google.cloud.tasks.v2beta3.QueueName;
import com.google.cloud.tasks.v2beta3.Task;
import com.google.common.base.Strings;
import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;

import java.nio.charset.Charset;
import java.time.Clock;
import java.time.Instant;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CreateHttpTask {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a specific directive to make this into a CLI sample instead of the new sample format? If it's just to keep it consistent with the CreateTask sample that already exists here, I don't think that's enough of a reason to do the whole CLI thing instead of following the new template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a quickstart, so It is nice to have the CLI to run it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an easy way to compile and run if the correct variables are added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the CLI.

private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";

private static Option PROJECT_ID_OPTION = Option.builder("pid")
.longOpt("project-id")
.desc("The Google Cloud Project, if not set as GOOGLE_CLOUD_PROJECT env var.")
.hasArg()
.argName("project-id")
.type(String.class)
.build();

private static Option QUEUE_OPTION = Option.builder("q")
.required()
.longOpt("queue")
.desc("The Cloud Tasks queue.")
.hasArg()
.argName("queue")
.type(String.class)
.build();

private static Option LOCATION_OPTION = Option.builder("l")
.required()
.longOpt("location")
.desc("The region in which your queue is running.")
.hasArg()
.argName("location")
.type(String.class)
.build();

private static Option URL_OPTION = Option.builder("u")
.required()
.longOpt("url")
.desc("The full url path that the request will be sent to.")
.hasArg()
.argName("url")
.type(String.class)
.build();

private static Option PAYLOAD_OPTION = Option.builder("p")
.longOpt("payload")
.desc("The payload string for the task.")
.hasArg()
.argName("payload")
.type(String.class)
.build();

private static Option IN_SECONDS_OPTION = Option.builder("s")
.longOpt("in-seconds")
.desc("Schedule time for the task to create.")
.hasArg()
.argName("in-seconds")
.type(int.class)
.build();

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(URL_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 url = params.getOptionValue(URL_OPTION.getOpt());
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");

// [START cloud_tasks_create_http_task]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the START tag above import statements so that the dependency paths are included when the code is hosted on c.g.c. (and move the END tag past close brackets as necessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {

// Variables provided by the CLI.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by system variables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// projectId = "my-project-id";
// queueName = "my-appengine-queue";
// location = "us-central1";
// url = "https://<project-id>.appspot.com/tasks/create";
// 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()
.setHttpRequest(HttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setUrl(url)
.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_create_http_task]
}

private static void printUsage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(
"client",
"A simple Cloud Tasks command line client that creates a task with an "
+ "HTTP endpoint.",
options, "", true);
throw new RuntimeException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ private static void printUsage(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(
"client",
"A simple Cloud Tasks command line client that triggers a call to an AppEngine "
+ "endpoint.",
"A simple Cloud Tasks command line client that creates a task with an "
+ "App Engine endpoint.",
options, "", true);
throw new RuntimeException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
String body = req.getReader()
.lines()
.reduce("", (accumulator, actual) -> accumulator + actual);

if (!body.isEmpty()) {
log.info("Request payload: " + body);
String output = String.format("Received task with payload %s", body);
Expand Down