Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bugthesystem committed Feb 2, 2016
1 parent 33f5a9e commit 48bb5c4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microphone-consul",
"version": "1.0.6",
"version": "1.1.0",
"description": "Consul provider for microphone.js Microservice framework",
"main": "lib/index.js",
"scripts": {
Expand All @@ -25,7 +25,7 @@
"license": "MIT",
"dependencies": {
"cron": "^1.1.0",
"microphone-core": "^1.0.7",
"microphone-core": "^1.1.0",
"request": "^2.67.0"
},
"devDependencies": {
Expand Down
11 changes: 7 additions & 4 deletions src/consul-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ export default class ConsulProvider extends ClusterProvider {
this.__startReaper();
}

async bootstrapClientAsync() {
this.__startReaper();

//Task.FromResult(0);
async bootstrapClientAsync() {
return new Promise((resolve, reject)=> {
return resolve({});
try {
this.__startReaper();
return resolve({status: 'STARTED'});
} catch (ex) {
reject(ex);
}
});
}

Expand Down
33 changes: 33 additions & 0 deletions src/consul-rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import {ServiceInformation} from 'microphone-core';
const DEFAULT_PORT = 8500;
const DEFAULT_HOST = 'http://localhost';

/**
*
*/
export default class ConsulRestClient {
constructor(address, port) {
this._address = address || DEFAULT_HOST;
this._port = port || DEFAULT_PORT;
}

/**
*
* @param serviceName
* @param serviceId
* @param address
*/
async registerServiceAsync(serviceName, serviceId, address) {
let payload = {
ID: serviceId,
Expand All @@ -34,6 +43,11 @@ export default class ConsulRestClient {
await this.__request(options, "Could not register service");
}

/**
*
* @param serviceName
* @returns {Array|*|{}}
*/
async findServiceAsync(serviceName) {
let options = {
uri: `${this._address}:${this._port}/v1/health/service/${serviceName}`,
Expand All @@ -45,6 +59,10 @@ export default class ConsulRestClient {
return serviceArray.map(svcItem => new ServiceInformation(svcItem["Service"]["Address"], svcItem["Service"]["Port"]));
}

/**
*
* @returns {Array|*|{}}
*/
async getCriticalServicesAsync() {
var options = {
uri: `${this._address}:${this._port}/v1/health/state/critical`,
Expand All @@ -56,6 +74,10 @@ export default class ConsulRestClient {
return serviceArray.map(svcItem => svcItem["ServiceID"]);
}

/**
*
* @param serviceId
*/
async unRegisterServiceAsync(serviceId) {
var options = {
uri: `${this._address}:${this._port}/v1/agent/service/deregister/${serviceId}`,
Expand All @@ -65,10 +87,21 @@ export default class ConsulRestClient {
await this.__request(options, "Could not de register service");
}

/**
*
* @param port
*/
setPort(port) {
this._port = port || DEFAULT_PORT;
}

/**
*
* @param options
* @param message
* @returns {Promise}
* @private
*/
__request(options, message) {
return new Promise((resolve, reject)=> {
request(options, (error, response, body)=> {
Expand Down
9 changes: 9 additions & 0 deletions tests/consul-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ describe('ConsulRestClient:', ()=> {
});
});

describe('#bootstrapClientAsync', ()=> {

it('should start reaper', async ()=> {
let resp = await provider.bootstrapClientAsync();
console.log(resp);
resp.status.should.be.equal("STARTED");
});
});

});

0 comments on commit 48bb5c4

Please sign in to comment.