build: properly close firebase apps (#4743)

* Properly close the Firebase app connections to avoid that the NodeJS process keeps hanging.
This commit is contained in:
Paul Gschwendtner
2017-05-23 22:56:40 +02:00
committed by tinayuangao
parent 79c9c85912
commit eddf30daf2
4 changed files with 20 additions and 17 deletions
+6 -4
View File
@@ -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<void> {
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.
+6 -4
View File
@@ -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());
}
+5 -4
View File
@@ -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());
}
});
+3 -5
View File
@@ -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();
}
/**