From 10c6fb611f8edd65312f629272643e080eef526e Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 17 May 2018 14:42:58 -0700 Subject: [PATCH 1/3] removed error test log and added db creation --- config/loggerConfig.js | 11 +++++----- index.js | 8 +++++-- .../models/utils/createDatabaseIfNotExists.js | 22 +++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 server/models/utils/createDatabaseIfNotExists.js diff --git a/config/loggerConfig.js b/config/loggerConfig.js index f2097e35..df81d7e5 100644 --- a/config/loggerConfig.js +++ b/config/loggerConfig.js @@ -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'); }; }; diff --git a/index.js b/index.js index 74e913e8..a812a220 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ 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'); function Server () { this.configureLogger = loggerConfig.update; @@ -81,8 +82,11 @@ function Server () { const db = require('./server/models'); const PORT = siteConfig.details.port; // sync sequelize - db.sequelize.sync() - // start the server + createDatabaseIfNotExists() + .then(() => { + db.sequelize.sync() + }) + // start the server .then(() => { this.server.listen(PORT, () => { logger.info(`Server is listening on PORT ${PORT}`); diff --git a/server/models/utils/createDatabaseIfNotExists.js b/server/models/utils/createDatabaseIfNotExists.js new file mode 100644 index 00000000..d8ab0e02 --- /dev/null +++ b/server/models/utils/createDatabaseIfNotExists.js @@ -0,0 +1,22 @@ +const Sequelize = require('sequelize'); + +const createDatabaseIfNotExists = () => { + const {database, username, password} = require('../../../config/mysqlConfig.js'); + const sequelize = new Sequelize('', username, password, { + dialect : 'mysql', + logging : true, + operatorsAliases: false, + }); + console.log('username:', username, 'password:', password); + return new Promise((resolve, reject) => { + sequelize.query(`CREATE DATABASE IF NOT EXISTS ${database};`) + .then(() => { + resolve(); + }) + .catch(error => { + reject(error); + }); + }); +}; + +module.exports = createDatabaseIfNotExists; -- 2.45.2 From 8118d5b196061c761d8a5a8d5ecb8408805a9b70 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 17 May 2018 18:11:22 -0700 Subject: [PATCH 2/3] added check for lbrynet via startup wallet balance check --- index.js | 14 ++++++++++++-- server/lbrynet/index.js | 16 ++++++++++++++++ server/models/utils/createDatabaseIfNotExists.js | 3 +-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a812a220..32fd784d 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ 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; @@ -83,6 +84,10 @@ function Server () { const PORT = siteConfig.details.port; // sync sequelize createDatabaseIfNotExists() + getWalletBalance() + .then(balance => { + logger.info('starting LBC balance:', balance); + }) .then(() => { db.sequelize.sync() }) @@ -92,8 +97,13 @@ function Server () { 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); }); }; }; diff --git a/server/lbrynet/index.js b/server/lbrynet/index.js index c8ea72a1..90a45875 100644 --- a/server/lbrynet/index.js +++ b/server/lbrynet/index.js @@ -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); + }); + }); + }, }; diff --git a/server/models/utils/createDatabaseIfNotExists.js b/server/models/utils/createDatabaseIfNotExists.js index d8ab0e02..4a717067 100644 --- a/server/models/utils/createDatabaseIfNotExists.js +++ b/server/models/utils/createDatabaseIfNotExists.js @@ -4,10 +4,9 @@ const createDatabaseIfNotExists = () => { const {database, username, password} = require('../../../config/mysqlConfig.js'); const sequelize = new Sequelize('', username, password, { dialect : 'mysql', - logging : true, + logging : false, operatorsAliases: false, }); - console.log('username:', username, 'password:', password); return new Promise((resolve, reject) => { sequelize.query(`CREATE DATABASE IF NOT EXISTS ${database};`) .then(() => { -- 2.45.2 From 968880c256011079a2ac109b8382f12065842a0a Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 17 May 2018 19:01:35 -0700 Subject: [PATCH 3/3] cleaned up server formatting --- index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 32fd784d..7c3ed5ca 100644 --- a/index.js +++ b/index.js @@ -84,15 +84,12 @@ function Server () { const PORT = siteConfig.details.port; // sync sequelize createDatabaseIfNotExists() - getWalletBalance() + .then(() => { + return getWalletBalance(); + }) .then(balance => { logger.info('starting LBC balance:', balance); - }) - .then(() => { - db.sequelize.sync() - }) - // start the server - .then(() => { + db.sequelize.sync(); this.server.listen(PORT, () => { logger.info(`Server is listening on PORT ${PORT}`); }); -- 2.45.2