Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
nodejs-express: Shut down Kafka clients when HTTP server is closed (#832
Browse files Browse the repository at this point in the history
)

* nodejs-express: Shut down Kafka clients when HTTP server is closed

This PR illustrates how to avoid a hang when running the stack tests where the application makes connections to external services.

* Bump version in stack.yaml

* Bump stack version

Co-authored-by: Neeraj Laad <neeraj.laad@gmail.com>
  • Loading branch information
djones6 and neeraj-laad authored Jun 26, 2020
1 parent 9cff0b9 commit 33376d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion incubator/nodejs-express/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Node.js Express
version: 0.4.11
version: 0.4.12
description: Express web framework for Node.js
license: Apache-2.0
language: nodejs
Expand Down
13 changes: 11 additions & 2 deletions incubator/nodejs-express/templates/kafka/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ module.exports = (options) => {
// 'log.connection.close' : false,
}

Produce(Kafka, config, log, app);
Consume(Kafka, brokers, log);
producer = Produce(Kafka, config, log, app);
consumer = Consume(Kafka, brokers, log);

// Disconnect our Kafka clients when the HTTP server is stopped.
options.server.on('close', function () {
log.info('Disconnecting Kafka clients');
producer.disconnect();
consumer.disconnect();
});

return app;
};
Expand Down Expand Up @@ -89,6 +96,7 @@ function Produce(Kafka, config, log, app) {

// starting the producer
producer.connect();
return producer;
}

// Based on:
Expand Down Expand Up @@ -145,4 +153,5 @@ function Consume(Kafka, brokers, log) {

// starting the consumer
consumer.connect();
return consumer;
}

0 comments on commit 33376d5

Please sign in to comment.