-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Update Cloud tasks samples #1211
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
Changes from 4 commits
99d3912
73abaee
3b226e7
19a16f2
d45b01d
3e56cd1
15625a2
f094018
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,11 @@ | |
|
||
package com.example.task; | ||
|
||
import com.google.cloud.tasks.v2beta2.AppEngineHttpRequest; | ||
import com.google.cloud.tasks.v2beta2.CloudTasksClient; | ||
import com.google.cloud.tasks.v2beta2.HttpMethod; | ||
import com.google.cloud.tasks.v2beta2.QueueName; | ||
import com.google.cloud.tasks.v2beta2.Task; | ||
import com.google.cloud.tasks.v2beta3.AppEngineHttpRequest; | ||
import com.google.cloud.tasks.v2beta3.CloudTasksClient; | ||
import com.google.cloud.tasks.v2beta3.HttpMethod; | ||
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; | ||
|
@@ -38,11 +38,11 @@ | |
import org.apache.commons.cli.ParseException; | ||
|
||
public class CreateTask { | ||
private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT"; | ||
private static String GOOGLE_CLOUD_PROJECT_KEY = "PROJECT_ID"; | ||
|
||
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.") | ||
.desc("The Google Cloud Project, if not set as PROJECT_ID env var.") | ||
.hasArg() | ||
.argName("project-id") | ||
.type(String.class) | ||
|
@@ -109,7 +109,7 @@ public static void main(String... args) throws Exception { | |
if (params.hasOption("project-id")) { | ||
projectId = params.getOptionValue("project-id"); | ||
} else { | ||
projectId = System.getenv(GGOGLE_CLOUD_PROJECT_KEY); | ||
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY); | ||
} | ||
if (Strings.isNullOrEmpty(projectId)) { | ||
printUsage(options); | ||
|
@@ -121,22 +121,36 @@ public static void main(String... args) throws Exception { | |
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload"); | ||
|
||
// [START cloud_tasks_appengine_create_task] | ||
// Instantiates a client. | ||
try (CloudTasksClient client = CloudTasksClient.create()) { | ||
|
||
// TODO(developer): Uncomment these lines and replace with your values. | ||
// String project = "my-project-id"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vars should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to comprise between the code snippet for the docs and running the sample with the CLI. Changing the comment to "Variables provided by the CLI." |
||
// String queue = "my-appengine-queue"; | ||
// String location = "us-central1"; | ||
// 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() | ||
.setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder() | ||
.setPayload(ByteString.copyFrom(payload, Charset.defaultCharset())) | ||
.setRelativeUrl("/tasks/create") | ||
.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())); | ||
} | ||
Task task = client.createTask( | ||
QueueName.of(projectId, location, queueName).toString(), taskBuilder.build()); | ||
|
||
// Send create task request. | ||
Task task = client.createTask(queuePath, taskBuilder.build()); | ||
System.out.println("Task created: " + task.getName()); | ||
} | ||
// [END cloud_tasks_appengine_create_task] | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this being changed? It's GOOGLE_CLOUD_PROJECT in all the samples I can think of, though I wouldn't be surprised if there were inconsistencies. Was there guidance on this that I missed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to make it consistent between the samples; there are many inconsistencies between repos/samples. I don't think there is an official call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are documentation pages that refer to GOOGLE_CLOUD_PROJECT env var though, including it being required for AEF, where I haven't seen any doc pages using just PROJECT_ID (aside from as a placeholder in CLI args or urls). That seems to only exist in samples.
You're right that the inconsistencies should be addressed though. If you're looking to remove one in favor of the other, though, this is the wrong direction - I found 39 examples of
System.getenv("GOOGLE_CLOUD_PROJECT")
in the project, versus 15 examples ofSystem.getenv("PROJECT_ID")
. Considering the first is referred to as an env var in our documentation, I'd prefer leaving this the way it was.