Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions run/jobs/src/main/java/com/example/JobsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
* limitations under the License.
*/

// [START cloudrun_jobs_quickstart]

package com.example;

abstract class JobsExample {
// [START cloudrun_jobs_env_vars]
// These values are provided automatically by the Cloud Run Jobs runtime.
private static String TASK_NUM = System.getenv().getOrDefault("TASK_NUM", "0");
private static String ATTEMPT_NUM = System.getenv().getOrDefault("ATTEMPT_NUM", "0");
private static String CLOUD_RUN_TASK_INDEX =
System.getenv().getOrDefault("CLOUD_RUN_TASK_INDEX", "0");
private static String CLOUD_RUN_TASK_ATTEMPT =
System.getenv().getOrDefault("CLOUD_RUN_TASK_ATTEMPT", "0");

// User-provided environment variables
private static int SLEEP_MS = Integer.parseInt(System.getenv().getOrDefault("SLEEP_MS", "0"));
Expand All @@ -30,11 +34,15 @@ abstract class JobsExample {

// Start script
public static void main(String[] args) {
System.out.println(String.format("Starting Task #%s, Attempt #%s...", TASK_NUM, ATTEMPT_NUM));
System.out.println(
String.format(
"Starting Task #%s, Attempt #%s...", CLOUD_RUN_TASK_INDEX, CLOUD_RUN_TASK_ATTEMPT));
try {
runTask(SLEEP_MS, FAIL_RATE);
} catch (RuntimeException | InterruptedException e) {
System.err.println(String.format("Task #%s, Attempt #%s failed.", TASK_NUM, ATTEMPT_NUM));
System.err.println(
String.format(
"Task #%s, Attempt #%s failed.", CLOUD_RUN_TASK_INDEX, CLOUD_RUN_TASK_ATTEMPT));
// [START cloudrun_jobs_exit_process]
// Catch error and denote process-level failure to retry Task
System.exit(1);
Expand All @@ -59,6 +67,7 @@ static void runTask(int sleepTime, float failureRate) throws InterruptedExceptio
if (Math.random() < failureRate) {
throw new RuntimeException("Task Failed.");
}
System.out.println(String.format("Completed Task #%s", TASK_NUM));
System.out.println(String.format("Completed Task #%s", CLOUD_RUN_TASK_INDEX));
}
}
// [END cloudrun_jobs_quickstart]
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void generatesLogs() throws Exception {
calendar.add(Calendar.MINUTE, -5);
DateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String logFilter =
"resource.type = \"cloud_run_revision\""
+ " resource.labels.service_name = \""
"resource.type = \"cloud_run_job\""
+ " resource.labels.job_name = \""
+ service
+ "\" resource.labels.location = \"us-central1\""
+ " timestamp>=\""
Expand Down