15
15
const assert = require ( 'assert' ) ;
16
16
const request = require ( 'got' ) ;
17
17
const { Logging} = require ( '@google-cloud/logging' ) ;
18
- const { execSync } = require ( 'child_process ' ) ;
18
+ const { exec } = require ( 'child-process-promise ' ) ;
19
19
const { GoogleAuth} = require ( 'google-auth-library' ) ;
20
20
const auth = new GoogleAuth ( ) ;
21
21
@@ -66,6 +66,31 @@ function sleep(ms) {
66
66
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
67
67
}
68
68
69
+ // Run shell commands with retries and exponential backoff
70
+ async function runShellCmd ( cmd , opts = { } ) {
71
+ const maxAttempts = opts . maxAttempts || 10 ;
72
+ let attempt = 0 ;
73
+ while ( attempt < maxAttempts ) {
74
+ try {
75
+ console . log ( 'Running command:' , cmd ) ;
76
+ const result = await exec ( cmd , opts ) ;
77
+ return result ;
78
+ } catch ( err ) {
79
+ console . log ( 'Shell command failed!' ) ;
80
+ console . log ( '\tCommand:' , cmd ) ;
81
+ console . log ( '\tError:' , err ) ;
82
+ attempt += 1 ;
83
+
84
+ if ( attempt < maxAttempts ) {
85
+ // Exponential backoff
86
+ await sleep ( attempt ** 2 * 1000 ) ;
87
+ } else {
88
+ throw err ;
89
+ }
90
+ }
91
+ }
92
+ }
93
+
69
94
describe ( 'Logging' , ( ) => {
70
95
let requestLog ;
71
96
let sampleLog ;
@@ -96,14 +121,16 @@ describe('Logging', () => {
96
121
if ( SAMPLE_VERSION ) buildCmd += `,_VERSION=${ SAMPLE_VERSION } ` ;
97
122
98
123
console . log ( 'Starting Cloud Build...' ) ;
99
- execSync ( buildCmd , { timeout : 240000 } ) ; // timeout at 4 mins
124
+ runShellCmd ( buildCmd , { timeout : 240000 } ) ; // timeout at 4 mins
100
125
console . log ( 'Cloud Build completed.' ) ;
101
126
102
127
// Retrieve URL of Cloud Run service
103
- const url = execSync (
128
+ const proc = await runShellCmd (
104
129
`gcloud run services describe ${ SERVICE_NAME } --project=${ GOOGLE_CLOUD_PROJECT } ` +
105
130
`--platform=${ PLATFORM } --region=${ REGION } --format='value(status.url)'`
106
131
) ;
132
+ const url = proc . stdout ;
133
+ console . log ( 'Read URL:' , url ) ;
107
134
108
135
BASE_URL = url . toString ( 'utf-8' ) . trim ( ) ;
109
136
if ( ! BASE_URL ) throw Error ( 'Cloud Run service URL not found' ) ;
@@ -122,7 +149,9 @@ describe('Logging', () => {
122
149
`--substitutions _SERVICE=${ SERVICE_NAME } ,_PLATFORM=${ PLATFORM } ,_REGION=${ REGION } ` ;
123
150
if ( SAMPLE_VERSION ) cleanUpCmd += `,_VERSION=${ SAMPLE_VERSION } ` ;
124
151
125
- execSync ( cleanUpCmd ) ;
152
+ console . log ( 'Cleaning up via Cloud Build.' ) ;
153
+ runShellCmd ( cleanUpCmd ) ;
154
+ console . log ( 'Cleanup completed.' ) ;
126
155
} ) ;
127
156
128
157
it ( 'can be reached by an HTTP request' , async ( ) => {
@@ -152,6 +181,7 @@ describe('Logging', () => {
152
181
let attempt = 0 ;
153
182
const maxAttempts = 5 ;
154
183
while ( ( ! requestLog || ! sampleLog ) && attempt < maxAttempts ) {
184
+ console . log ( `Fetching logs, attempt #${ attempt } ` ) ;
155
185
await sleep ( attempt * 30000 ) ; // Linear backoff between retry attempts
156
186
// Filter by service name over the last 5 minutes
157
187
const filter = `resource.labels.service_name="${ service_name } " timestamp>="${ dateMinutesAgo (
0 commit comments