Skip to content

Commit

Permalink
refactor(workflows): change how runtimeArgs are defined in workflows …
Browse files Browse the repository at this point in the history
…quickstart (#3461)

* define runtimeArgs outside of execute function

* add runtimeargs as execute function param

* add missing param

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* lint

* typo

* give runtime args default values

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* reincorporate searchTerm param into function

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* update var description one more time

* somehow forgot to type the typescript

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* forgot to readd the new region tag

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* indent

* remove region tags from this PR

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* rm runtime args tags from ts too

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* add type

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* move json stringify

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* add execution and libraries region tags

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* lint

* add type

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* add type again again

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Patti Shin <pattishin@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 22, 2023
1 parent 5383ced commit 815d72c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
21 changes: 12 additions & 9 deletions workflows/quickstart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,29 @@
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] || null;
const searchTerm = process.argv[5] || '';

// [START workflows_api_quickstart]
// [START workflows_api_quickstart_client_libraries]
const {ExecutionsClient} = require('@google-cloud/workflows');
const client = new ExecutionsClient();

// [END workflows_api_quickstart_client_libraries]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project';
// const location = 'us-central1';
// const workflow = 'myFirstWorkflow';
// const searchTerm = null;
// 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 as runtime argument to Workflow
* @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument
*/
async function executeWorkflow(projectId, location, workflow) {
async function executeWorkflow(projectId, location, workflow, searchTerm) {
/**
* Sleeps the process N number of milliseconds.
* @param {Number} ms The number of milliseconds to sleep.
Expand All @@ -50,14 +51,14 @@ async function executeWorkflow(projectId, location, workflow) {
setTimeout(resolve, ms);
});
}

const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
// [START workflows_api_quickstart_execution]
// Execute workflow
try {
const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
// Provide runtime arguments as a JSON string
// Runtime arguments can be passed as a JSON string
argument: JSON.stringify(runtimeArgs),
},
});
Expand Down Expand Up @@ -88,10 +89,12 @@ async function executeWorkflow(projectId, location, workflow) {
} catch (e) {
console.error(`Error executing workflow: ${e}`);
}
// [END workflows_api_quickstart_execution]
}

executeWorkflow(projectId, location, workflowName).catch(err => {
executeWorkflow(projectId, location, workflowName, searchTerm).catch(err => {
console.error(err.message);
process.exitCode = 1;
});

// [END workflows_api_quickstart]
29 changes: 17 additions & 12 deletions workflows/quickstart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@ 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] || null;
const searchTerm = process.argv[5] || '';

// [START workflows_api_quickstart]
// [START workflows_api_quickstart_client_libraries]
import {ExecutionsClient} from '@google-cloud/workflows';
const client: ExecutionsClient = new ExecutionsClient();

// [END workflows_api_quickstart_client_libraries]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project';
// const location = 'us-central1';
// const workflow = 'myFirstWorkflow';
// const searchTerm = null;
// 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 as runtime argument to Workflow
* @param {string} searchTerm Optional search term to pass to the Workflow as a runtime argument
*/
async function executeWorkflow(
projectId: string,
location: string,
workflow: string
workflow: string,
searchTerm: string
) {
/**
* Sleeps the process N number of milliseconds.
Expand All @@ -51,14 +53,14 @@ async function executeWorkflow(
setTimeout(resolve, ms);
});
}

const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
// [START workflows_api_quickstart_execution]
// Execute workflow
try {
const runtimeArgs = searchTerm ? {searchTerm: searchTerm} : {};
const createExecutionRes = await client.createExecution({
parent: client.workflowPath(projectId, location, workflow),
execution: {
// Provide runtime arguments as a JSON string
// Runtime arguments can be passed as a JSON string
argument: JSON.stringify(runtimeArgs),
},
});
Expand Down Expand Up @@ -89,10 +91,13 @@ async function executeWorkflow(
} catch (e) {
console.error(`Error executing workflow: ${e}`);
}
// [END workflows_api_quickstart_execution]
}

executeWorkflow(projectId, location, workflowName).catch((err: Error) => {
console.error(err.message);
process.exitCode = 1;
});
executeWorkflow(projectId, location, workflowName, searchTerm).catch(
(err: Error) => {
console.error(err.message);
process.exitCode = 1;
}
);
// [END workflows_api_quickstart]

0 comments on commit 815d72c

Please sign in to comment.