From 08c7ca8b0daa78a31ebc79d578bd62edb7ed3abf Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Thu, 14 Jul 2016 18:52:30 -0700 Subject: [PATCH] Use EC2 instance ID instead of hostname --- package.json | 1 + .../models/account/syncbackRequest.js | 4 +- packages/nylas-sync/app.js | 47 +++++++++++++------ packages/nylas-sync/sync-process-manager.js | 3 +- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 3d887676a..313d7604e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "promise-props": "^1.0.0", "promise.prototype.finally": "^1.0.1", "redis": "2.x.x", + "request": "^2.73.0", "rx": "4.x.x", "sequelize": "3.x.x", "underscore": "1.x.x", diff --git a/packages/nylas-core/models/account/syncbackRequest.js b/packages/nylas-core/models/account/syncbackRequest.js index 007be0223..720461ea0 100644 --- a/packages/nylas-core/models/account/syncbackRequest.js +++ b/packages/nylas-core/models/account/syncbackRequest.js @@ -20,7 +20,7 @@ module.exports = (sequelize, Sequelize) => { error: this.error, props: this.props, } - } - } + }, + }, }); }; diff --git a/packages/nylas-sync/app.js b/packages/nylas-sync/app.js index 1d8ccd647..7f32ad355 100644 --- a/packages/nylas-sync/app.js +++ b/packages/nylas-sync/app.js @@ -1,8 +1,7 @@ -const Metrics = require(`nylas-metrics`) +const Metrics = require('nylas-metrics') Metrics.startCapturing('nylas-k2-sync') const {DatabaseConnector, Logger} = require('nylas-core') -const SyncProcessManager = require('./sync-process-manager'); global.Metrics = Metrics global.Logger = Logger.createLogger('nylas-k2-sync') @@ -14,19 +13,37 @@ const onUnhandledError = (err) => { process.on('uncaughtException', onUnhandledError) process.on('unhandledRejection', onUnhandledError) -const manager = new SyncProcessManager(); -DatabaseConnector.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(); - }) +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(); + }); + } +} + +prepareEnvironmentInfo(() => { + const SyncProcessManager = require('./sync-process-manager') + const manager = new SyncProcessManager(); + + DatabaseConnector.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/nylas-sync/sync-process-manager.js b/packages/nylas-sync/sync-process-manager.js index 0c2e79b3a..bf23f1a99 100644 --- a/packages/nylas-sync/sync-process-manager.js +++ b/packages/nylas-sync/sync-process-manager.js @@ -1,8 +1,7 @@ -const os = require('os'); const SyncWorker = require('./sync-worker'); const {PromiseUtils, DatabaseConnector, PubsubConnector, SchedulerUtils} = require(`nylas-core`) -const IDENTITY = `${os.hostname()}-${process.pid}`; +const IDENTITY = `${global.instanceId}-${process.pid}`; const { ACCOUNTS_FOR,