Skip to content

Commit eddf30d

Browse files
devversiontinayuangao
authored andcommitted
build: properly close firebase apps (#4743)
* Properly close the Firebase app connections to avoid that the NodeJS process keeps hanging.
1 parent 79c9c85 commit eddf30d

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

tools/gulp/tasks/coverage.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {existsSync} from 'fs-extra';
33
import {COVERAGE_RESULT_FILE} from '../constants';
44
import {spawnSync} from 'child_process';
55
import {isTravisMasterBuild} from '../util/travis-ci';
6-
import {openFirebaseDashboardDatabase} from '../util/firebase';
6+
import {openFirebaseDashboardApp} from '../util/firebase';
77

88
task('coverage:upload', () => {
99
if (!existsSync(COVERAGE_RESULT_FILE)) {
@@ -24,11 +24,13 @@ task('coverage:upload', () => {
2424

2525
/** Uploads the coverage results to the firebase database. */
2626
function uploadResults(results: any): Promise<void> {
27-
let latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim();
28-
let database = openFirebaseDashboardDatabase();
27+
const latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim();
28+
const dashboardApp = openFirebaseDashboardApp();
29+
const database = dashboardApp.database();
2930

3031
return database.ref('coverage-reports').child(latestSha).set(results)
31-
.then(() => database.goOffline(), () => database.goOffline());
32+
.catch((err: any) => console.error(err))
33+
.then(() => dashboardApp.delete());
3234
}
3335

3436
// TODO(devversion): In the future we might have a big database where we can store full summaries.

tools/gulp/tasks/payload.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {statSync} from 'fs';
44
import {DIST_ROOT} from '../constants';
55
import {spawnSync} from 'child_process';
66
import {isTravisMasterBuild} from '../util/travis-ci';
7-
import {openFirebaseDashboardDatabase} from '../util/firebase';
7+
import {openFirebaseDashboardApp} from '../util/firebase';
88

99
const bundlesDir = join(DIST_ROOT, 'bundles');
1010

@@ -47,10 +47,12 @@ function getFilesize(filePath: string) {
4747

4848
/** Publishes the given results to the firebase database. */
4949
function publishResults(results: any) {
50-
let latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim();
51-
let database = openFirebaseDashboardDatabase();
50+
const latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim();
51+
const dashboardApp = openFirebaseDashboardApp();
52+
const database = dashboardApp.database();
5253

5354
// Write the results to the payloads object with the latest Git SHA as key.
5455
return database.ref('payloads').child(latestSha).set(results)
55-
.then(() => database.goOffline(), () => database.goOffline());
56+
.catch((err: any) => console.error(err))
57+
.then(() => dashboardApp.delete());
5658
}

tools/gulp/tasks/screenshots.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ const FIREBASE_STORAGE_GOLDENS = 'goldens';
2525

2626
/** Task which upload screenshots generated from e2e test. */
2727
task('screenshots', () => {
28-
let prNumber = process.env['TRAVIS_PULL_REQUEST'];
28+
const prNumber = process.env['TRAVIS_PULL_REQUEST'];
2929

3030
if (isTravisMasterBuild()) {
3131
// Only update goldens for master build
3232
return uploadScreenshots();
3333
} else if (prNumber) {
34-
let firebaseApp = connectFirebaseScreenshots();
35-
let database = firebaseApp.database();
34+
const firebaseApp = connectFirebaseScreenshots();
35+
const database = firebaseApp.database();
3636

3737
return updateTravis(database, prNumber)
3838
.then(() => getScreenshotFiles(database))
3939
.then(() => downloadAllGoldsAndCompare(database, prNumber))
4040
.then((results: boolean) => updateResult(database, prNumber, results))
4141
.then(() => uploadScreenshotsData(database, 'diff', prNumber))
4242
.then(() => uploadScreenshotsData(database, 'test', prNumber))
43-
.then(() => database.goOffline(), () => database.goOffline());
43+
.catch((err: any) => console.error(err))
44+
.then(() => firebaseApp.delete());
4445
}
4546
});
4647

tools/gulp/util/firebase.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const cloudStorage = require('@google-cloud/storage');
55
// Firebase configuration for the Screenshot project. Use the config from the screenshot functions.
66
const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json');
77

8-
/** Opens a connection to the firebase realtime database. */
9-
export function openFirebaseDashboardDatabase() {
8+
/** Opens a connection to the Firebase dashboard app. */
9+
export function openFirebaseDashboardApp() {
1010
// Initialize the Firebase application with firebaseAdmin credentials.
1111
// Credentials need to be for a Service Account, which can be created in the Firebase console.
12-
firebaseAdmin.initializeApp({
12+
return firebaseAdmin.initializeApp({
1313
credential: firebaseAdmin.credential.cert({
1414
project_id: 'material2-dashboard',
1515
client_email: 'firebase-adminsdk-ch1ob@material2-dashboard.iam.gserviceaccount.com',
@@ -19,8 +19,6 @@ export function openFirebaseDashboardDatabase() {
1919
}),
2020
databaseURL: 'https://material2-dashboard.firebaseio.com'
2121
});
22-
23-
return firebaseAdmin.database();
2422
}
2523

2624
/**

0 commit comments

Comments
 (0)