Merge pull request #449 from lbryio/startup-fixes

Startup fixes
This commit is contained in:
Bill Bittner 2018-05-17 20:34:07 -07:00 committed by GitHub
commit 7d69c1fb90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 10 deletions

View file

@ -25,12 +25,11 @@ function LoggerConfig () {
});
// test all the log levels
logger.info('testing winston log levels...');
logger.error('Level 0');
logger.warn('Level 1');
logger.info('Level 2');
logger.verbose('Level 3');
logger.debug('Level 4');
logger.silly('Level 5');
logger.warn('Testing: Log Level 1');
logger.info('Testing: Log Level 2');
logger.verbose('Testing: Log Level 3');
logger.debug('Testing: Log Level 4');
logger.silly('Testing: Log Level 5');
};
};

View file

@ -13,6 +13,8 @@ const loggerConfig = require('./config/loggerConfig.js');
const mysqlConfig = require('./config/mysqlConfig.js');
const siteConfig = require('./config/siteConfig.js');
const slackConfig = require('./config/slackConfig.js');
const createDatabaseIfNotExists = require('./server/models/utils/createDatabaseIfNotExists.js');
const { getWalletBalance } = require('./server/lbrynet');
function Server () {
this.configureLogger = loggerConfig.update;
@ -81,15 +83,24 @@ function Server () {
const db = require('./server/models');
const PORT = siteConfig.details.port;
// sync sequelize
db.sequelize.sync()
// start the server
createDatabaseIfNotExists()
.then(() => {
return getWalletBalance();
})
.then(balance => {
logger.info('starting LBC balance:', balance);
db.sequelize.sync();
this.server.listen(PORT, () => {
logger.info(`Server is listening on PORT ${PORT}`);
});
})
.catch((error) => {
logger.error(`Startup Error:`, error);
.catch(error => {
if (error.code === 'ECONNREFUSED') {
return logger.error('Connection refused. The daemon may not be running.')
} else if (error.message) {
logger.error(error.message);
}
logger.error(error);
});
};
};

View file

@ -125,4 +125,20 @@ module.exports = {
});
});
},
getWalletBalance () {
const gaStartTime = Date.now();
return new Promise((resolve, reject) => {
axios
.post(lbrynetUri, {
method: 'wallet_balance',
})
.then(response => {
sendGATimingEvent('lbrynet', 'getWalletBalance', 'SETTINGS_GET', gaStartTime, Date.now());
handleLbrynetResponse(response, resolve, reject);
})
.catch(error => {
reject(error);
});
});
},
};

View file

@ -0,0 +1,21 @@
const Sequelize = require('sequelize');
const createDatabaseIfNotExists = () => {
const {database, username, password} = require('../../../config/mysqlConfig.js');
const sequelize = new Sequelize('', username, password, {
dialect : 'mysql',
logging : false,
operatorsAliases: false,
});
return new Promise((resolve, reject) => {
sequelize.query(`CREATE DATABASE IF NOT EXISTS ${database};`)
.then(() => {
resolve();
})
.catch(error => {
reject(error);
});
});
};
module.exports = createDatabaseIfNotExists;