|
| 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 os |
| 16 | + |
| 17 | + |
| 18 | +def 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 |
| 24 | + |
| 25 | + from google.cloud import workflows_v1beta |
| 26 | + from google.cloud.workflows import executions_v1beta |
| 27 | + from google.cloud.workflows.executions_v1beta.types import executions |
| 28 | + |
| 29 | + # TODO(developer): Uncomment these lines and replace with your values. |
| 30 | + # project = 'my-project-id' |
| 31 | + # location = 'us- central1' |
| 32 | + # workflow = 'myFirstWorkflow' |
| 33 | + |
| 34 | + if not project: |
| 35 | + raise Exception('GOOGLE_CLOUD_PROJECT env var is required.') |
| 36 | + |
| 37 | + # Set up API clients. |
| 38 | + execution_client = executions_v1beta.ExecutionsClient() |
| 39 | + workflows_client = workflows_v1beta.WorkflowsClient() |
| 40 | + |
| 41 | + # Construct the fully qualified location path. |
| 42 | + parent = workflows_client.workflow_path(project, location, workflow) |
| 43 | + |
| 44 | + # Execute the workflow. |
| 45 | + response = execution_client.create_execution(request={"parent": parent}) |
| 46 | + print(f"Created execution: {response.name}") |
| 47 | + |
| 48 | + # Wait for execution to finish, then print results. |
| 49 | + execution_finished = False |
| 50 | + 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(request={"name": response.name}) |
| 54 | + execution_finished = execution.state != executions.Execution.State.ACTIVE |
| 55 | + |
| 56 | + # If we haven't seen the result yet, wait a second. |
| 57 | + if not execution_finished: |
| 58 | + print('- Waiting for results...') |
| 59 | + time.sleep(backoff_delay) |
| 60 | + backoff_delay *= 2 # Double the delay to provide exponential backoff. |
| 61 | + else: |
| 62 | + print(f'Execution finished with state: {execution.state.name}') |
| 63 | + print(execution.result) |
| 64 | + return execution.result |
| 65 | + # [END workflows_api_quickstart] |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + project = os.environ.get('GOOGLE_CLOUD_PROJECT') |
| 70 | + execute_workflow(project=project) |
0 commit comments