Skip to content

Commit

Permalink
Merge pull request #70 from thiagobustamante/master
Browse files Browse the repository at this point in the history
FIX gateway starting
  • Loading branch information
thiagobustamante committed Oct 30, 2017
2 parents 8a1581f + fe71633 commit be62fcb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tree-gateway",
"version": "1.1.13",
"version": "1.1.14",
"homepage": "http://treegateway.org",
"description": "The Tree Gateway API Gateway",
"author": "Thiago da Rosa de Bustamante <trbustamante@gmail.com>",
Expand Down
56 changes: 33 additions & 23 deletions src/application.ts
Expand Up @@ -24,7 +24,7 @@ export class Application {
});
}

cluster(instances: number){
cluster(instances: number) {
// tslint:disable:no-console
if (cluster.isMaster) {
const n = instances < 1 ? os.cpus().length : instances;
Expand Down Expand Up @@ -59,31 +59,41 @@ export class Application {
});
}

private startGateway() : Promise<void> {
private startGateway(): Promise<void> {
return new Promise<void>((resolve, reject) => {
const config: Configuration = Container.get(Configuration);
config.on('load', () => {
const gateway: Gateway = Container.get(Gateway);
const database: Database = Container.get(Database);
if (gateway.running) {
return resolve();
}
gateway.start()
.then(() => gateway.startAdmin())
.then(resolve)
.catch(reject);
if (config.loaded) {
this.runGateway().then(resolve).catch(reject);
} else {
config.on('load', () => {
this.runGateway().then(resolve).catch(reject);
});
}
});
}

function graceful() {
gateway.stopAdmin()
.then(() => gateway.stop())
.then(() => database.disconnect())
.then(() => process.exit(0));
}
private runGateway() {
return new Promise<void>((resolve, reject) => {
const gateway: Gateway = Container.get(Gateway);
const database: Database = Container.get(Database);
if (gateway.running) {
return resolve();
}
gateway.start()
.then(() => gateway.startAdmin())
.then(resolve)
.catch(reject);

// Stop graceful
process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);
});
function graceful() {
gateway.stopAdmin()
.then(() => gateway.stop())
.then(() => database.disconnect())
.then(() => process.exit(0));
}

// Stop graceful
process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);
});
}
}
}
1 change: 1 addition & 0 deletions src/configuration.ts
Expand Up @@ -193,6 +193,7 @@ export class Configuration extends EventEmitter {
if (!this.config.gateway.protocol) {
return reject(new Error('GatewayConfig protocol is required.'));
}
resolve();
} catch (e) {
return reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -5,4 +5,4 @@ import { Application } from './application';

const app = new Application();

app.start();
app.start();

0 comments on commit be62fcb

Please sign in to comment.