-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add workflow execution samples with and without arguments #4302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6323a73
701ef0f
f461601
709e533
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // This is a generated sample, using the typeless sample bot. Please | ||
| // look for the source TypeScript sample (.ts) for modifications. | ||
| 'use strict'; | ||
|
|
||
| const projectId = process.argv[2] || process.env.GOOGLE_CLOUD_PROJECT; | ||
| const location = process.argv[3] || 'us-central1'; | ||
| const workflowName = process.argv[4] || 'myFirstWorkflow'; | ||
| const searchTerm = process.argv[5] || ''; | ||
|
|
||
| // [START workflows_execute_with_arguments] | ||
| const {ExecutionsClient} = require('@google-cloud/workflows'); | ||
| const client = new ExecutionsClient(); | ||
|
|
||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = 'my-project'; | ||
| // const location = 'us-central1'; | ||
| // const workflow = 'myFirstWorkflow'; | ||
| // const searchTerm = ''; | ||
|
|
||
| /** | ||
| * Executes a Workflow and waits for the results with exponential backoff. | ||
| * @param {string} projectId The Google Cloud Project containing the workflow | ||
| * @param {string} location The workflow location | ||
| * @param {string} workflow The workflow name | ||
| * @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument | ||
| */ | ||
| async function executeWorkflow(projectId, location, workflow, searchTerm) { | ||
| /** | ||
| * Sleeps the process N number of milliseconds. | ||
| * @param {Number} ms The number of milliseconds to sleep. | ||
| */ | ||
| function sleep(ms) { | ||
| return new Promise(resolve => { | ||
| setTimeout(resolve, ms); | ||
| }); | ||
| } | ||
| const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {}; | ||
| // Execute workflow | ||
| try { | ||
| const createExecutionRes = await client.createExecution({ | ||
| parent: client.workflowPath(projectId, location, workflow), | ||
| execution: { | ||
| // Runtime arguments can be passed as a JSON string | ||
| argument: JSON.stringify(runtimeArgs), | ||
| }, | ||
| }); | ||
| const executionName = createExecutionRes[0].name; | ||
| console.log(`Created execution: ${executionName}`); | ||
|
|
||
| // Wait for execution to finish, then print results. | ||
| let executionFinished = false; | ||
| let backoffDelay = 1000; // Start wait with delay of 1,000 ms | ||
| console.log('Poll every second for result...'); | ||
| while (!executionFinished) { | ||
| const [execution] = await client.getExecution({ | ||
| name: executionName, | ||
| }); | ||
| executionFinished = execution.state !== 'ACTIVE'; | ||
|
|
||
| // If we haven't seen the result yet, wait a second. | ||
| if (!executionFinished) { | ||
| console.log('- Waiting for results...'); | ||
| await sleep(backoffDelay); | ||
| backoffDelay *= 2; // Double the delay to provide exponential backoff. | ||
| } else { | ||
| console.log(`Execution finished with state: ${execution.state}`); | ||
| console.log(execution.result); | ||
| return execution.result; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.error(`Error executing workflow: ${e}`); | ||
| } | ||
| } | ||
|
|
||
| executeWorkflow(projectId, location, workflowName, searchTerm).catch(err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
|
|
||
| // [END workflows_execute_with_arguments] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| const projectId = | ||
| process.argv[2] || (process.env.GOOGLE_CLOUD_PROJECT as string); | ||
| const location = process.argv[3] || 'us-central1'; | ||
| const workflowName = process.argv[4] || 'myFirstWorkflow'; | ||
| const searchTerm = process.argv[5] || ''; | ||
|
|
||
| // [START workflows_execute_with_arguments] | ||
| import {ExecutionsClient} from '@google-cloud/workflows'; | ||
| const client: ExecutionsClient = new ExecutionsClient(); | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = 'my-project'; | ||
| // const location = 'us-central1'; | ||
| // const workflow = 'myFirstWorkflow'; | ||
| // const searchTerm = ''; | ||
|
|
||
| /** | ||
| * Executes a Workflow and waits for the results with exponential backoff. | ||
| * @param {string} projectId The Google Cloud Project containing the workflow | ||
| * @param {string} location The workflow location | ||
| * @param {string} workflow The workflow name | ||
| * @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument | ||
| */ | ||
| async function executeWorkflow( | ||
| projectId: string, | ||
| location: string, | ||
| workflow: string, | ||
| searchTerm: string | ||
| ) { | ||
| /** | ||
| * Sleeps the process N number of milliseconds. | ||
| * @param {Number} ms The number of milliseconds to sleep. | ||
| */ | ||
| function sleep(ms: number): Promise<unknown> { | ||
| return new Promise(resolve => { | ||
| setTimeout(resolve, ms); | ||
| }); | ||
| } | ||
| const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {}; | ||
| // Execute workflow | ||
| try { | ||
| const createExecutionRes = await client.createExecution({ | ||
| parent: client.workflowPath(projectId, location, workflow), | ||
| execution: { | ||
| // Runtime arguments can be passed as a JSON string | ||
| argument: JSON.stringify(runtimeArgs), | ||
| }, | ||
| }); | ||
| const executionName = createExecutionRes[0].name; | ||
| console.log(`Created execution: ${executionName}`); | ||
|
|
||
| // Wait for execution to finish, then print results. | ||
| let executionFinished = false; | ||
| let backoffDelay = 1000; // Start wait with delay of 1,000 ms | ||
| console.log('Poll every second for result...'); | ||
| while (!executionFinished) { | ||
| const [execution] = await client.getExecution({ | ||
| name: executionName, | ||
| }); | ||
| executionFinished = execution.state !== 'ACTIVE'; | ||
|
|
||
| // If we haven't seen the result yet, wait a second. | ||
| if (!executionFinished) { | ||
| console.log('- Waiting for results...'); | ||
| await sleep(backoffDelay); | ||
| backoffDelay *= 2; // Double the delay to provide exponential backoff. | ||
| } else { | ||
| console.log(`Execution finished with state: ${execution.state}`); | ||
| console.log(execution.result); | ||
| return execution.result; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.error(`Error executing workflow: ${e}`); | ||
| } | ||
|
Comment on lines
+88
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error is caught and logged, but not rethrown. This causes the promise returned by } catch (e) {
console.error(`Error executing workflow: ${e}`);
throw e;
} |
||
| } | ||
|
|
||
| executeWorkflow(projectId, location, workflowName, searchTerm).catch( | ||
| (err: Error) => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| } | ||
| ); | ||
| // [END workflows_execute_with_arguments] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // This is a generated sample, using the typeless sample bot. Please | ||
| // look for the source TypeScript sample (.ts) for modifications. | ||
| 'use strict'; | ||
|
|
||
| const projectId = process.argv[2] || process.env.GOOGLE_CLOUD_PROJECT; | ||
| const location = process.argv[3] || 'us-central1'; | ||
| const workflowName = process.argv[4] || 'myFirstWorkflow'; | ||
|
|
||
| // [START workflows_execute_without_arguments] | ||
| const {ExecutionsClient} = require('@google-cloud/workflows'); | ||
| const client = new ExecutionsClient(); | ||
|
|
||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = 'my-project'; | ||
| // const location = 'us-central1'; | ||
| // const workflow = 'myFirstWorkflow'; | ||
|
|
||
| /** | ||
| * Executes a Workflow and waits for the results with exponential backoff. | ||
| * @param {string} projectId The Google Cloud Project containing the workflow | ||
| * @param {string} location The workflow location | ||
| * @param {string} workflow The workflow name | ||
| */ | ||
| async function executeWorkflow(projectId, location, workflow) { | ||
| /** | ||
| * Sleeps the process N number of milliseconds. | ||
| * @param {Number} ms The number of milliseconds to sleep. | ||
| */ | ||
| function sleep(ms) { | ||
| return new Promise(resolve => { | ||
| setTimeout(resolve, ms); | ||
| }); | ||
| } | ||
| // Execute workflow | ||
| try { | ||
| const createExecutionRes = await client.createExecution({ | ||
| parent: client.workflowPath(projectId, location, workflow), | ||
| }); | ||
| const executionName = createExecutionRes[0].name; | ||
| console.log(`Created execution: ${executionName}`); | ||
|
|
||
| // Wait for execution to finish, then print results. | ||
| let executionFinished = false; | ||
| let backoffDelay = 1000; // Start wait with delay of 1,000 ms | ||
| console.log('Poll every second for result...'); | ||
| while (!executionFinished) { | ||
| const [execution] = await client.getExecution({ | ||
| name: executionName, | ||
| }); | ||
| executionFinished = execution.state !== 'ACTIVE'; | ||
|
|
||
| // If we haven't seen the result yet, wait a second. | ||
| if (!executionFinished) { | ||
| console.log('- Waiting for results...'); | ||
| await sleep(backoffDelay); | ||
| backoffDelay *= 2; // Double the delay to provide exponential backoff. | ||
| } else { | ||
| console.log(`Execution finished with state: ${execution.state}`); | ||
| console.log(execution.result); | ||
| return execution.result; | ||
| } | ||
| } | ||
| } catch (e) { | ||
| console.error(`Error executing workflow: ${e}`); | ||
| } | ||
| } | ||
|
|
||
| executeWorkflow(projectId, location, workflowName).catch(err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }); | ||
|
|
||
| // [END workflows_execute_without_arguments] |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||
| // Copyright 2026 Google LLC | ||||||
| // | ||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| // you may not use this file except in compliance with the License. | ||||||
| // You may obtain a copy of the License at | ||||||
| // | ||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| // | ||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| // See the License for the specific language governing permissions and | ||||||
| // limitations under the License. | ||||||
|
|
||||||
| const projectId = | ||||||
| process.argv[2] || (process.env.GOOGLE_CLOUD_PROJECT as string); | ||||||
| const location = process.argv[3] || 'us-central1'; | ||||||
| const workflowName = process.argv[4] || 'myFirstWorkflow'; | ||||||
|
|
||||||
| // [START workflows_execute_without_arguments] | ||||||
| import {ExecutionsClient} from '@google-cloud/workflows'; | ||||||
| const client: ExecutionsClient = new ExecutionsClient(); | ||||||
|
|
||||||
| /** | ||||||
| * TODO(developer): Uncomment these variables before running the sample. | ||||||
| */ | ||||||
| // const projectId = 'my-project'; | ||||||
| // const location = 'us-central1'; | ||||||
| // const workflow = 'myFirstWorkflow'; | ||||||
|
|
||||||
| /** | ||||||
| * Executes a Workflow and waits for the results with exponential backoff. | ||||||
| * @param {string} projectId The Google Cloud Project containing the workflow | ||||||
| * @param {string} location The workflow location | ||||||
| * @param {string} workflow The workflow name | ||||||
| */ | ||||||
| async function executeWorkflow( | ||||||
| projectId: string, | ||||||
| location: string, | ||||||
| workflow: string | ||||||
| ) { | ||||||
| /** | ||||||
| * Sleeps the process N number of milliseconds. | ||||||
| * @param {Number} ms The number of milliseconds to sleep. | ||||||
| */ | ||||||
| function sleep(ms: number): Promise<unknown> { | ||||||
| return new Promise(resolve => { | ||||||
| setTimeout(resolve, ms); | ||||||
| }); | ||||||
| } | ||||||
| // Execute workflow | ||||||
| try { | ||||||
| const createExecutionRes = await client.createExecution({ | ||||||
| parent: client.workflowPath(projectId, location, workflow), | ||||||
| }); | ||||||
| const executionName = createExecutionRes[0].name; | ||||||
| console.log(`Created execution: ${executionName}`); | ||||||
|
|
||||||
| // Wait for execution to finish, then print results. | ||||||
| let executionFinished = false; | ||||||
| let backoffDelay = 1000; // Start wait with delay of 1,000 ms | ||||||
| console.log('Poll every second for result...'); | ||||||
| while (!executionFinished) { | ||||||
| const [execution] = await client.getExecution({ | ||||||
| name: executionName, | ||||||
| }); | ||||||
| executionFinished = execution.state !== 'ACTIVE'; | ||||||
|
|
||||||
| // If we haven't seen the result yet, wait a second. | ||||||
| if (!executionFinished) { | ||||||
| console.log('- Waiting for results...'); | ||||||
| await sleep(backoffDelay); | ||||||
| backoffDelay *= 2; // Double the delay to provide exponential backoff. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a best practice to cap the exponential backoff delay to prevent excessively long wait times between polling attempts if the workflow execution takes a significant amount of time.
Suggested change
|
||||||
| } else { | ||||||
| console.log(`Execution finished with state: ${execution.state}`); | ||||||
| console.log(execution.result); | ||||||
| return execution.result; | ||||||
| } | ||||||
| } | ||||||
| } catch (e) { | ||||||
| console.error(`Error executing workflow: ${e}`); | ||||||
|
Comment on lines
+79
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error is caught and logged, but not rethrown. This causes the promise returned by } catch (e) {
console.error(`Error executing workflow: ${e}`);
throw e;
} |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| executeWorkflow(projectId, location, workflowName).catch((err: Error) => { | ||||||
| console.error(err.message); | ||||||
| process.exitCode = 1; | ||||||
| }); | ||||||
| // [END workflows_execute_without_arguments] | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a best practice to cap the exponential backoff delay to prevent excessively long wait times between polling attempts if the workflow execution takes a significant amount of time.