Skip to content

Commit 1ecd5c7

Browse files
authored
fix(enabler): Add unregistration method to Node.js enabler (#1214)
Signed-off-by: at670475 <andrea.tabone@broadcom.com>
1 parent 7ad1946 commit 1ecd5c7

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

onboarding-enabler-nodejs-sample-app/src/index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ const args = {
2020
// The APIML stores such certificates in files with `-ebcdic` suffix
2121
};
2222

23+
const app = express();
24+
let httpsServer;
2325
/**
2426
* Registers the service to the APIML Discovery service
2527
*/
2628
/**
2729
* Starts the REST API service as an HTTPS server
2830
*/
2931
function startHttpsService() {
30-
const app = express();
3132

3233
// Index page with a link to the REST API endpoint:
3334
app.get("/", (req, res) =>
@@ -49,15 +50,43 @@ function startHttpsService() {
4950
app.get("/api/v1/status", (req, res) => res.json({ status: "UP" }));
5051

5152
// Static resources (contains Swagger JSON document with API documentation):
52-
app.use(express.static("static"));
53+
app.use(express.static("src/static"));
5354

5455
// Start HTTPS server and register to Discovery Service:
5556
tlsOptions = apiLayerService.tlsOptions;
56-
const httpsServer = https.createServer(tlsOptions, app);
57+
httpsServer = https.createServer(tlsOptions, app);
5758
httpsServer.listen(args.port, function () {
5859
console.log(`${args.serviceId} service listening on port ${args.port}`);
5960
apiLayerService.connectToEureka();
6061
});
6162
}
6263

6364
startHttpsService();
65+
66+
process.on('SIGTERM', signal => {
67+
apiLayerService.unregisterFromEureka();
68+
httpsServer.close(() => {
69+
process.exit(0);
70+
});
71+
});
72+
73+
process.on('SIGINT', signal => {
74+
apiLayerService.unregisterFromEureka();
75+
httpsServer.close(() => {
76+
process.exit(0);
77+
});
78+
});
79+
80+
process.on('uncaughtException', err => {
81+
apiLayerService.unregisterFromEureka();
82+
httpsServer.close(() => {
83+
process.exit(1);
84+
});
85+
});
86+
87+
process.on('unhandledRejection', (reason, promise) => {
88+
apiLayerService.unregisterFromEureka();
89+
httpsServer.close(() => {
90+
process.exit(1);
91+
});
92+
});

onboarding-enabler-nodejs/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ This is the onboarding Node.js enabler for [Zowe API Mediation Layer](https://gi
1818
apiLayerService.connectToEureka();
1919
});
2020

21+
```
22+
To make sure that your application will automatically unregister from Eureka once shut down, you can use the `unregisterFromEureka()` function, like shown in the example below.
23+
**Example:**
24+
25+
```js
26+
process.on('SIGINT', signal => {
27+
apiLayerService.unregisterFromEureka();
28+
httpsServer.close(() => {
29+
process.exit(0);
30+
});
31+
});
32+
2133
```
2234

2335
3. Create a yaml file named `service-configuration.yml`, add the configuration properties and place the yaml file inside a `/config` directory at the same level of your `index.js`.

onboarding-enabler-nodejs/src/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ let keyFile = null;
1717
let caFile = null;
1818
let passPhrase = null;
1919

20-
// Read ssl service configuration
20+
/**
21+
* Read ssl service configuration
22+
*/
2123
function readTlsProps() {
2224
try {
2325
const config = yaml.load(fs.readFileSync('config/service-configuration.yml', 'utf8'));
@@ -47,6 +49,9 @@ const client = new Eureka({
4749
}
4850
});
4951

52+
/**
53+
* Function that uses the eureka-js-client library to register the application to Eureka
54+
*/
5055
function connectToEureka() {
5156
client.start(function(error) {
5257
if (error != null) {
@@ -55,9 +60,17 @@ function connectToEureka() {
5560
});
5661
}
5762

63+
/**
64+
* Unregister the Eureka client from Eureka (i.e. when the application down)
65+
*/
66+
function unregisterFromEureka() {
67+
console.log("\nUnregistering the service from Eureka...")
68+
client.stop();
69+
}
70+
5871
connectToEureka();
5972

60-
module.exports = {connectToEureka, tlsOptions};
73+
module.exports = {connectToEureka, tlsOptions, unregisterFromEureka};
6174

6275

6376

0 commit comments

Comments
 (0)