Execute a workflow by using client libraries
You can execute a workflow and view the execution results using a client library.
For more information about installing the client libraries and setting up your development environment, see the Workflows client libraries overview.
Before you begin
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
- The following sample assumes you have already deployed the workflow,
myFirstWorkflow
. If you haven't, deploy it now using the Google Cloud console or the Google Cloud CLI. - Download and install the Git source code management tool.
Get the sample code
Clone the sample app repository to your local machine:
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Alternatively, you can download the sample as a zip file and extract it.
Change to the directory that contains the Workflows sample code:
Java
cd java-docs-samples/workflows/cloud-client/
Node.js
cd nodejs-docs-samples/workflows/quickstart/
Python
cd python-docs-samples/workflows/cloud-client/
Take a look at the sample code:
Java
Node.js
Python
The program does the following:
- Sets up the Cloud Client Libraries for Workflows.
- Executes a workflow.
- Polls the workflow's execution (using exponential backoff) until the execution terminates.
- Prints the execution results.
Run the sample
To run the sample, first install dependencies:
Java
mvn compile
Node.js
npm install
Python
pip3 install -r requirements.txt
Run the script:
Java
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME mvn compile exec:java -Dexec.mainClass=com.example.workflows.WorkflowsQuickstart
Node.js
node . PROJECT_ID CLOUD_REGION WORKFLOW_NAME
Python
GOOGLE_CLOUD_PROJECT=PROJECT_ID LOCATION=CLOUD_REGION WORKFLOW=WORKFLOW_NAME python3 main.py
Replace the following:
PROJECT_ID
: (required) the Project ID of the Google Cloud projectCLOUD_REGION
: the location for the workflow (default:us-central1
)WORKFLOW_NAME
: the ID of the workflow (default:myFirstWorkflow
)
The output is similar to the following:
Execution finished with state: SUCCEEDED ["Sunday","Sunday in the Park with George","Sunday shopping","Sunday Bloody Sunday","Sunday Times Golden Globe Race","Sunday All Stars","Sunday Night (South Korean TV series)","Sunday Silence","Sunday Without God","Sunday Independent (Ireland)"]
Pass data in an execution request
Depending on the client library language, you can also pass a runtime argument in an execution request.
For example, using JavaScript:
// Execute workflow
try {
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
argument: JSON.stringify({"searchTerm": "Friday"})
}
});
const executionName = createExecutionRes[0].name;
Or, using Java:
// Creates the execution object.
CreateExecutionRequest request =
CreateExecutionRequest.newBuilder()
.setParent(parent.toString())
.setExecution(Execution.newBuilder().setArgument("{\"searchTerm\":\"Friday\"}").build())
.build();
For more information about passing runtime arguments, see Pass runtime arguments in an execution request.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
In the Google Cloud console, go to the Workflows page.
From the list of workflows, click a workflow to go to its Workflow details page.
Click
Delete.Type the name of the workflow and then click Confirm.