Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): reject on server initialization errors #584

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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