From eddf30daf2807c5f37bf6f2c3155f7a1f343a326 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 23 May 2017 22:56:40 +0200 Subject: [PATCH] build: properly close firebase apps (#4743) * Properly close the Firebase app connections to avoid that the NodeJS process keeps hanging. --- tools/gulp/tasks/coverage.ts | 10 ++++++---- tools/gulp/tasks/payload.ts | 10 ++++++---- tools/gulp/tasks/screenshots.ts | 9 +++++---- tools/gulp/util/firebase.ts | 8 +++----- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tools/gulp/tasks/coverage.ts b/tools/gulp/tasks/coverage.ts index a7832db58a6..6e9a52887a0 100644 --- a/tools/gulp/tasks/coverage.ts +++ b/tools/gulp/tasks/coverage.ts @@ -3,7 +3,7 @@ import {existsSync} from 'fs-extra'; import {COVERAGE_RESULT_FILE} from '../constants'; import {spawnSync} from 'child_process'; import {isTravisMasterBuild} from '../util/travis-ci'; -import {openFirebaseDashboardDatabase} from '../util/firebase'; +import {openFirebaseDashboardApp} from '../util/firebase'; task('coverage:upload', () => { if (!existsSync(COVERAGE_RESULT_FILE)) { @@ -24,11 +24,13 @@ task('coverage:upload', () => { /** Uploads the coverage results to the firebase database. */ function uploadResults(results: any): Promise { - let latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim(); - let database = openFirebaseDashboardDatabase(); + const latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim(); + const dashboardApp = openFirebaseDashboardApp(); + const database = dashboardApp.database(); return database.ref('coverage-reports').child(latestSha).set(results) - .then(() => database.goOffline(), () => database.goOffline()); + .catch((err: any) => console.error(err)) + .then(() => dashboardApp.delete()); } // TODO(devversion): In the future we might have a big database where we can store full summaries. diff --git a/tools/gulp/tasks/payload.ts b/tools/gulp/tasks/payload.ts index bdcc74484bc..da5071d80d3 100644 --- a/tools/gulp/tasks/payload.ts +++ b/tools/gulp/tasks/payload.ts @@ -4,7 +4,7 @@ import {statSync} from 'fs'; import {DIST_ROOT} from '../constants'; import {spawnSync} from 'child_process'; import {isTravisMasterBuild} from '../util/travis-ci'; -import {openFirebaseDashboardDatabase} from '../util/firebase'; +import {openFirebaseDashboardApp} from '../util/firebase'; const bundlesDir = join(DIST_ROOT, 'bundles'); @@ -47,10 +47,12 @@ function getFilesize(filePath: string) { /** Publishes the given results to the firebase database. */ function publishResults(results: any) { - let latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim(); - let database = openFirebaseDashboardDatabase(); + const latestSha = spawnSync('git', ['rev-parse', 'HEAD']).stdout.toString().trim(); + const dashboardApp = openFirebaseDashboardApp(); + const database = dashboardApp.database(); // Write the results to the payloads object with the latest Git SHA as key. return database.ref('payloads').child(latestSha).set(results) - .then(() => database.goOffline(), () => database.goOffline()); + .catch((err: any) => console.error(err)) + .then(() => dashboardApp.delete()); } diff --git a/tools/gulp/tasks/screenshots.ts b/tools/gulp/tasks/screenshots.ts index 237bb979068..8f9f1f5db6d 100644 --- a/tools/gulp/tasks/screenshots.ts +++ b/tools/gulp/tasks/screenshots.ts @@ -25,14 +25,14 @@ const FIREBASE_STORAGE_GOLDENS = 'goldens'; /** Task which upload screenshots generated from e2e test. */ task('screenshots', () => { - let prNumber = process.env['TRAVIS_PULL_REQUEST']; + const prNumber = process.env['TRAVIS_PULL_REQUEST']; if (isTravisMasterBuild()) { // Only update goldens for master build return uploadScreenshots(); } else if (prNumber) { - let firebaseApp = connectFirebaseScreenshots(); - let database = firebaseApp.database(); + const firebaseApp = connectFirebaseScreenshots(); + const database = firebaseApp.database(); return updateTravis(database, prNumber) .then(() => getScreenshotFiles(database)) @@ -40,7 +40,8 @@ task('screenshots', () => { .then((results: boolean) => updateResult(database, prNumber, results)) .then(() => uploadScreenshotsData(database, 'diff', prNumber)) .then(() => uploadScreenshotsData(database, 'test', prNumber)) - .then(() => database.goOffline(), () => database.goOffline()); + .catch((err: any) => console.error(err)) + .then(() => firebaseApp.delete()); } }); diff --git a/tools/gulp/util/firebase.ts b/tools/gulp/util/firebase.ts index 1c8cf5e58e8..ff2f3321dfd 100644 --- a/tools/gulp/util/firebase.ts +++ b/tools/gulp/util/firebase.ts @@ -5,11 +5,11 @@ const cloudStorage = require('@google-cloud/storage'); // Firebase configuration for the Screenshot project. Use the config from the screenshot functions. const screenshotFirebaseConfig = require('../../screenshot-test/functions/config.json'); -/** Opens a connection to the firebase realtime database. */ -export function openFirebaseDashboardDatabase() { +/** Opens a connection to the Firebase dashboard app. */ +export function openFirebaseDashboardApp() { // Initialize the Firebase application with firebaseAdmin credentials. // Credentials need to be for a Service Account, which can be created in the Firebase console. - firebaseAdmin.initializeApp({ + return firebaseAdmin.initializeApp({ credential: firebaseAdmin.credential.cert({ project_id: 'material2-dashboard', client_email: 'firebase-adminsdk-ch1ob@material2-dashboard.iam.gserviceaccount.com', @@ -19,8 +19,6 @@ export function openFirebaseDashboardDatabase() { }), databaseURL: 'https://material2-dashboard.firebaseio.com' }); - - return firebaseAdmin.database(); } /**