Skip to content

Commit

Permalink
#8381 avoid callback functions and replace with Promises (fixes Rocke…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkurz committed Apr 15, 2018
1 parent 84d9dc9 commit 1cd2dbd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 88 deletions.
100 changes: 52 additions & 48 deletions packages/chatpal-search/server/provider/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,26 @@ class ChatpalProvider extends SearchProvider {
/**
* ping if configuration has been set correctly
* @param config
* @param callback (err,result) if ping was successfull
* @param resolve if ping was successfull
* @param reject if some error occurs
* @param timeout until ping is repeated
* @private
*/
_ping(config, callback, timeout = 5000) {
_ping(config, resolve, reject, timeout = 5000) {

const maxTimeout = 200000;

const stats = Index.ping(config);

if (stats) {
ChatpalLogger.debug('ping was successfull');
callback(config, stats);
resolve({config, stats});
} else {

ChatpalLogger.warn(`ping failed, retry in ${ timeout } ms`);

this._pingTimeout = Meteor.setTimeout(() => {
this._ping(config, callback, Math.min(maxTimeout, 2*timeout));
this._ping(config, resolve, reject, Math.min(maxTimeout, 2*timeout));
}, timeout);
}

Expand All @@ -213,81 +214,84 @@ class ChatpalProvider extends SearchProvider {
* @param callback
* @private
*/
_getIndexConfig(callback) {

const config = {
backendtype: this._settings.get('Backend')
};

if (this._settings.get('Backend') === 'cloud') {
config.baseurl = this.chatpalBaseUrl;
config.language = this._settings.get('Main_Language');
config.searchpath = '/search/search';
config.updatepath = '/search/update';
config.pingpath = '/search/ping';
config.clearpath = '/search/clear';
config.suggestionpath = '/search/suggest';
config.httpOptions = {
headers: {
'X-Api-Key': this._settings.get('API_Key')
}
};
} else {
config.baseurl = this._settings.get('Base_URL').endsWith('/') ? this._settings.get('Base_URL').slice(0, -1) : this._settings.get('Base_URL');
config.language = this._settings.get('Main_Language');
config.searchpath = '/chatpal/search';
config.updatepath = '/chatpal/update';
config.pingpath = '/chatpal/ping';
config.clearpath = '/chatpal/clear';
config.suggestionpath = '/chatpal/suggest';
config.httpOptions = {
headers: this._parseHeaders()
_getIndexConfig() {

return new Promise((resolve, reject) => {
const config = {
backendtype: this._settings.get('Backend')
};
}

config.batchSize = this._settings.get('BatchSize');
config.timeout = this._settings.get('TimeoutSize');
config.windowSize = this._settings.get('WindowSize');
if (this._settings.get('Backend') === 'cloud') {
config.baseurl = this.chatpalBaseUrl;
config.language = this._settings.get('Main_Language');
config.searchpath = '/search/search';
config.updatepath = '/search/update';
config.pingpath = '/search/ping';
config.clearpath = '/search/clear';
config.suggestionpath = '/search/suggest';
config.httpOptions = {
headers: {
'X-Api-Key': this._settings.get('API_Key')
}
};
} else {
config.baseurl = this._settings.get('Base_URL').endsWith('/') ? this._settings.get('Base_URL').slice(0, -1) : this._settings.get('Base_URL');
config.language = this._settings.get('Main_Language');
config.searchpath = '/chatpal/search';
config.updatepath = '/chatpal/update';
config.pingpath = '/chatpal/ping';
config.clearpath = '/chatpal/clear';
config.suggestionpath = '/chatpal/suggest';
config.httpOptions = {
headers: this._parseHeaders()
};
}

this._ping(config, callback);
config.batchSize = this._settings.get('BatchSize');
config.timeout = this._settings.get('TimeoutSize');
config.windowSize = this._settings.get('WindowSize');

this._ping(config, resolve, reject);
});

}

/**
* @inheritDoc
* @param callback
*/
stop(callback) {
stop(resolve) {
ChatpalLogger.info('Provider stopped');
Meteor.clearTimeout(this._pingTimeout);
this.indexFail = false;
this.index && this.index.stop();
callback();
resolve();
}

/**
* @inheritDoc
* @param reason
* @param callback
* @param resolve
* @param reject
*/
start(reason, callback) {
start(reason, resolve, reject) {

const clear = this._checkForClear(reason);

ChatpalLogger.debug(`clear = ${ clear } with reason '${ reason }'`);

this._getIndexConfig((config, stats) => {
this._indexConfig = config;
this._getIndexConfig().then((server) => {
this._indexConfig = server.config;

this._stats = stats;
this._stats = server.stats;

ChatpalLogger.debug('config:', JSON.stringify(this._indexConfig, null, 2));
ChatpalLogger.debug('stats:', JSON.stringify(this._stats, null, 2));

this.index = new Index(this._indexConfig, this.indexFail || clear, stats.message.oldest || new Date().valueOf());
this.index = new Index(this._indexConfig, this.indexFail || clear, this._stats.message.oldest || new Date().valueOf());

callback();
});
resolve();
}, reject);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions packages/rocketchat-search/server/model/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,18 @@ export default class SearchProvider {

/*--- livecycle ---*/
run(reason, callback) {
this._settings.load();
this.start(reason, callback);
return new Promise((resolve, reject) => {
this._settings.load();
this.start(reason, resolve, reject);
});
}

start(reason, callback) {
callback();
start(reason, resolve) {
resolve();
}

stop(callback) {
callback();
stop(resolve) {
resolve();
}
}

62 changes: 28 additions & 34 deletions packages/rocketchat-search/server/service/providerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,51 @@ class SearchProviderService {
* @param id the id of the provider which should be started
* @param cb a possible callback if provider is active or not (currently not in use)
*/
use(id, cb = function() {}) {
use(id) {

if (!this.providers[id]) { throw new Error(`provider ${ id } cannot be found`); }

let reason = 'switch';

if (!this.activeProvider) {
reason = 'startup';
} else if (this.activeProvider.key === this.providers[id].key) {
reason = 'update';
}

const stopProvider = (callback) => {
if (this.activeProvider) {
return new Promise((resolve, reject) => {
if (!this.providers[id]) { throw new Error(`provider ${ id } cannot be found`); }

SearchLogger.debug(`Stopping provider '${ this.activeProvider.key }'`);
let reason = 'switch';

this.activeProvider.stop(callback);
} else {
callback();
if (!this.activeProvider) {
reason = 'startup';
} else if (this.activeProvider.key === this.providers[id].key) {
reason = 'update';
}
};

stopProvider((err)=>{
const stopProvider = () => {
return new Promise((resolve, reject) => {
if (this.activeProvider) {

if (!err) {
SearchLogger.debug(`Stopping provider '${ this.activeProvider.key }'`);

this.activeProvider.stop(resolve, reject);
} else {
resolve();
}
});
};

stopProvider().then(() => {
this.activeProvider = undefined;

SearchLogger.debug(`Start provider '${ id }'`);

try {

this.providers[id].run(reason, (err) => {
if (err) {
cb(err);
} else {
this.activeProvider = this.providers[id];
cb();
}
});
this.providers[id].run(reason).then(() => {
this.activeProvider = this.providers[id];
resolve();
}, reject);

} catch (e) {
cb(e);
reject(e);
}

} else {
cb(err);
}
}, reject);

});

}

/**
Expand Down Expand Up @@ -126,7 +120,7 @@ class SearchProviderService {
const providerId = RocketChat.settings.get('Search.Provider');

if (providerId) {
this.use(providerId);
this.use(providerId);//TODO do something with success and errors
}

}), 1000);
Expand Down

0 comments on commit 1cd2dbd

Please sign in to comment.