1
1
/*
2
- * Copyright 2021 Google Inc.
2
+ * Copyright 2021 Google LLC
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
21
21
// Imports the Google Cloud client library
22
22
23
+ import com .google .cloud .workflows .executions .v1 .CreateExecutionRequest ;
23
24
import com .google .cloud .workflows .executions .v1 .Execution ;
24
25
import com .google .cloud .workflows .executions .v1 .ExecutionsClient ;
25
26
import com .google .cloud .workflows .executions .v1 .WorkflowName ;
26
27
28
+ import java .io .IOException ;
29
+
27
30
/**
28
31
* Cloud Workflows API sample. Executes a workflow and waits for results.
29
32
* Example usage:
@@ -35,11 +38,13 @@ public class WorkflowsQuickstart {
35
38
private static String LOCATION = System .getenv ("LOCATION" );
36
39
private static String WORKFLOW = System .getenv ("WORKFLOW" );
37
40
38
- public static String workflowsQuickstart (String projectId , String location , String workflow ) {
41
+ public static String workflowsQuickstart (String projectId , String location , String workflow ) throws InterruptedException , IOException {
39
42
// Execute workflow
40
43
try (ExecutionsClient workflowExecutionsClient = ExecutionsClient .create ()) {
41
44
WorkflowName parent = WorkflowName .of (projectId , location , workflow );
42
45
Execution initialExecution = Execution .newBuilder ().build ();
46
+
47
+ // Creates the execution object.
43
48
Execution createExecutionRes = workflowExecutionsClient
44
49
.createExecution (parent , initialExecution );
45
50
@@ -49,11 +54,17 @@ public static String workflowsQuickstart(String projectId, String location, Stri
49
54
// Wait for execution to finish, then print results.
50
55
boolean executionFinished = false ;
51
56
long backoffDelay = 1_000 ; // Start wait with delay of 1,000 ms
57
+ final long BACKOFF_TIMEOUT = 10 * 60_000 ; // Time out at 10 minutes
52
58
System .out .println ("Poll for results..." );
53
59
while (!executionFinished ) {
54
60
Execution execution = workflowExecutionsClient .getExecution (executionName );
55
61
executionFinished = execution .getState () != Execution .State .ACTIVE ;
56
62
63
+ // We've passed the max
64
+ if (backoffDelay > BACKOFF_TIMEOUT ) {
65
+ return "" ;
66
+ }
67
+
57
68
// If we haven't seen the results yet, wait.
58
69
if (!executionFinished ) {
59
70
System .out .println ("- Waiting for results" );
@@ -65,18 +76,14 @@ public static String workflowsQuickstart(String projectId, String location, Stri
65
76
return execution .getResult ();
66
77
}
67
78
}
68
- // This return is never reached.
69
- return "" ;
70
- } catch (Exception e ) {
71
- System .out .printf ("Error executing workflow: %s%n" , e );
72
- return "" ;
73
79
}
80
+ return "" ;
74
81
}
75
82
76
83
/**
77
84
* Demonstrates using the Workflows API.
78
85
*/
79
- public static void main (String ... args ) {
86
+ public static void main (String ... args ) throws IOException , InterruptedException {
80
87
if (GOOGLE_CLOUD_PROJECT .isEmpty ()) {
81
88
System .out .println ("GOOGLE_CLOUD_PROJECT is empty" );
82
89
}
0 commit comments