|
| 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] |
0 commit comments