Skip to content

Commit

Permalink
Merge pull request #29 from Notastica/fix-db-initializtion-between-re…
Browse files Browse the repository at this point in the history
…starts

fix db initialization between restarts
  • Loading branch information
Panthro committed Aug 21, 2016
2 parents 260d0c1 + b9912a7 commit e1e7d3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Promise from 'bluebird';
import logger from './logging/logger';
import _ from 'lodash';
import EventEmitter from 'events';
import JSON from 'json3';
import * as JSON from 'json3';

/**
* Module class that represents a module that is connected directly to the Orchestrator.
Expand Down
23 changes: 18 additions & 5 deletions src/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Promise from 'bluebird';
import _ from 'lodash/core';
import uuid from 'node-uuid';
import * as temp from 'temp';
import JSON from 'json3';
import * as JSON from 'json3';

/**
* The Orchestrator is the class that manages all modules
Expand Down Expand Up @@ -75,6 +75,14 @@ class Orchestrator {
*/
this.messagesQueue = options.messagesQueue;


/**
* Wether the database was fully initialized or not.
* @type {boolean}
* @private
*/
this._dbInitialized = false;

/**
* The lokijs database;
* @type {Loki}
Expand Down Expand Up @@ -158,7 +166,7 @@ class Orchestrator {
if (this._running) {
return Promise.resolve(this);
}
if (!this.modulesCollection) {
if (!this._dbInitialized) {
return new Promise((resolve) => {
logger.debug('Database not initialized, waiting 100ms');
setTimeout(() => {
Expand Down Expand Up @@ -486,14 +494,19 @@ class Orchestrator {
if (!this.modulesCollection) {
this.modulesCollection = this._db.addCollection(this.modulesCollectionName);
} else {
this.modulesCollection.find().forEach((m) => {

const parseAndRegister = (m) => {
if (typeof m === 'string') {
m = JSON.parse(m);
}
this.register(m);
});
return this.register(m);
};

Promise.all(this.modulesCollection.find().map(parseAndRegister));

logger.debug(`Database loaded with, ${this.modulesCollection.count()} modules`);
}
this._dbInitialized = true;
}
}

Expand Down

0 comments on commit e1e7d3f

Please sign in to comment.