Skip to content

Commit 6323a73

Browse files
committed
feat: add workflow execution samples with and without arguments
1 parent 6a530b5 commit 6323a73

10 files changed

Lines changed: 632 additions & 24 deletions
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2026 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+
// This is a generated sample, using the typeless sample bot. Please
16+
// look for the source TypeScript sample (.ts) for modifications.
17+
'use strict';
18+
19+
const projectId = process.argv[2] || process.env.GOOGLE_CLOUD_PROJECT;
20+
const location = process.argv[3] || 'us-central1';
21+
const workflowName = process.argv[4] || 'myFirstWorkflow';
22+
const searchTerm = process.argv[5] || '';
23+
24+
// [START workflows_execute_with_arguments]
25+
const {ExecutionsClient} = require('@google-cloud/workflows');
26+
const client = new ExecutionsClient();
27+
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'my-project';
32+
// const location = 'us-central1';
33+
// const workflow = 'myFirstWorkflow';
34+
// const searchTerm = '';
35+
36+
/**
37+
* Executes a Workflow and waits for the results with exponential backoff.
38+
* @param {string} projectId The Google Cloud Project containing the workflow
39+
* @param {string} location The workflow location
40+
* @param {string} workflow The workflow name
41+
* @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument
42+
*/
43+
async function executeWorkflow(projectId, location, workflow, searchTerm) {
44+
/**
45+
* Sleeps the process N number of milliseconds.
46+
* @param {Number} ms The number of milliseconds to sleep.
47+
*/
48+
function sleep(ms) {
49+
return new Promise(resolve => {
50+
setTimeout(resolve, ms);
51+
});
52+
}
53+
const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
54+
// Execute workflow
55+
try {
56+
const createExecutionRes = await client.createExecution({
57+
parent: client.workflowPath(projectId, location, workflow),
58+
execution: {
59+
// Runtime arguments can be passed as a JSON string
60+
argument: JSON.stringify(runtimeArgs),
61+
},
62+
});
63+
const executionName = createExecutionRes[0].name;
64+
console.log(`Created execution: ${executionName}`);
65+
66+
// Wait for execution to finish, then print results.
67+
let executionFinished = false;
68+
let backoffDelay = 1000; // Start wait with delay of 1,000 ms
69+
console.log('Poll every second for result...');
70+
while (!executionFinished) {
71+
const [execution] = await client.getExecution({
72+
name: executionName,
73+
});
74+
executionFinished = execution.state !== 'ACTIVE';
75+
76+
// If we haven't seen the result yet, wait a second.
77+
if (!executionFinished) {
78+
console.log('- Waiting for results...');
79+
await sleep(backoffDelay);
80+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
81+
} else {
82+
console.log(`Execution finished with state: ${execution.state}`);
83+
console.log(execution.result);
84+
return execution.result;
85+
}
86+
}
87+
} catch (e) {
88+
console.error(`Error executing workflow: ${e}`);
89+
}
90+
}
91+
92+
executeWorkflow(projectId, location, workflowName, searchTerm).catch(err => {
93+
console.error(err.message);
94+
process.exitCode = 1;
95+
});
96+
97+
// [END workflows_execute_with_arguments]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2026 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+
const projectId =
16+
process.argv[2] || (process.env.GOOGLE_CLOUD_PROJECT as string);
17+
const location = process.argv[3] || 'us-central1';
18+
const workflowName = process.argv[4] || 'myFirstWorkflow';
19+
const searchTerm = process.argv[5] || '';
20+
21+
// [START workflows_execute_with_arguments]
22+
import {ExecutionsClient} from '@google-cloud/workflows';
23+
const client: ExecutionsClient = new ExecutionsClient();
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'my-project';
28+
// const location = 'us-central1';
29+
// const workflow = 'myFirstWorkflow';
30+
// const searchTerm = '';
31+
32+
/**
33+
* Executes a Workflow and waits for the results with exponential backoff.
34+
* @param {string} projectId The Google Cloud Project containing the workflow
35+
* @param {string} location The workflow location
36+
* @param {string} workflow The workflow name
37+
* @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument
38+
*/
39+
async function executeWorkflow(
40+
projectId: string,
41+
location: string,
42+
workflow: string,
43+
searchTerm: string
44+
) {
45+
/**
46+
* Sleeps the process N number of milliseconds.
47+
* @param {Number} ms The number of milliseconds to sleep.
48+
*/
49+
function sleep(ms: number): Promise<unknown> {
50+
return new Promise(resolve => {
51+
setTimeout(resolve, ms);
52+
});
53+
}
54+
const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
55+
// Execute workflow
56+
try {
57+
const createExecutionRes = await client.createExecution({
58+
parent: client.workflowPath(projectId, location, workflow),
59+
execution: {
60+
// Runtime arguments can be passed as a JSON string
61+
argument: JSON.stringify(runtimeArgs),
62+
},
63+
});
64+
const executionName = createExecutionRes[0].name;
65+
console.log(`Created execution: ${executionName}`);
66+
67+
// Wait for execution to finish, then print results.
68+
let executionFinished = false;
69+
let backoffDelay = 1000; // Start wait with delay of 1,000 ms
70+
console.log('Poll every second for result...');
71+
while (!executionFinished) {
72+
const [execution] = await client.getExecution({
73+
name: executionName,
74+
});
75+
executionFinished = execution.state !== 'ACTIVE';
76+
77+
// If we haven't seen the result yet, wait a second.
78+
if (!executionFinished) {
79+
console.log('- Waiting for results...');
80+
await sleep(backoffDelay);
81+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
82+
} else {
83+
console.log(`Execution finished with state: ${execution.state}`);
84+
console.log(execution.result);
85+
return execution.result;
86+
}
87+
}
88+
} catch (e) {
89+
console.error(`Error executing workflow: ${e}`);
90+
}
91+
}
92+
93+
executeWorkflow(projectId, location, workflowName, searchTerm).catch(
94+
(err: Error) => {
95+
console.error(err.message);
96+
process.exitCode = 1;
97+
}
98+
);
99+
// [END workflows_execute_with_arguments]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2026 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+
// This is a generated sample, using the typeless sample bot. Please
16+
// look for the source TypeScript sample (.ts) for modifications.
17+
'use strict';
18+
19+
const projectId = process.argv[2] || process.env.GOOGLE_CLOUD_PROJECT;
20+
const location = process.argv[3] || 'us-central1';
21+
const workflowName = process.argv[4] || 'myFirstWorkflow';
22+
23+
// [START workflows_execute_without_arguments]
24+
const {ExecutionsClient} = require('@google-cloud/workflows');
25+
const client = new ExecutionsClient();
26+
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const projectId = 'my-project';
31+
// const location = 'us-central1';
32+
// const workflow = 'myFirstWorkflow';
33+
34+
/**
35+
* Executes a Workflow and waits for the results with exponential backoff.
36+
* @param {string} projectId The Google Cloud Project containing the workflow
37+
* @param {string} location The workflow location
38+
* @param {string} workflow The workflow name
39+
*/
40+
async function executeWorkflow(projectId, location, workflow) {
41+
/**
42+
* Sleeps the process N number of milliseconds.
43+
* @param {Number} ms The number of milliseconds to sleep.
44+
*/
45+
function sleep(ms) {
46+
return new Promise(resolve => {
47+
setTimeout(resolve, ms);
48+
});
49+
}
50+
// Execute workflow
51+
try {
52+
const createExecutionRes = await client.createExecution({
53+
parent: client.workflowPath(projectId, location, workflow),
54+
});
55+
const executionName = createExecutionRes[0].name;
56+
console.log(`Created execution: ${executionName}`);
57+
58+
// Wait for execution to finish, then print results.
59+
let executionFinished = false;
60+
let backoffDelay = 1000; // Start wait with delay of 1,000 ms
61+
console.log('Poll every second for result...');
62+
while (!executionFinished) {
63+
const [execution] = await client.getExecution({
64+
name: executionName,
65+
});
66+
executionFinished = execution.state !== 'ACTIVE';
67+
68+
// If we haven't seen the result yet, wait a second.
69+
if (!executionFinished) {
70+
console.log('- Waiting for results...');
71+
await sleep(backoffDelay);
72+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
73+
} else {
74+
console.log(`Execution finished with state: ${execution.state}`);
75+
console.log(execution.result);
76+
return execution.result;
77+
}
78+
}
79+
} catch (e) {
80+
console.error(`Error executing workflow: ${e}`);
81+
}
82+
}
83+
84+
executeWorkflow(projectId, location, workflowName).catch(err => {
85+
console.error(err.message);
86+
process.exitCode = 1;
87+
});
88+
89+
// [END workflows_execute_without_arguments]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2026 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+
const projectId =
16+
process.argv[2] || (process.env.GOOGLE_CLOUD_PROJECT as string);
17+
const location = process.argv[3] || 'us-central1';
18+
const workflowName = process.argv[4] || 'myFirstWorkflow';
19+
20+
// [START workflows_execute_without_arguments]
21+
import {ExecutionsClient} from '@google-cloud/workflows';
22+
const client: ExecutionsClient = new ExecutionsClient();
23+
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'my-project';
28+
// const location = 'us-central1';
29+
// const workflow = 'myFirstWorkflow';
30+
31+
/**
32+
* Executes a Workflow and waits for the results with exponential backoff.
33+
* @param {string} projectId The Google Cloud Project containing the workflow
34+
* @param {string} location The workflow location
35+
* @param {string} workflow The workflow name
36+
*/
37+
async function executeWorkflow(
38+
projectId: string,
39+
location: string,
40+
workflow: string
41+
) {
42+
/**
43+
* Sleeps the process N number of milliseconds.
44+
* @param {Number} ms The number of milliseconds to sleep.
45+
*/
46+
function sleep(ms: number): Promise<unknown> {
47+
return new Promise(resolve => {
48+
setTimeout(resolve, ms);
49+
});
50+
}
51+
// Execute workflow
52+
try {
53+
const createExecutionRes = await client.createExecution({
54+
parent: client.workflowPath(projectId, location, workflow),
55+
});
56+
const executionName = createExecutionRes[0].name;
57+
console.log(`Created execution: ${executionName}`);
58+
59+
// Wait for execution to finish, then print results.
60+
let executionFinished = false;
61+
let backoffDelay = 1000; // Start wait with delay of 1,000 ms
62+
console.log('Poll every second for result...');
63+
while (!executionFinished) {
64+
const [execution] = await client.getExecution({
65+
name: executionName,
66+
});
67+
executionFinished = execution.state !== 'ACTIVE';
68+
69+
// If we haven't seen the result yet, wait a second.
70+
if (!executionFinished) {
71+
console.log('- Waiting for results...');
72+
await sleep(backoffDelay);
73+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
74+
} else {
75+
console.log(`Execution finished with state: ${execution.state}`);
76+
console.log(execution.result);
77+
return execution.result;
78+
}
79+
}
80+
} catch (e) {
81+
console.error(`Error executing workflow: ${e}`);
82+
}
83+
}
84+
85+
executeWorkflow(projectId, location, workflowName).catch((err: Error) => {
86+
console.error(err.message);
87+
process.exitCode = 1;
88+
});
89+
// [END workflows_execute_without_arguments]

workflows/quickstart/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build": "tsc -p .",
1212
"fix": "gts fix",
1313
"lint": "gts lint",
14-
"test": "mocha --loader=ts-node/esm --extension ts --timeout 600000 --exit"
14+
"test": "mocha --require ts-node/register --extension ts,js --timeout 600000 --exit"
1515
},
1616
"dependencies": {
1717
"@google-cloud/workflows": "^3.0.0",
@@ -21,6 +21,7 @@
2121
"@types/mocha": "^10.0.1",
2222
"@types/node": "^20.0.0",
2323
"gts": "^5.0.0",
24-
"mocha": "^10.2.0"
24+
"mocha": "^10.2.0",
25+
"ts-node": "^10.9.2"
2526
}
2627
}

0 commit comments

Comments
 (0)