Skip to content

Commit 062a5af

Browse files
chore(workflows): migrate samples 'execute_without_arguments' and 'execute_with_arguments' - step 1 (#13328)
* chore(workflows): migrate samples - step 1 - Copy main.py to execute_without_arguments.py - Rename pass_data_in_execution_request.py to execute_with_arguments.py * chore(workflows): remove unused region tags.
1 parent 89352d0 commit 062a5af

File tree

4 files changed

+135
-7
lines changed

4 files changed

+135
-7
lines changed

workflows/cloud-client/pass_data_in_execution_request.py renamed to workflows/cloud-client/execute_with_arguments.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from google.cloud.workflows.executions_v1 import Execution
1818

1919

20-
def execute_workflow_with_argument(
20+
def execute_workflow_with_arguments(
2121
project_id: str,
2222
location: str,
2323
workflow_id: str
@@ -54,7 +54,7 @@ def execute_workflow_with_argument(
5454
# Construct the fully qualified location path.
5555
parent = workflows_client.workflow_path(project_id, location, workflow_id)
5656

57-
# Execute the workflow.
57+
# Execute the workflow adding an dictionary of arguments.
5858
# Find more information about the Execution object here:
5959
# https://cloud.google.com/python/docs/reference/workflows/latest/google.cloud.workflows.executions_v1.types.Execution
6060
execution = executions_v1.Execution(
@@ -73,6 +73,8 @@ def execute_workflow_with_argument(
7373
backoff_delay = 1 # Start wait with delay of 1 second.
7474
print("Poll for result...")
7575

76+
# Keep polling the state until the execution finishes,
77+
# using exponential backoff.
7678
while not execution_finished:
7779
execution = execution_client.get_execution(
7880
request={"name": response.name}
@@ -96,4 +98,4 @@ def execute_workflow_with_argument(
9698
PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
9799
assert PROJECT, "'GOOGLE_CLOUD_PROJECT' environment variable not set."
98100

99-
execute_workflow_with_argument(PROJECT, "us-central1", "myFirstWorkflow")
101+
execute_workflow_with_arguments(PROJECT, "us-central1", "myFirstWorkflow")

workflows/cloud-client/pass_data_in_execution_request_test.py renamed to workflows/cloud-client/execute_with_arguments_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Google LLC
1+
# Copyright 2025 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -16,12 +16,14 @@
1616

1717
from google.cloud.workflows.executions_v1.types import executions
1818

19-
import pass_data_in_execution_request
19+
import execute_with_arguments
2020

2121

2222
@backoff.on_exception(backoff.expo, AssertionError, max_tries=5)
23-
def test_workflow_execution_with_arguments(project_id: str, location: str, workflow_id: str) -> None:
24-
execution_result = pass_data_in_execution_request.execute_workflow_with_argument(
23+
def test_workflow_execution_with_arguments(
24+
project_id: str, location: str, workflow_id: str
25+
) -> None:
26+
execution_result = execute_with_arguments.execute_workflow_with_arguments(
2527
project_id, location, workflow_id
2628
)
2729
assert execution_result.state == executions.Execution.State.SUCCEEDED
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_without_arguments(
21+
project_id: str,
22+
location: str,
23+
workflow_id: str
24+
) -> Execution:
25+
"""Execute a workflow and print the execution results.
26+
27+
A workflow consists of a series of steps described
28+
using the Workflows syntax, and can be written in either YAML or JSON.
29+
30+
Args:
31+
project: The ID of the Google Cloud project
32+
which contains the workflow to execute.
33+
location: The location for the workflow.
34+
workflow: The ID of the workflow to execute.
35+
36+
Returns:
37+
The execution response.
38+
"""
39+
40+
# [START workflows_execute_without_arguments]
41+
import time
42+
43+
from google.cloud import workflows_v1
44+
from google.cloud.workflows import executions_v1
45+
46+
from google.cloud.workflows.executions_v1.types import executions
47+
48+
# TODO(developer): Update and uncomment the following lines.
49+
# project_id = "YOUR_PROJECT_ID"
50+
# location = "YOUR_LOCATION" # For example: us-central1
51+
# workflow_id = "YOUR_WORKFLOW_ID" # For example: myFirstWorkflow
52+
53+
# Initialize API clients.
54+
execution_client = executions_v1.ExecutionsClient()
55+
workflows_client = workflows_v1.WorkflowsClient()
56+
57+
# Construct the fully qualified location path.
58+
parent = workflows_client.workflow_path(project_id, location, workflow_id)
59+
60+
# Execute the workflow.
61+
response = execution_client.create_execution(request={"parent": parent})
62+
print(f"Created execution: {response.name}")
63+
64+
# Wait for execution to finish, then print results.
65+
execution_finished = False
66+
backoff_delay = 1 # Start wait with delay of 1 second.
67+
print("Poll for result...")
68+
69+
# Keep polling the state until the execution finishes,
70+
# using exponential backoff.
71+
while not execution_finished:
72+
execution = execution_client.get_execution(
73+
request={"name": response.name}
74+
)
75+
execution_finished = execution.state != executions.Execution.State.ACTIVE
76+
77+
# If we haven't seen the result yet, keep waiting.
78+
if not execution_finished:
79+
print("- Waiting for results...")
80+
time.sleep(backoff_delay)
81+
# Double the delay to provide exponential backoff.
82+
backoff_delay *= 2
83+
else:
84+
print(f"Execution finished with state: {execution.state.name}")
85+
print(f"Execution results: {execution.result}")
86+
# [END workflows_execute_without_arguments]
87+
return execution
88+
89+
90+
if __name__ == "__main__":
91+
PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
92+
assert PROJECT, "'GOOGLE_CLOUD_PROJECT' environment variable not set."
93+
94+
execute_workflow_without_arguments(PROJECT, "us-central1", "myFirstWorkflow")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2021 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 backoff
16+
17+
from google.cloud.workflows.executions_v1.types import executions
18+
19+
import execute_without_arguments
20+
21+
22+
@backoff.on_exception(backoff.expo, AssertionError, max_tries=5)
23+
def test_workflow_execution_without_arguments(
24+
project_id: str, location: str, workflow_id: str
25+
) -> None:
26+
result = execute_without_arguments.execute_workflow_without_arguments(
27+
project_id, location, workflow_id
28+
)
29+
assert result.state == executions.Execution.State.SUCCEEDED
30+
assert result.result

0 commit comments

Comments
 (0)