Skip to content

Commit

Permalink
Merge pull request #12 from Notastica/move-init-to-constructor
Browse files Browse the repository at this point in the history
moving init to constructor
  • Loading branch information
Panthro committed Aug 14, 2016
2 parents dfa52f1 + 29b67fc commit b351b01
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 99 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"elasticsearch": "^11.0.1",
"isprod": "^1.1.0",
"jsonpath": "^0.2.6",
"lodash": "4.15.0",
"lokijs": "^1.4.1",
"node-uuid": "^1.4.7",
"rabbit.js": "0.4.4",
Expand Down
77 changes: 32 additions & 45 deletions src/orchestrator.js
Expand Up @@ -7,43 +7,43 @@ import * as mq from './mq/connection';
import * as es from './es/connection';
import * as symbols from './utils/symbols';
import Promise from 'bluebird';
import _ from 'lodash/core';

class Orchestrator {
constructor() {
this.dbPath = 'orchestrator.js';
this.name = dockerNames.getRandomName(false);
this.db = null;
this.modulesCollection = null;
this.initialized = false;
this.running = false;
this.order = 0;
this.registerQueue = null;
}

/**
* Initialise the Orchestrator
* @param {Object} [options]
* @return {Promise}
*/
init(options) {
return new Promise((resolve) => {
if (this.initialized) {
resolve();
}
options = options || {};
logger.info('Initializing Orchestrator with options', options);
this.name = options.name || this.name;
this.dbPath = options.dbPath || this.dbPath;
this.db = new Loki(this.dbName);
this.modulesCollection = this.db.addCollection('modules');
logger.debug('Database initialized with name', this.dbPath);
this.registerQueue = options.registerQueue || 'o_register';
this.amqpContext = null;
this.amqpURL = options.amqpURL || 'amqp://localhost:5672';
this.esClient = null;
this.initialized = true;
resolve(this.initialized);
});

constructor(options) {
const defaults = {
dbPath: 'orchestrator.js',
name: dockerNames.getRandomName(false),
modulesCollectionName: 'modules',
registerQueue: 'o_register',
messagesQueue: 'o_messages',
amqpURL: 'amqp://localhost:5672'
};

options = _.defaults(options || {}, defaults);
logger.info('Initializing Orchestrator with options', options);

this.dbPath = options.dbPath;
this.name = options.name;
this.running = false;
this.order = 0;

this.messagesQueue = options.messagesQueue;
this.name = options.name || this.name;
this.dbPath = options.dbPath || this.dbPath;
this.db = new Loki(this.dbName);
this.modulesCollection = this.db.addCollection(options.modulesCollectionName);
this.registerQueue = options.registerQueue;
this.amqpURL = options.amqpURL;
this.amqpContext = null;
this.esClient = null;
}


Expand Down Expand Up @@ -94,6 +94,7 @@ class Orchestrator {
}
logger.info(`[${symbols.check}] Elasticsearch disconnected`);
this.running = false;
this.modulesCollection.clear();
}
}

Expand All @@ -119,20 +120,6 @@ class Orchestrator {
});
}

/**
* Check the Orchestrator is initialized
* @return {Promise}
*/
checkInit() {
return new Promise((resolve, reject) => {
if (!this.initialized) {
logger.warn('Orchestrator not initialized');
reject('Orchestrator not initialized');
}
resolve(this.initialized);
});

}

/**
* Check if the module is of type {Module} if not convert to it
Expand All @@ -152,7 +139,7 @@ class Orchestrator {
*/
unregister(module) {

return this.checkInit()
return Promise.resolve()
.then(() => {
module = Orchestrator.checkModule(module);
const previousModule = this.modulesCollection.find({ uuid: module.uuid });
Expand Down Expand Up @@ -182,7 +169,7 @@ class Orchestrator {
* @return {*|Promise}
*/
register(module) {
return this.checkInit().then(() => {
return Promise.resolve().then(() => {
module = Orchestrator.checkModule(module);
logger.info('Registering new module', module);

Expand Down
87 changes: 33 additions & 54 deletions test/orchestrator.spec.js
Expand Up @@ -22,9 +22,7 @@ describe('Orchestrator', function () {

before(function () {
o = new Orchestrator();
o.init().then(() => {
o.listen();
});
return o.listen();
});

after(function () {
Expand All @@ -33,23 +31,14 @@ describe('Orchestrator', function () {

const serviceName = dockerNames.getRandomName(false);


it('Should reject when not initialized', function () {
return new Promise((resolve, reject) => {
const or = new Orchestrator();

or.register(new Module({ name: 'test', service: serviceName })).then(reject, resolve); // orders flipped as it should be rejected
});
});

it('Should init with default options', function () {
return new Orchestrator().init();
return new Orchestrator();
});

it('Should reregister a module if already registered', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const name = 'test-processor';
const negativePath = '$';
const updatedKeyValue = '$..name';
Expand Down Expand Up @@ -78,7 +67,7 @@ describe('Orchestrator', function () {
it('Should register a module', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const name = 'test-processor';


Expand All @@ -93,7 +82,7 @@ describe('Orchestrator', function () {
it('Should unregister a module', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const name = 'test-processor';

return o.register(new Module({ service: serviceName, name: name })).then((module) => {
Expand All @@ -112,7 +101,7 @@ describe('Orchestrator', function () {
it('Should find a module by negativePath', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const originalModule = new Module({ service: serviceName });

originalModule.negativePath = '$.negativeKeyName';
Expand All @@ -131,7 +120,7 @@ describe('Orchestrator', function () {
it('Should NOT find a module by negativePath', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const originalModule = new Module({ service: serviceName });

originalModule.negativePath = '$.key';
Expand All @@ -140,15 +129,15 @@ describe('Orchestrator', function () {
return o.findMatchingModules({ key: 'value' });
}).then((modules) => {
chai.expect(modules).to.be.a('array');
chai.expect(modules).to.be.empty();
chai.expect(modules).to.be.not.to.contains.key('uuid').that.is.equals(originalModule.uuid);
});
});
});

it('Should find a module by positivePath', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const originalModule = new Module({ service: serviceName });

originalModule.positivePath = '$.positiveKeyName';
Expand All @@ -167,7 +156,7 @@ describe('Orchestrator', function () {
it('Should NOT find a module by positivePath', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen().then(() => {
const originalModule = new Module({ service: serviceName });

originalModule.positivePath = '$.BadPositiveKeyName';
Expand All @@ -176,66 +165,56 @@ describe('Orchestrator', function () {
return o.findMatchingModules({ positiveKeyName: 'value' });
}).then((modules) => {
chai.expect(modules).to.be.a('array');
chai.expect(modules).to.be.empty();
chai.expect(modules).not.to.have.property('uuid').that.is.equals(originalModule.uuid);
});
});
});

it('Should listen with default options', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen();
});
return o.listen();
});

it('Should handle new modules registration by queue', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen();
}).then(() => {
const m = new Module({ service: dockerNames.getRandomName(false) });

return o.onNewModule(m).then(() => {
chai.expect(o.isRegistered(m)).to.be.true();
});
});
});
// it('Should handle new modules registration by queue', function () {
// // const o = new Orchestrator();
//
// return o.listen().then(() => {
// const m = new Module({ service: dockerNames.getRandomName(false) });
//
// return o.onNewModule(m).then(() => {
// chai.expect(o.isRegistered(m)).to.be.true();
// });
// });
// });

it('Should reject when bad modules are passed', function () {
// const o = new Orchestrator();

return o.init().then(() => {
return o.listen();
}).then(() => {
const m = { invalid_module: 'invalid' };
return o.listen()
.then(() => {
const m = { invalid_module: 'invalid' };

return o.onNewModule(m)
.catch((err) => {
chai.expect(err).to.be.not.null();
});
});
return o.onNewModule(m)
.catch((err) => {
chai.expect(err).to.be.not.null();
});
});
});

it('Should shutdown gracefully', function () {
// const o = new Orchestrator();

return o.init()
return o.listen()
.then(() => {
return o.listen();
}).then(() => {
return o.shutdown();
});
});

it('Should handle module registration on it\'s queue', function () {
// const o = new Orchestrator();

return o.init()
.then(() => {
return o.listen();
})
return o.listen()
.then(() => {
const m = new Module({ service: dockerNames.getRandomName(false), registerQueue: o.registerQueue });

Expand Down

0 comments on commit b351b01

Please sign in to comment.