Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions workflows/quickstart/executeWithArguments.js
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]
99 changes: 99 additions & 0 deletions workflows/quickstart/executeWithArguments.ts
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}`);
}
}

executeWorkflow(projectId, location, workflowName, searchTerm).catch(
(err: Error) => {
console.error(err.message);
process.exitCode = 1;
}
);
// [END workflows_execute_with_arguments]
89 changes: 89 additions & 0 deletions workflows/quickstart/executeWithoutArguments.js
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]
89 changes: 89 additions & 0 deletions workflows/quickstart/executeWithoutArguments.ts
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.
} 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: Error) => {
console.error(err.message);
process.exitCode = 1;
});
// [END workflows_execute_without_arguments]
5 changes: 3 additions & 2 deletions workflows/quickstart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "tsc -p .",
"fix": "gts fix",
"lint": "gts lint",
"test": "mocha --loader=ts-node/esm --extension ts --timeout 600000 --exit"
"test": "mocha --require ts-node/register --extension ts,js --timeout 600000 --exit"
},
"dependencies": {
"@google-cloud/workflows": "^3.0.0",
Expand All @@ -21,6 +21,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^20.0.0",
"gts": "^5.0.0",
"mocha": "^10.2.0"
"mocha": "^10.2.0",
"ts-node": "^10.9.2"
}
}
Loading
Loading