-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.ts
90 lines (82 loc) · 3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2021 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_api_quickstart]
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_api_quickstart]