1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import os
15+ # [START workflows_api_quickstart]
16+ import time
17+
18+ from google .cloud import workflows_v1
19+ from google .cloud .workflows import executions_v1
20+ from google .cloud .workflows .executions_v1 import Execution
21+ from google .cloud .workflows .executions_v1 .types import executions
1622
1723
1824def execute_workflow (
19- project , location = "us-central1" , workflow = "myFirstWorkflow"
20- ):
21- """Execute a workflow and print the execution results."""
22- # [START workflows_api_quickstart]
23- import time
25+ project : str , location : str = "us-central1" , workflow : str = "myFirstWorkflow"
26+ ) -> Execution :
27+ """Execute a workflow and print the execution results.
2428
25- from google .cloud import workflows_v1
26- from google .cloud .workflows import executions_v1
27- from google .cloud .workflows .executions_v1 .types import executions
29+ A workflow consists of a series of steps described using the Workflows syntax, and can be written in either YAML or JSON.
2830
29- # TODO(developer): Uncomment these lines and replace with your values.
30- # project = 'my- project-id'
31- # location = 'us-central1'
32- # workflow = 'myFirstWorkflow'
31+ Args:
32+ project: The Google Cloud project id which contains the workflow to execute.
33+ location: The location for the workflow
34+ workflow: The ID of the workflow to execute.
3335
34- if not project :
35- raise Exception ('GOOGLE_CLOUD_PROJECT env var is required.' )
36+ Returns:
37+ The execution response.
38+ """
3639
3740 # Set up API clients.
3841 execution_client = executions_v1 .ExecutionsClient ()
@@ -48,25 +51,21 @@ def execute_workflow(
4851 # Wait for execution to finish, then print results.
4952 execution_finished = False
5053 backoff_delay = 1 # Start wait with delay of 1 second
51- print ('Poll every second for result...' )
52- while (not execution_finished ):
53- execution = execution_client .get_execution (
54- request = {"name" : response .name })
54+ print ("Poll every second for result..." )
55+ while not execution_finished :
56+ execution = execution_client .get_execution (request = {"name" : response .name })
5557 execution_finished = execution .state != executions .Execution .State .ACTIVE
5658
5759 # If we haven't seen the result yet, wait a second.
5860 if not execution_finished :
59- print (' - Waiting for results...' )
61+ print (" - Waiting for results..." )
6062 time .sleep (backoff_delay )
6163 # Double the delay to provide exponential backoff.
6264 backoff_delay *= 2
6365 else :
64- print (f' Execution finished with state: { execution .state .name } ' )
65- print (f' Execution results: { execution .result } ' )
66+ print (f" Execution finished with state: { execution .state .name } " )
67+ print (f" Execution results: { execution .result } " )
6668 return execution
67- # [END workflows_api_quickstart]
6869
6970
70- if __name__ == "__main__" :
71- project = os .environ .get ('GOOGLE_CLOUD_PROJECT' )
72- execute_workflow (project = project )
71+ # [END workflows_api_quickstart]
0 commit comments