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
20 changes: 19 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,18 @@ 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"
```
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,77 @@
/*
* 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.


public static void main(String[] args) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

Following the new sample template, this wouldn't be a main method, but rather a named one like "createHttpTask", and instead of getting the System env var values below, it would include those 4 things as named parameters. Then there would be commented out declarations of them below, e.g.

public static void createHttpTask(String projectId, String queueName, string location, string url) {
    // String projectId = "YOUR-PROJECT-ID";
    // etc

Then in your test file (unless there's a reason not to include a test with this sample, I don't have full context), you would get those input parameters from required environment variables and supply them in the method call.

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 sample will be a part of a quickstart as of right now; therefore I would like to keep the instructions in the README and the main method. I'm adding a bug for myself to update this complete sample after the launch to re-evaluate the CLI in CreateTask.java and add tests.

String projectId = System.getenv("PROJECT_ID");
String queueName = System.getenv("QUEUE_ID");
String location = System.getenv("LOCATION_ID");
String url = System.getenv("URL");

// [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";
String 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());

// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
// [END cloud_tasks_create_http_task]
}
}
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