Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a126b09

Browse files
authoredApr 12, 2021
fix(server): reject on server initialization errors (#584)
1 parent 10e873a commit a126b09

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
 

‎packages/server/src/api/storage/sql/sql.js

+5
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ class SqlStorageMethod {
225225
if (!statisticModelDefn.attributes.projectId.references) throw new Error('Invalid runModel');
226226
if (!statisticModelDefn.attributes.buildId.references) throw new Error('Invalid runModel');
227227

228+
log('[initialize] initializing database connection');
228229
const sequelize = createSequelize(options);
229230

231+
log('[initialize] defining models');
230232
const projectModel = sequelize.define(projectModelDefn.tableName, projectModelDefn.attributes);
231233

232234
buildModelDefn.attributes.projectId.references.model = projectModel;
@@ -245,10 +247,13 @@ class SqlStorageMethod {
245247

246248
const umzug = createUmzug(sequelize, options);
247249
if (options.sqlDangerouslyResetDatabase) {
250+
log('[initialize] resetting database');
248251
await umzug.down({to: 0});
249252
}
250253

254+
log('[initialize] running migrations');
251255
await umzug.up();
256+
log('[initialize] migrations performed');
252257

253258
this._sequelize = {sequelize, projectModel, buildModel, runModel, statisticModel};
254259
}

‎packages/server/src/server.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/** @typedef {{port: number, close: () => Promise<void>, storageMethod: StorageMethod}} ServerInstance */
99

10+
const log = require('debug')('lhci:server:sql');
1011
const path = require('path');
1112
const createHttpServer = require('http').createServer;
1213
const express = require('express');
@@ -30,9 +31,11 @@ const DIST_FOLDER = path.join(__dirname, '../dist');
3031
async function createApp(options) {
3132
const {storage} = options;
3233

34+
log('[createApp] initializing storage method');
3335
const storageMethod = StorageMethod.from(storage);
3436
await storageMethod.initialize(storage);
3537

38+
log('[createApp] creating express app');
3639
const context = {storageMethod, options};
3740
const app = express();
3841
if (options.logLevel !== 'silent') app.use(morgan('short'));
@@ -58,6 +61,7 @@ async function createApp(options) {
5861
app.get('/app/*', (_, res) => res.sendFile(path.join(DIST_FOLDER, 'index.html')));
5962
app.use(errorMiddleware);
6063

64+
log('[createApp] launching cron jobs');
6165
startPsiCollectCron(storageMethod, options);
6266
startDeleteOldBuildsCron(storageMethod, options);
6367

@@ -71,7 +75,7 @@ async function createApp(options) {
7175
async function createServer(options) {
7276
const {app, storageMethod} = await createApp(options);
7377

74-
return new Promise(resolve => {
78+
return new Promise((resolve, reject) => {
7579
const server = createHttpServer(app);
7680

7781
// Node default socket timeout is 2 minutes.
@@ -88,6 +92,8 @@ async function createServer(options) {
8892
});
8993
});
9094

95+
server.on('error', err => reject(err));
96+
9197
server.listen(options.port, () => {
9298
const serverAddress = server.address();
9399
const listenPort =

0 commit comments

Comments
 (0)
Please sign in to comment.