From 5b7214e464961b69deebbb926718e73d33fb95df Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Wed, 23 Nov 2016 10:17:55 -0800 Subject: [PATCH] [local-sync]: Let local-sync start, add loadModels to iso-core --- packages/isomorphic-core/index.js | 1 + packages/isomorphic-core/src/models/index.js | 27 +++++++ packages/local-sync/main.es6 | 2 +- .../local-sync/src/local-sync-worker/app.js | 46 +++++------- .../src/shared/local-database-connector.js | 72 ++----------------- 5 files changed, 50 insertions(+), 98 deletions(-) create mode 100644 packages/isomorphic-core/src/models/index.js diff --git a/packages/isomorphic-core/index.js b/packages/isomorphic-core/index.js index b54e88e98..4ac562016 100644 --- a/packages/isomorphic-core/index.js +++ b/packages/isomorphic-core/index.js @@ -7,4 +7,5 @@ module.exports = { IMAPConnection: require('./src/imap-connection'), IMAPErrors: require('./src/imap-errors'), PromiseUtils: require('./src/promise-utils'), + loadModels: require('./src/models'), } diff --git a/packages/isomorphic-core/src/models/index.js b/packages/isomorphic-core/src/models/index.js new file mode 100644 index 000000000..c4ec9a187 --- /dev/null +++ b/packages/isomorphic-core/src/models/index.js @@ -0,0 +1,27 @@ +const fs = require('fs'); +const path = require('path'); + +function loadModels(Sequelize, sequelize, modelsPath, {schema} = {}) { + const db = {}; + const dirname = path.join(__dirname, modelsPath) + for (const filename of fs.readdirSync(dirname)) { + if (filename.endsWith('.js')) { + let model = require(filename)(sequelize, Sequelize) // eslint-disable-line + if (schema) { + model = model.schema(schema); + } + db[model.name[0].toUpperCase() + model.name.substr(1)] = model; + } + } + + Object.keys(db).forEach((modelName) => { + if ("associate" in db[modelName]) { + db[modelName].associate(db); + } + }); + + return db; +} + + +module.exports = loadModels diff --git a/packages/local-sync/main.es6 b/packages/local-sync/main.es6 index bd555b299..af5fafb3c 100644 --- a/packages/local-sync/main.es6 +++ b/packages/local-sync/main.es6 @@ -2,7 +2,7 @@ import {createLogger} from './src/shared/logger' export function activate() { global.Logger = createLogger('local-sync') - require('./src/local-api/app.js'); + // require('./src/local-api/app.js'); require('./src/local-sync-worker/app.js'); } diff --git a/packages/local-sync/src/local-sync-worker/app.js b/packages/local-sync/src/local-sync-worker/app.js index c6e593daa..1df8e50dd 100644 --- a/packages/local-sync/src/local-sync-worker/app.js +++ b/packages/local-sync/src/local-sync-worker/app.js @@ -1,35 +1,21 @@ const LocalDatabaseConnector = require('../shared/local-database-connector') +const os = require('os') +global.instanceId = os.hostname(); -const prepareEnvironmentInfo = (callback) => { - if (process.env.NODE_ENV === 'development') { - const os = require('os') - global.instanceId = os.hostname(); - callback(); - } else { - const request = require('request') - request('http://169.254.169.254/latest/meta-data/instance-id', (error, response, body) => { - global.instanceId = body; - callback(); - }); - } -} +const SyncProcessManager = require('./sync-process-manager') +const manager = new SyncProcessManager(); -prepareEnvironmentInfo(() => { - const SyncProcessManager = require('./sync-process-manager') - const manager = new SyncProcessManager(); - - LocalDatabaseConnector.forShared().then((db) => { - const {Account} = db; - Account.findAll().then((accounts) => { - if (accounts.length === 0) { - global.Logger.info(`Couldn't find any accounts to sync. Run this CURL command to auth one!`) - global.Logger.info(`curl -X POST -H "Content-Type: application/json" -d '{"email":"inboxapptest1@fastmail.fm", "name":"Ben Gotow", "provider":"imap", "settings":{"imap_username":"inboxapptest1@fastmail.fm","imap_host":"mail.messagingengine.com","imap_port":993,"smtp_host":"mail.messagingengine.com","smtp_port":0,"smtp_username":"inboxapptest1@fastmail.fm", "smtp_password":"trar2e","imap_password":"trar2e","ssl_required":true}}' "http://localhost:5100/auth?client_id=123"`) - } - manager.ensureAccountIDsInRedis(accounts.map(a => a.id)).then(() => { - manager.start(); - }) - }); +LocalDatabaseConnector.forShared().then((db) => { + const {Account} = db; + Account.findAll().then((accounts) => { + if (accounts.length === 0) { + global.Logger.info(`Couldn't find any accounts to sync. Run this CURL command to auth one!`) + global.Logger.info(`curl -X POST -H "Content-Type: application/json" -d '{"email":"inboxapptest1@fastmail.fm", "name":"Ben Gotow", "provider":"imap", "settings":{"imap_username":"inboxapptest1@fastmail.fm","imap_host":"mail.messagingengine.com","imap_port":993,"smtp_host":"mail.messagingengine.com","smtp_port":0,"smtp_username":"inboxapptest1@fastmail.fm", "smtp_password":"trar2e","imap_password":"trar2e","ssl_required":true}}' "http://localhost:5100/auth?client_id=123"`) + } + manager.ensureAccountIDsInRedis(accounts.map(a => a.id)).then(() => { + manager.start(); + }) }); - - global.manager = manager; }); + +global.manager = manager; diff --git a/packages/local-sync/src/shared/local-database-connector.js b/packages/local-sync/src/shared/local-database-connector.js index 864fce8fd..d76628947 100644 --- a/packages/local-sync/src/shared/local-database-connector.js +++ b/packages/local-sync/src/shared/local-database-connector.js @@ -1,7 +1,7 @@ const Sequelize = require('sequelize'); const fs = require('fs'); const path = require('path'); -const {PromiseUtils} = require('isomorphic-core'); +const {loadModels, PromiseUtils} = require('isomorphic-core'); const HookTransactionLog = require('./hook-transaction-log'); const HookAccountCRUD = require('./hook-account-crud'); const HookIncrementVersionOnSave = require('./hook-increment-version-on-save'); @@ -18,46 +18,7 @@ class LocalDatabaseConnector { this._cache = {}; } - _readModelsInDirectory(sequelize, dirname, {schema} = {}) { - const db = {}; - for (const filename of fs.readdirSync(dirname)) { - if (filename.endsWith('.js')) { - let model = sequelize.import(path.join(dirname, filename)); - if (schema) { - model = model.schema(schema); - } - db[model.name[0].toUpperCase() + model.name.substr(1)] = model; - } - } - - Object.keys(db).forEach((modelName) => { - if ("associate" in db[modelName]) { - db[modelName].associate(db); - } - }); - - return db; - } - _sequelizePoolForDatabase(dbname) { - if (process.env.DB_HOSTNAME) { - return new Sequelize(dbname, process.env.DB_USERNAME, process.env.DB_PASSWORD, { - host: process.env.DB_HOSTNAME, - dialect: "mysql", - charset: 'utf8', - logging: false, - pool: { - min: 1, - max: 15, - idle: 5000, - }, - define: { - charset: 'utf8', - collate: 'utf8_general_ci', - }, - }); - } - return new Sequelize(dbname, '', '', { storage: path.join(STORAGE_DIR, `${dbname}.sqlite`), dialect: "sqlite", @@ -74,26 +35,8 @@ class LocalDatabaseConnector { return this._cache[accountId]; } - let newSequelize = null; - - if (process.env.DB_HOSTNAME) { - if (!this._accountsRootSequelize) { - this._accountsRootSequelize = this._sequelizePoolForDatabase(`account_data`); - } - - // Create a new sequelize instance, but tie it to the same connection pool - // as the other account instances. - newSequelize = this._sequelizePoolForDatabase(`account_data`); - newSequelize.dialect = this._accountsRootSequelize.dialect; - newSequelize.config = this._accountsRootSequelize.config; - newSequelize.connectionManager.close() - newSequelize.connectionManager = this._accountsRootSequelize.connectionManager; - } else { - newSequelize = this._sequelizePoolForDatabase(`a-${accountId}`); - } - - const modelsPath = path.join(__dirname, 'models/account'); - const db = this._readModelsInDirectory(newSequelize, modelsPath, {schema: `a${accountId}`}) + const newSequelize = this._sequelizePoolForDatabase(`a-${accountId}`); + const db = loadModels(Sequelize, newSequelize, 'account', {schema: `a${accountId}`}) HookTransactionLog(db, newSequelize); HookIncrementVersionOnSave(db, newSequelize); @@ -120,18 +63,13 @@ class LocalDatabaseConnector { destroyAccountDatabase(accountId) { const dbname = `a-${accountId}`; - if (process.env.DB_HOSTNAME) { - // todo - } else { - fs.removeFileSync(path.join(STORAGE_DIR, `${dbname}.sqlite`)); - } + fs.removeFileSync(path.join(STORAGE_DIR, `${dbname}.sqlite`)); return Promise.resolve() } _sequelizeForShared() { const sequelize = this._sequelizePoolForDatabase(`shared`); - const modelsPath = path.join(__dirname, 'models/shared'); - const db = this._readModelsInDirectory(sequelize, modelsPath) + const db = loadModels(Sequelize, sequelize, 'shared') HookAccountCRUD(db, sequelize);