Skip to content

Commit

Permalink
fix(server): reject on server initialization errors (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Apr 12, 2021
1 parent 10e873a commit a126b09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/server/src/api/storage/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ class SqlStorageMethod {
if (!statisticModelDefn.attributes.projectId.references) throw new Error('Invalid runModel');
if (!statisticModelDefn.attributes.buildId.references) throw new Error('Invalid runModel');

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

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

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

const umzug = createUmzug(sequelize, options);
if (options.sqlDangerouslyResetDatabase) {
log('[initialize] resetting database');
await umzug.down({to: 0});
}

log('[initialize] running migrations');
await umzug.up();
log('[initialize] migrations performed');

this._sequelize = {sequelize, projectModel, buildModel, runModel, statisticModel};
}
Expand Down
8 changes: 7 additions & 1 deletion packages/server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

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

log('[createApp] initializing storage method');
const storageMethod = StorageMethod.from(storage);
await storageMethod.initialize(storage);

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

log('[createApp] launching cron jobs');
startPsiCollectCron(storageMethod, options);
startDeleteOldBuildsCron(storageMethod, options);

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

return new Promise(resolve => {
return new Promise((resolve, reject) => {
const server = createHttpServer(app);

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

server.on('error', err => reject(err));

server.listen(options.port, () => {
const serverAddress = server.address();
const listenPort =
Expand Down

0 comments on commit a126b09

Please sign in to comment.