|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from google.cloud.workflows.executions_v1 import Execution |
| 18 | + |
| 19 | + |
| 20 | +def execute_workflow_with_argument( |
| 21 | + project_id: str, |
| 22 | + location: str, |
| 23 | + workflow_id: str |
| 24 | +) -> Execution: |
| 25 | + """Execute a workflow and print the execution results. |
| 26 | +
|
| 27 | + Args: |
| 28 | + project: The ID of the Google Cloud project |
| 29 | + which contains the workflow to execute. |
| 30 | + location: The location for the workflow. |
| 31 | + workflow: The ID of the workflow to execute. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + The execution response. |
| 35 | + """ |
| 36 | + |
| 37 | +# [START workflows_execute_with_arguments] |
| 38 | + import time |
| 39 | + |
| 40 | + from google.cloud import workflows_v1 |
| 41 | + from google.cloud.workflows import executions_v1 |
| 42 | + |
| 43 | + from google.cloud.workflows.executions_v1.types import executions |
| 44 | + |
| 45 | + # TODO(developer): Update and uncomment the following lines. |
| 46 | + # project_id = "YOUR_PROJECT_ID" |
| 47 | + # location = "YOUR_LOCATION" # For example: us-central1 |
| 48 | + # workflow_id = "YOUR_WORKFLOW_ID" # For example: myFirstWorkflow |
| 49 | + |
| 50 | + # Initialize API clients. |
| 51 | + execution_client = executions_v1.ExecutionsClient() |
| 52 | + workflows_client = workflows_v1.WorkflowsClient() |
| 53 | + |
| 54 | + # Construct the fully qualified location path. |
| 55 | + parent = workflows_client.workflow_path(project_id, location, workflow_id) |
| 56 | + |
| 57 | + # Execute the workflow. |
| 58 | + # Find more information about the Execution object here: |
| 59 | + # https://cloud.google.com/python/docs/reference/workflows/latest/google.cloud.workflows.executions_v1.types.Execution |
| 60 | + execution = executions_v1.Execution( |
| 61 | + name=parent, |
| 62 | + argument='{"searchTerm": "Cloud"}', |
| 63 | + ) |
| 64 | + |
| 65 | + response = execution_client.create_execution( |
| 66 | + parent=parent, |
| 67 | + execution=execution, |
| 68 | + ) |
| 69 | + print(f"Created execution: {response.name}") |
| 70 | + |
| 71 | + # Wait for execution to finish, then print results. |
| 72 | + execution_finished = False |
| 73 | + backoff_delay = 1 # Start wait with delay of 1 second. |
| 74 | + print("Poll for result...") |
| 75 | + |
| 76 | + while not execution_finished: |
| 77 | + execution = execution_client.get_execution( |
| 78 | + request={"name": response.name} |
| 79 | + ) |
| 80 | + execution_finished = execution.state != executions.Execution.State.ACTIVE |
| 81 | + |
| 82 | + # If we haven't seen the result yet, keep waiting. |
| 83 | + if not execution_finished: |
| 84 | + print("- Waiting for results...") |
| 85 | + time.sleep(backoff_delay) |
| 86 | + # Double the delay to provide exponential backoff. |
| 87 | + backoff_delay *= 2 |
| 88 | + else: |
| 89 | + print(f"Execution finished with state: {execution.state.name}") |
| 90 | + print(f"Execution results: {execution.result}") |
| 91 | +# [END workflows_execute_with_arguments] |
| 92 | + return execution |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT") |
| 97 | + assert PROJECT, "'GOOGLE_CLOUD_PROJECT' environment variable not set." |
| 98 | + |
| 99 | + execute_workflow_with_argument(PROJECT, "us-central1", "myFirstWorkflow") |
0 commit comments